1. 程式人生 > 實用技巧 >MSE(均方誤差)、RMSE (均方根誤差)、MAE (平均絕對誤差)

MSE(均方誤差)、RMSE (均方根誤差)、MAE (平均絕對誤差)

MSE(均方誤差)、RMSE (均方根誤差)、MAE (平均絕對誤差)

1、MSE(均方誤差)(Mean Square Error)

MSE是真實值與預測值的差值的平方然後求和平均。

範圍[0,+∞),當預測值與真實值完全相同時為0,誤差越大,該值越大。

import numpy as np
from sklearn import metrics
y_true = np.array([1.0, 5.0, 4.0, 3.0, 2.0, 5.0, -3.0])
y_pred = np.array([1.0, 4.5, 3.5, 5.0, 8.0, 4.5, 1.0])
print(metrics.mean_squared_error(y_true, y_pred)) # 8.107142857142858

2、

RMSE (均方根誤差)(Root Mean Square Error)

import numpy as np
from sklearn import metrics
y_true = np.array([1.0, 5.0, 4.0, 3.0, 2.0, 5.0, -3.0])
y_pred = np.array([1.0, 4.5, 3.5, 5.0, 8.0, 4.5, 1.0])
print(np.sqrt(metrics.mean_squared_error(y_true, y_pred)))

3、MAE (平均絕對誤差)(Mean Absolute Error)

import numpy as np
from sklearn import metrics
y_true = np.array([1.0, 5.0, 4.0, 3.0, 2.0, 5.0, -3.0])
y_pred = np.array([1.0, 4.5, 3.5, 5.0, 8.0, 4.5, 1.0])
print(metrics.mean_absolute_error(y_true, y_pred))