MSE、RMSE、MAER、R_Square可直接调用sklearn(scikit-learn的简称)进行计算 代码如下:
import numpy as np
y_predict = np.array([35,8,45,8,47,8,61,62])
y_output = np.array([18,32,55,32,40,39,33,66])
from sklearn import metrics
from sklearn.metrics import r2_score
MSE = metrics.mean_squared_error(y_predict, y_output)
RMSE = metrics.mean_squared_error(y_predict, y_output)**0.5
MAE = metrics.mean_absolute_error(y_predict, y_output)
R_Squared = r2_score(y_predict, y_output)
print("mse:", MSE)
print("rmse:", RMSE)
print("mae:", MAE)
print("r2:", R_Squared)
MAPE、SMAPE不能直接调用,需要自己定义函数进行计算 代码如下:
def mape(y_predict, y_output):
return np.mean(np.abs((y_predict-y_output)/y_output))*100
def smape(y_predict, y_output):
return 2 * np.mean(np.abs(y_predict-y_output)/(np.abs(y_predict)+np.abs(y_output)))*100
print("mape:", mape(y_predict, y_output))
print("smape:", smape(y_predict, y_output))
|