常用回归模型评估指标的python计算逻辑。python和相关package版本:
import sys
import sklearn
import pandas as pd
import numpy as np
import math
print("Python版本:",sys.version)
print("sklearn版本:",sklearn.__version__)
print("pandas:",pd.__version__)
print("numpy:",np.__version__)
Python版本: 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)]
sklearn版本: 0.22
pandas: 0.23.4
numpy: 1.17.4
from sklearn.metrics import mean_absolute_error
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
mean_absolute_error(y_true, y_pred)
0.5
sum([abs(y_true[i]-y_pred[i]) for i in range(len(y_true))])/len(y_true)
0.5
from sklearn.metrics import mean_squared_error
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
mean_squared_error(y_true, y_pred)
0.375
sum([abs(y_true[i]-y_pred[i])**2 for i in range(len(y_true))])/len(y_true)
0.375
这个没啥说的,mse开方就行
math.sqrt(mean_squared_error(y_true, y_pred))
0.6123724356957945
from sklearn.metrics import r2_score
r2_score(y_true, y_pred)
0.9486081370449679
1 - sum([(y_true[i] - y_pred[i])**2 for i in range(len(y_true))]) / sum(
[(y_true[i] - sum(y_true) / len(y_true))**2 for i in range(len(y_true))])
0.9486081370449679
mape由于可能存在分母为0的情况,因此暂时没有统计。把其他指标整合一下,格式化输出:
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
def reg_metric(y_true, y_predict):
mae = mean_absolute_error(y_true, y_pred)
mse = mean_squared_error(y_true, y_pred)
rmse = math.sqrt(mean_squared_error(y_true, y_pred))
r2 = r2_score(y_true, y_pred)
print("MAE:",mae)
print("MSE:",mse)
print("RMSE:",rmse)
print("R Square:",r2)
reg_metric(y_true=y_true, y_predict=y_pred)
MAE: 0.5
MSE: 0.375
RMSE: 0.6123724356957945
R Square: 0.9486081370449679
2020-06-16、连绵梅雨季 于南京市江宁区九龙湖