# 线性回归解决波士顿房价预测问题
# 一、数据集
波士顿房价预测也是个经典的数据集,利用sklearn.datasets 的数据集直接进行训练
数据集获取以及相关库导入
| import numpy as np |
| import matplotlib.pyplot as plt |
| from sklearn.model_selection import train_test_split |
| import sklearn.datasets as datasets |
| from sklearn.linear_model import Lasso |
| from sklearn.linear_model import Ridge |
| from sklearn.linear_model import LinearRegression |
| def load_dataset(): |
| boston = datasets.load_boston() |
| feature_name = boston['feature_names'] |
| train = boston.data |
| target = boston.target |
| X_train,X_test,Y_train,Y_test = train_test_split(train,target,test_size=0.3,random_state=666) |
| return X_train,X_test,Y_train,Y_test |
| |
# 二、Lasso 回归预测
# 1、利用 sklearn 库进行预测
| def Lasso_train(X_train,Y_train): |
| lasso = Lasso() |
| lasso.fit(X_train,Y_train) |
| predict = lasso.predict(X_train) |
| return lasso,predict |
| def Lasso_draw(Y_test,Y_predict): |
| plt.plot(Y_test,label='Y_True') |
| plt.plot(Y_predict,'r:',label='Y_Predict') |
| plt.legend() |
| plt.show() |
| |
| X_train,X_test,Y_train,Y_test = load_dataset() |
| lasso,lasso_predict = Lasso_train(X_train,Y_train) |
| print('w=',lasso.coef_) |
| print('b=',lasson.intercept_) |
| print("训练集得分:{:.2f}".format(lasso.score(X_train, Y_train))) |
| print("测试集得分:{:.2f}".format(lasso.score(X_test, Y_test))) |
| Lasso_draw() |
# 2、输出图片如下:
![输出图片]()
# 三、Ridge 回归预测
# 1、利用 sklearn 库进行检测
| def Ridge_train(X_train,Y_train): |
| ridge = Ridge(alpha=1,max_iter=1000) |
| |
| |
| ridge.fit(X_train,Y_train) |
| predict = lasso.predict(X_train) |
| return ridge,predict |
| def Ridge_draw(Y_test,Y_predict): |
| plt.plot(Y_test,label='Y_True') |
| plt.plot(Y_predict,'r:',label='Y_Predict') |
| plt.legend() |
| plt.show() |
| |
| X_train,X_test,Y_train,Y_test = load_dataset() |
| Ridge,Ridge_predict = Ridge_train(X_train,Y_train) |
| print('w=',Ridge.coef_) |
| print('b=',Ridge.intercept_) |
| print("训练集得分:{:.2f}".format(Ridge.score(X_train, Y_train))) |
| print("测试集得分:{:.2f}".format(Ridge.score(X_test, Y_test))) |
| Ridge_draw() |
# 2、输出图片如下:
![输出图片]()
# 四、Linear_Regression 多元回归预测
# 1、利用 sklearn 库进行检测
| def Linear_train(X_train,Y_train): |
| linear = LinearRegression() |
| |
| |
| linear.fit(X_train,Y_train) |
| predict = linear.predict(X_train) |
| return linear,predict |
| def Linear_draw(Y_test,Y_predict): |
| plt.plot(Y_test,label='Y_True') |
| plt.plot(Y_predict,'r:',label='Y_Predict') |
| plt.legend() |
| plt.show() |
| |
| X_train,X_test,Y_train,Y_test = load_dataset() |
| linear,linear_predict = Linear_train(X_train,Y_train) |
| print('w=',linear.coef_) |
| print('b=',linear.intercept_) |
| print("训练集得分:{:.2f}".format(linear.score(X_train, Y_train))) |
| print("测试集得分:{:.2f}".format(linear.score(X_test, Y_test))) |
| Linear_draw() |
# 2、输出图片如下:
![输出图片]()