- 模型建立:线性回归原理、线性回归模型todo
- 学习策略:线性回归损失函数、代价函数、目标函数todo
- 算法求解:梯度下降法、牛顿法、拟牛顿法等todo
- 线性回归的评估指标todo
- sklearn参数详解todo
- 案例:
-
import numpy as np from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt #from sklearn.linear_model import ElasticNet #from sklearn.model_selection import train_test_split #生成随机数 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])) print('X:',x.shape) print('Y:',y.shape) # # from sklearn.cross_decomposition import train_test_split # X_train,X_test,y_train,y_test=train_test_split(x,y,train_size=0.7) # # from sklearn import preprocessing # standard_X=preprocessing.StandardScaler() # X_train=standard_X.fit_transform(X_train) # X_test=standard_X.transform(X_test) # # standard_y=preprocessing.StandardScaler() # y_train=standard_y.fit_transform(y_train,reshape(-1,1)) # y_test=standard_y.transform(y_test.reshape(-1,1)) # # # # # # ElasticNet_clf =ElasticNet(alpha=0.1,l1_ratio=0.71) # ElasticNet_clf.fit(X_train,y_train.ravel()) # ElasticNet_clf_score=ElasticNet_clf.score(X_test,y_test.ravel()) #print('模型得分',ElasticNet_clf_score) #print('权重',ElasticNet_clf.coef_) #print('偏置',ElasticNet_clf.intercept_) #print('迭代',ElasticNet_clf.n_iter_) print('_______________________________________') #1、先尝试调用sklearn的线性回归模型训练数据 # 调用模型 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)) #2、最小二乘法的矩阵求解 class LR_LS(): def __init__(self): self.w = None def fit(self, X, y): # 最小二乘法矩阵求解 #============================= show me your code ======================= self.w =np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y) #============================= show me your code ======================= def predict(self, X): # 用已经拟合的参数值预测新自变量 #============================= show me your code ======================= y_pred = X.dot(self.w) #============================= show me your code ======================= 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))) #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)) # 将参数的初始值定为0 tol = 1e5 # ============================= show me your code ======================= while tol > loss: h_f = X.dot(self.w).reshape(-1,1) theta = self.w + alpha*np.mean(X*(y - h_f),axis=0) #计算迭代的参数值 tol = np.sum(np.abs(theta - self.w)) self.w = theta # here # ============================= show me your code ======================= 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)))
- 结果:
-
X: (500, 3) Y: (500,) _______________________________________ 估计的参数值为:[ 4.2 5.7 10.8] R2:1.0 预测值为: [85.2] 估计的参数值:[ 4.2 5.7 10.8] 预测值为: [85.2] 估计的参数值为:[ 4.20000001 5.70000003 10.79999997] 预测值为:[85.19999995]
Task02:线性回归
最新推荐文章于 2024-07-21 22:38:55 发布