# 1.5 创建岭回归
import sys
import numpy as np
from sklearn import linear_model
import sklearn.metrics as sm
filename = sys.argv[1]
X = []
y = []with open(filename, 'r') as f:
for line in f.readlines():
xt, yt = [float(i) for i in line.split(',')]
X.append(xt)
y.append(yt)
## 读取训练数据~~~
num_training = int(0.8 * len(X))
num_test = len(X) - num_training# 训练数据
X_train = np.array(X[:num_training]).reshape((num_training,1 ))
y_train = np.array(y[:num_training])
# 测试数据
X_test = np.array(X[num_training:]).reshape((num_test,1 ))
y_test = np.array(y[num_training:])
# ~~~1回归器 进行运算
# 创建岭回归器就是用普通最小二乘法的线性回归器
ridge_regressor = linear_model.Ridge(alpha=0.01,fit_intercept=True,max_iter=10000)
# 训练数据
ridge_regressor.fit(X_train, y_train)
y_test_pred_ridge = ridge_regressor.predict(X_test)##~~~ 4 训练的展示
import matplotlib.pyplot as plt
y_train_pred = ridge_regressor.predict(X_train)
plt.figure()
plt.scatter(X_train, y_train, color='green')
plt.plot(X_train, y_train_pred, color='black', linewidth=4)
plt.title('Training data')
plt.show()
# ~~ 5 预测-画出预测图
y_test_pred_ridge = ridge_regressor.predict(X_test)
plt.scatter(X_test, y_test, color='green')
plt.plot(X_test, y_test_pred_ridge, color='black', linewidth=4)
plt.title('Test data')
plt.show()print("Mean absolute error =", round(sm.mean_absolute_error(y_test, y_test_pred_ridge), 2))
print("Mean squared error =", round(sm.mean_squared_error(y_test, y_test_pred_ridge), 2))
print("Median absolute error =", round(sm.median_absolute_error(y_test, y_test_pred_ridge), 2))
print("Explain variance score =", round(sm.explained_variance_score(y_test, y_test_pred_ridge),2))
print("R2 score =", round(sm.r2_score(y_test, y_test_pred_ridge), 2))
结果: