代码:
# 使用sklearn内置的波士顿房价数据集,load_boston是加载数据集的函数
from sklearn.datasets import load_boston
# 使用sklearn中的train_test_split划分数据集
from sklearn.model_selection import train_test_split
# 使用sklearn中的线性回归模型进行预测
from sklearn.linear_model import LinearRegression
# 使用matplotlib中的pyplot进行可视化
import matplotlib.pylab as plt
# 加载波士顿房价数据集,返回特征X和标签y
X, y = load_boston(return_X_y=True)
# 只取第6列特征(方便可视化):住宅平均房间数
X = X[:, 5:6]
# 划分为训练集和测试集,测试集取20%
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2020)
# 创建线性回归对象
regr = LinearRegression()
# 使用训练集训练模型
regr.fit(X_train, y_train)
# 在测试集上进行预测
y_pred = regr.predict(X_test)
# 画测试数据散点图
plt.scatter(X_test, y_test, color='blue')
# 画线性回归模型对测试数据的拟合曲线
plt.plot(X_test, y_pred, color='red')
# 显示绘图结果
plt.show()
# 打印斜率和截距
print('斜率: {}, 截距: {}'.format(regr.coef_, regr.intercept_))
运行结果:
斜率: [9.11163398], 截距: -34.47557789280662