代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 导入数据
from sklearn.datasets import load_boston
data = load_boston()
x = data['data']
y = data['target']

# 划分训练集和测试集
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(x,y,test_size = 0.2,random = ...) # 如果想固定划分结果,用种子

X_train.shape # 查看情况,X_test,y_train,y_test应一一对应

# 调用线性模型
from sklearn.linear_model import LinearRegression

# 使用训练集上的x与y
clf = LinearRegression()
clf.fit(X_train,y_train)
clf.coef_ # 查看系数

# 查看该模型在训练集和测试集上的表现情况
y_train_pred = clf.predict(X_train)
y_test_pred = clf.predict(X_test)
# 查看MSE
from sklearn.metrics import mean_squared_error
mean_squared_error(y_train,y_train_pred) # 训练集
mean_squared_error(y_test,y_test_pred) # 测试集
# 查看R²
from sklearn.metrics import r2_score
r2_score(y_train,y_train_pred) # 训练集
r2_score(y_test,y_test_pred) # 测试集
# 查看绝对值
from sklearn.metrics import mean_absolute_error
mean_absolute_error(y_train,y_train_pred) # 训练集
mean_absolute_error(y_test,y_test_pred) # 测试集