python3学习使用api
线性回归,和 随机参数回归
git: https://github.com/linyi0604/MachineLearning
1 from sklearn.datasets import load_boston 2 from sklearn.cross_validation import train_test_split 3 from sklearn.preprocessing import StandardScaler 4 from sklearn.linear_model import LinearRegression, SGDRegressor 5 from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error 6 import numpy as np 7 8 # 1 准备数据 9 # 读取波士顿地区房价信息 10 boston = load_boston() 11 # 查看数据描述 12 # print(boston.DESCR) # 共506条波士顿地区房价信息,每条13项数值特征描述和目标房价 13 # 查看数据的差异情况 14 # print("最大房价:", np.max(boston.target)) # 50 15 # print("最小房价:",np.min(boston.target)) # 5 16 # print("平均房价:", np.mean(boston.target)) # 22.532806324110677 17 18 x = boston.data 19 y = boston.target 20 21 # 2 分割训练数据和测试数据 22 # 随机采样25%作为测试 75%作为训练 23 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33) 24 25 26 # 3 训练数据和测试数据进行标准化处理 27 ss_x = StandardScaler() 28 x_train = ss_x.fit_transform(x_train) 29 x_test = ss_x.transform(x_test) 30 31 ss_y = StandardScaler() 32 y_train = ss_y.fit_transform(y_train.reshape(-1, 1)) 33 y_test = ss_y.transform(y_test.reshape(-1, 1)) 34 35 # 4 使用两种线性回归模型进行训练和预测 36 # 初始化LinearRegression线性回归模型 37 lr = LinearRegression() 38 # 训练 39 lr.fit(x_train, y_train) 40 # 预测 保存预测结果 41 lr_y_predict = lr.predict(x_test) 42 43 # 初始化SGDRRegressor随机梯度回归模型 44 sgdr = SGDRegressor() 45 # 训练 46 sgdr.fit(x_train, y_train) 47 # 预测 保存预测结果 48 sgdr_y_predict = sgdr.predict(x_test) 49 50 # 5 模型评估 51 # 对Linear模型评估 52 lr_score = lr.score(x_test, y_test) 53 print("Linear的默认评估值为:", lr_score) 54 lr_R_squared = r2_score(y_test, lr_y_predict) 55 print("Linear的R_squared值为:", lr_R_squared) 56 lr_mse = mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict)) 57 print("Linear的均方误差为:", lr_mse) 58 lr_mae = mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict)) 59 print("Linear的平均绝对误差为:", lr_mae) 60 61 # 对SGD模型评估 62 sgdr_score = sgdr.score(x_test, y_test) 63 print("SGD的默认评估值为:", sgdr_score) 64 sgdr_R_squared = r2_score(y_test, sgdr_y_predict) 65 print("SGD的R_squared值为:", sgdr_R_squared) 66 sgdr_mse = mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_predict)) 67 print("SGD的均方误差为:", sgdr_mse) 68 sgdr_mae = mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_predict)) 69 print("SGD的平均绝对误差为:", sgdr_mae) 70 71 ‘‘‘ 72 Linear的默认评估值为: 0.6763403830998702 73 Linear的R_squared值为: 0.6763403830998701 74 Linear的均方误差为: 25.09698569206773 75 Linear的平均绝对误差为: 3.5261239963985433 76 77 SGD的默认评估值为: 0.659795654161198 78 SGD的R_squared值为: 0.659795654161198 79 SGD的均方误差为: 26.379885392159494 80 SGD的平均绝对误差为: 3.5094445431026413 81 ‘‘‘
原文地址:https://www.cnblogs.com/Lin-Yi/p/8971798.html
时间: 2024-11-12 07:06:44