赞
踩
在机器学习中,MSE和MAE是常用的评价回归模型的指标。
MSE(Mean Squared Error)均方误差,预测值与真实值之差的平方和的平均值:
MAE(Mean Absolute Error) 平均绝对误差:
目标变量
target = [ -2.2, 0.1, -0.5,0.5, 1.5, 2.1]
预测结果
prediction = [ -4.7, -2.3, 0.75,1.5, 2.1, 3.3]
计算误差error
error = []
for i in range(len(target)):
error.append(target[i] - prediction[i])
结果:
[2.5, 2.4, -1.25, -1.0, -0.6,-1.19]
计算误差squaredError和absError
squaredError = []
absError = []
for val in error:
squaredError.append(val * val)#target-prediction之差平方
absError.append(abs(val))#误差绝对值
print("Square Error[误差平方]): ", squaredError)
print("Absolute Value of Error[误差绝对值]: ", absError)
结果:
Square Error[误差平方]:[6.25, 5.76, 1.5625, 1.0, 0.36, 1.4399]
Absolute Value of Error[误差绝对值]: [2.5, 2.4, 1.25, 1.0, 0.6, 1.199]
计算误差均方误差MSE
print( sum(squaredError))
print( len(squaredError))
print("MSE = ", sum(squaredError) / len(squaredError)) #均方误差MSE
结果:
sum(squaredError):16.3725
len(squaredError):6
MSE = 2.72875
计算误差平均绝对误差MAE
from math import sqrt
print("RMSE = ", sqrt(sum(squaredError) / len(squaredError)))#均方根误差RMSE
print("MAE = ", sum(absError) / len(absError))#平均绝对误差MAE
结果:
RMSE = 1.65
MAE = 1.49
计算误差方差和标准差
方差,是样本实际值与实际值的总体平均值之差的平方和的平均值,描述数据集的离散程度。
方差:
标准差:
targetDeviation = []
targetMean = sum(target) / len(target) # target平均值
for val in target:
targetDeviation.append((val - targetMean) * (val - targetMean))
print("Target Variance = ", sum(targetDeviation) / len(targetDeviation)) # 方差
print("Target Standard Deviation = ", sqrt(sum(targetDeviation) / len(targetDeviation))) # 标准差
结果:
Target Variance[方差] = 1.939
Target Standard Deviation[标准差] = 1.39
补充:平均相对误差
https://www.jianshu.com/p/705fdb11ccd5
https://blog.csdn.net/reallocing1/article/details/56292877
参考:[https://blog.csdn.net/stone_fall/article/details/89389269]
[https://blog.csdn.net/llx1026/article/details/77752121?yyue=a21bo.50862.201879]
[https://blog.csdn.net/xiongchengluo1129/article/details/79155550]
http://latex.codecogs.com/eqneditor/editor.php
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。