一:三种方式
a.均方误差(MSE)
b.均方根误差(RMSE)
c.平均绝对误差(MAE)
二:评测公式
均方误差:对(y_test-y_test_predict)的平方求和,但是为了排除个数对数值的影响,我们将上述的值在进行除以y_test的大小。
均方根误差:对均方误差进行开根号。
平均绝对误差:对(y_test-y_test_predict)取绝对值,在求和,同样为了排除个数对数值的影响,我们将上述的值在除以y_test的大小。
三:代码测试
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_selection import train_test_split from Mine_Simple_Linear_Regression import Simple_Linear_Regression from math import sqrt from sklearn.metrics import mean_squared_error from sklearn.metrics import mean_absolute_error boston = datasets.load_boston() x = boston.data[:,5] y = boston.target x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=666) x_train = x_train[y_train<50] y_train = y_train[y_train<50] plt.scatter(x_train,y_train) plt.show() slr = Simple_Linear_Regression() slr.fit(x_train,y_train) print(slr.a_) print(slr.b_) y_train_hat = slr.predict(x_train) plt.scatter(x_train,y_train) plt.plot(x_train,y_train_hat,color='r') plt.show() y_test_predict = slr.predict(x_test) #均方误差 mse = np.sum((y_test_predict-y_test)**2)/len(y_test) print('均方误差',mse) #均方根误差 rmse = sqrt(np.sum((y_test_predict-y_test)**2)/len(y_test)) print('均方根误差',rmse) #平均绝对误差 mae = np.sum(np.absolute(y_test_predict-y_test))/len(y_test) print('平均绝对误差',mae) #sklearn中自带的对回归算法评测的函数 mse1 = mean_squared_error(y_test,y_test_predict) print('均方误差',mse1) rmse1 = sqrt(mse1) print('均方根误差',rmse1) mae1 = mean_absolute_error(y_test_predict,y_test) print('平均绝对误差',mae1)
四:很重要的一个评测函数,R_squared = 1 - MSE/y_test的方差
from sklearn.metrics import r2_score r_squared = 1 - np.sum((y_test-y_test_predict)**2)/len(y_test)/np.var(y_test) print(r_squared) print(1-mean_squared_error(y_test,y_test_predict)/np.var(y_test)) #sklearn中自带的R_squared函数 r2 = r2_score(y_test,y_test_predict) print(r2)