均方误差 MSE Mean Squared Error
均方根误差 RMSE Root Mean Squared Error
平均绝对误差 MAE Mean Absolute Error
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
boston = datasets.load_boston()
x = boston.data[:, 5] # 5代码房间数,保使用房间数量这个特征
y = boston.target
plt.scatter(x, y)
plt.show()
x = x[y < 50.0]
y = y[y < 50.0]
plt.scatter(x, y)
plt.show()
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_state=666)
reg = SimpleLinearRegression() #见5-4
reg.fit(x_train, y_train)
plt.scatter(x_train, y_train)
plt.plot(x_train, reg.predict(x_train), color='r')
plt.show()
y_predict = reg.predict(x_test)
mse_test = np.sum((y_predict - y_test) ** 2) / len(y_test)
from math import sqrt
rmse_test = sqrt(mse_test)
mae_test = np.sum(np.absolute(y_predict-y_test)) / len(y_test)
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
mean_squared_error(y_test, y_predict)
mean_absolute_error(y_test, y_predict)