机器学习-线性回归

一元线性线性回归



# 导入matplotlib库,主要用于可视化
import numpy as np
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
%matplotlib inline

# 引入本地字体文件,否则中文会有乱码
# font_set = FontProperties(fname=r"./work/ simsun.ttc", size=12)


# 构造用于训练的数据集
x_train = [4, 8, 5, 10, 12]
y_train = [20, 50, 30, 70, 60]


# 画图函数
def draw(x_train, y_train):
    plt.scatter(x_train, y_train)


# 定义函数求得斜率w和截距b
# 使用最小二乘法对斜率和截距求导并使得导数值等于0求解出斜率和截距
def fit(x_train, y_train):
    size = len(x_train)
    numerator = 0  # 初始化分子
    denominator = 0  # 初始化分母
    for i in range(size):
        numerator += (x_train[i] - np.mean(x_train)) * \
                     (y_train[i] - np.mean(y_train))
        denominator += (x_train[i] - np.mean(x_train)) ** 2
    w = numerator / denominator
    b = np.mean(y_train) - w * np.mean(x_train)
    return w, b


# 根据斜率w和截距b,输入x计算输出值
def predict(x, w, b):
    # 预测模型
    y = w * x + b
    return y


# 根据W,B画图
def fit_line(w, b):
    # 测试集进行测试,并作图
    # linspace 创建等差数列的函数    #numpy.limspace(start,stop,num,endpoint=True,retstep=False,dtype=None,axis=0#)
    x = np.linspace(4, 15, 9)
    y = w * x + b
    plt.plot(x, y)
    plt.show()


if __name__ == "__main__":
    draw(x_train, y_train)
    w, b = fit(x_train, y_train)
    print(w, b)  # 输出斜率和截距
fit_line(w, b)  # 绘制预测函数图像

多元线性回归

# 多元线性回归的实现
# 导入模块
import numpy as np
import pandas as pd

# 构造数据,前三列表示自变量X,最后一列表示因变量Y
data = np.array([[3, 2, 9, 20],
                 [4, 10, 2, 72],
                 [3, 4, 9, 21],
                 [12, 3, 4, 20]])
print("data:", data, "\n")

X = data[:, :-1]
Y = data[:, -1]

X = np.mat(np.c_[np.ones(X.shape[0]), X])  # 为系数矩阵增加常数项系数
Y = np.mat(Y)  # 数组转化为矩阵

print("X:", X, "\n")
print("Y:", Y, "\n")

# 根据最小二乘法的目标函数求导为0得到最优参数向量B的解析解公式如下,可以直接求取最优参数向量
B = np.linalg.inv(X.T * X) * (X.T) * (Y.T)
print("B:", B, "\n")  # 输出系数,第一项为常数项,其他为回归系数
print("1,60,60,60预测结果:", np.mat([1, 60, 60, 60]) * B, "\n")  # 预测结果

# 相关系数
Q_e = 0
Q_E = 0
Y_mean = np.mean(Y)
for i in range(Y.size):
    Q_e += pow(np.array((Y.T)[i] - X[i] * B), 2)
    Q_E += pow(np.array(X[i] * B) - Y_mean, 2)
R2 = Q_E / (Q_e + Q_E)
print("R2", R2)

**

回归库实现

# 导入sklearn下的LinearRegression 方法
from sklearn.linear_model import LinearRegression
import numpy as np

model = LinearRegression()

# 构造用于训练的数据集
x_train = np.array([[2, 4], [5, 8], [5, 9], [7, 10], [9, 12]])
y_train = np.array([20, 50, 30, 70, 60])

# 训练模型并输出模型系数和训练结果
model.fit(x_train, y_train)
# fit(x,y,sample_weight=None)x:训练集 y:目标值 sample_weight:每个样本的个数
# coef_ 系数w,intercept_截距
print(model.coef_)  # 输出系数w
print(model.intercept_)  # 输出截距b
print(model.score(x_train, y_train))  # 输出模型的评估分数R2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值