1. 导入boston房价数据集
from sklearn.datasets import load_boston import pandas as pd boston = load_boston() df = pd.DataFrame(boston.data)
2. 一元线性回归模型,建立一个变量与房价之间的预测模型,并图形化显示。
from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt x = boston.data[:, 5] y = boston.target LinR = LinearRegression() LinR.fit(x.reshape(-1, 1), y)
plt.figure(figsize
=
(
10
,
6
))
plt.scatter(x, y)
w
=
LinR.coef_
b
=
LinR.intercept_
print
(w, b)
plt.plot(x, w
*
x
+
b,
'orange'
)
plt.show()
3. 多元线性回归模型,建立13个变量与房价之间的预测模型,并检测模型好坏,并图形化显示检查结果。
from sklearn.datasets import load_boston import pandas as pd from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split boston = load_boston() df = pd.DataFrame(boston.data) x_train, x_test, y_train, y_test = train_test_split(boston.data,boston.target,test_size=0.3) lineR = LinearRegression() lineR.fit(x_train,y_train) import numpy as np x_predict = lineR.predict(x_test) # 打印预测的均方误差 print("预测的均方误差:", np.mean(x_predict - y_test)**2) # 打印模型的分数 print("模型的分数:",lineR.score(x_test, y_test)) x = boston.data[:,12].reshape(-1,1) y = boston.target plt.figure(figsize=(10,6)) plt.scatter(x,y) lineR = LinearRegression() lineR.fit(x,y) y_pred = lineR.predict(x) plt.plot(x,y_pred,'r') print(lineR.coef_,lineR.intercept_) plt.show()
4. 一元多项式回归模型,建立一个变量与房价之间的预测模型,并图形化显示。
from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree=3) x_poly = poly.fit_transform(x) print(x_poly) lrp = LinearRegression() lrp.fit(x_poly,y) y_poly_pred = lrp.predict(x_poly) plt.scatter(x,y) plt.scatter(x,y_pred) plt.scatter(x,y_poly_pred) plt.show()