金融信创如何推动金融服务效率与安全的全面提升
792
2022-09-02
回归评估指标-Python实现
常用回归模型评估指标的python计算逻辑。python和相关package版本:
import sysimport sklearnimport pandas as pdimport numpy as npimport 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.22pandas: 0.23.4numpy: 1.17.4
MAE
from sklearn.metrics import mean_absolute_errory_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
MSE
from sklearn.metrics import mean_squared_errory_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
RMSE
这个没啥说的,mse开方就行
math.sqrt(mean_squared_error(y_true, y_pred))
0.6123724356957945
R方
from sklearn.metrics import r2_scorer2_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_errorfrom sklearn.metrics import mean_squared_errorfrom sklearn.metrics import r2_scoredef 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.5MSE: 0.375RMSE: 0.6123724356957945R Square: 0.9486081370449679
2020-06-16、连绵梅雨季 于南京市江宁区九龙湖
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~