生成数据:
#生成数据
import numpy as np
#生成随机数
np.random.seed(1234)
x = np.random.rand(500,3)
#构建映射关系,模拟真实的数据待预测值,映射关系为y = 4.2 + 5.7*x1 + 10.8*x2,可自行设置值进行尝试
y = x.dot(np.array([4.2,5.7,10.8]))
1、调用sklearn的线性回归模型训练数据:
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
%matplotlib inline
# 调用模型
lr = LinearRegression(fit_intercept=True)
# 训练模型
lr.fit(x,y)
print("估计的参数值为:%s" %(lr.coef_))
# 计算R平方
print('R2:%s' %(lr.score(x,y)))
# 任意设定变量,预测目标值
x_test = np.array([2,4,5]).reshape(1,-1)
y_hat = lr.predict(x_test)
print("预测值为: %s" %(y_hat))
估计的参数值为:[ 4.2 5.7 10.8]
R2:1.0
预测值为: [85.2]
2、最小二乘法的矩阵求解:
class LR_LS():
def __init__(self):
self.w = None
def fit(self, X, y):
# 最小二乘法矩阵求解
temp0 = np.dot(X.T,X)
temp = np.dot(np.linalg.inv(tempo),X.T)
self.w = np.dot(temp,y)
return self.w
def predict(self, X):
# 用已经拟合的参数值预测新自变量
y_pred = np.dot(X,self.w)
return y_pred
if __name__ == "__main__":
lr_ls = LR_LS()
lr_ls.fit(x,y)
print("估计的参数值:%s" %(lr_ls.w))
x_test = np.array([2,4,5]).reshape(1,-1)
print("预测值为: %s" %(lr_ls.predict(x_test)))
估计的参数值:[ 4.2 5.7 10.8]
预测值为: [85.2]
3、梯度下降法:
class LR_GD():
def __init__(self):
self.w = None
def fit(self,X,y,alpha=0.02,loss = 1e-10): # 设定步长为0.002,判断是否收敛的条件为1e-10
y = y.reshape(-1,1) #重塑y值的维度以便矩阵运算
[m,d] = np.shape(X) #自变量的维度
self.w = np.zeros((d)).reshape(d,1)#将参数的初始值定为0
tol = 1e5
while tol > loss:
temp = X.dot(self.w)-y
self.w = self.w-1/m*alpha*(temp.dot(X))
tol = np.abs(np.sum(temp))
def predict(self, X):
# 用已经拟合的参数值预测新自变量
y_pred = X.dot(self.w)
return y_pred
if __name__ == "__main__":
lr_gd = LR_GD()
lr_gd.fit(x,y)
print("估计的参数值为:%s" %(lr_gd.w))
x_test = np.array([2,4,5]).reshape(1,-1)
print("预测值为:%s" %(lr_gd.predict(x_test)))
估计的参数值为:[ 4.20000001 5.70000003 10.79999997]
预测值为:[85.19999995]
学习资料来源:https://github.com/datawhalechina/team-learning/blob/master/%E5%88%9D%E7%BA%A7%E7%AE%97%E6%B3%95%E6%A2%B3%E7%90%86/Task2_Linear_regression.ipynb
来源:CSDN
作者:大丸子哟
链接:https://blog.csdn.net/Dawanz/article/details/103940972