1. 程式人生 > >Bobo老師機器學習筆記第五課-線性迴歸演算法的評估指標

Bobo老師機器學習筆記第五課-線性迴歸演算法的評估指標

評價線性迴歸的指標有四種,均方誤差(Mean Squared Error)、均方根誤差(Root Mean Squared Error)、平均絕對值誤差(Mean Absolute Error)以及R Squared方法。 sklearnz中使用的,也是大家推薦的方法是R Squared方法。

1、均方誤差 MSE

MSE的值越小,說明預測模型描述實驗資料具有更好的精確度 

2、均方根誤差 RMSE

3、平均絕對值誤差 MAE

4、 R平方 R2S 

四種程式碼實現:

def mean_squared_error(y_true, y_predict):
    """計算y_true和y_predict之間的MSE"""
    assert len(y_true) == len(y_predict), \
        "the size of y_true must be equal to the size of y_predict"

    return np.sum((y_true - y_predict)**2) / len(y_true)


def root_mean_squared_error(y_true, y_predict):
    """計算y_true和y_predict之間的RMSE"""

    return sqrt(mean_squared_error(y_true, y_predict))


def mean_absolute_error(y_true, y_predict):
    """計算y_true和y_predict之間的RMSE"""
    assert len(y_true) == len(y_predict), \
        "the size of y_true must be equal to the size of y_predict"

    return np.sum(np.absolute(y_true - y_predict)) / len(y_true)


def r2_score(y_true, y_predict):
    """計算y_true和y_predict之間的R Square"""

    return 1 - mean_squared_error(y_true, y_predict)/np.var(y_true)
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from model_selection import train_test_split
from simplelinerregression import SimpleLineRegession2
from metrics import *

boston = load_boston()

x = boston.data[:, 5]
y = boston.target

x = x[y < 50.0]
y = y[y < 50.0]

x_train, x_test, y_train, y_test = train_test_split(x, y)
reg = SimpleLineRegession2()
reg.fit(x_train, y_train)

print 'MAE:',  mean_absoulte_error(y_test, reg.predict(x_test))
print 'RMSE:', root_mean_squared_error(y_test, reg.predict(x_test))
print 'MSE:', mean_squared_error(y_test, reg.predict(x_test))
print 'RSE:', r2_score(y_test, reg.predict(x_test))

plt.scatter(x, y)
plt.plot(x, reg.predict(x), color="red")
plt.show()

以上用波士頓房價資料,其中選擇一個維度進行的分析:

不同指標的分數:

MAE: 4.341835956360974
RMSE: 6.2500402273
MSE: 39.063002842901284
RSE: 0.47025653581866034

參考文章:

https://blog.csdn.net/qq_37279279/article/details/81041470?utm_source=blogxgwz1

要是你在西安,感興趣一起學習AIOPS,歡迎加入QQ群 860794445