帝王谷资源网 Design By www.wdxyy.com

我就废话不多说啦,直接上代码吧!

target = [1.5, 2.1, 3.3, -4.7, -2.3, 0.75]
prediction = [0.5, 1.5, 2.1, -2.2, 0.1, -0.5]
 
 
error = []
for i in range(len(target)):
 error.append(target[i] - prediction[i])
 
print("Errors: ", error)
print(error)
 
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)
 
 
print("MSE = ", sum(squaredError) / len(squaredError))#均方误差MSE
 
 
from math import sqrt
print("RMSE = ", sqrt(sum(squaredError) / len(squaredError)))#均方根误差RMSE
print("MAE = ", sum(absError) / len(absError))#平均绝对误差MAE
 
 
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)))#标准差

补充拓展:回归模型指标:MSE 、 RMSE、 MAE、R2

sklearn调用

# 测试集标签预测
y_predict = lin_reg.predict(X_test)

# 衡量线性回归的MSE 、 RMSE、 MAE、r2
from math import sqrt
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
print("mean_absolute_error:", mean_absolute_error(y_test, y_predict))
print("mean_squared_error:", mean_squared_error(y_test, y_predict))
print("rmse:", sqrt(mean_squared_error(y_test, y_predict)))
print("r2 score:", r2_score(y_test, y_predict))

原生实现

# 测试集标签预测
y_predict = lin_reg.predict(X_test)
# 衡量线性回归的MSE 、 RMSE、 MAE
mse = np.sum((y_test - y_predict) ** 2) / len(y_test)
rmse = sqrt(mse)
mae = np.sum(np.absolute(y_test - y_predict)) / len(y_test)
r2 = 1-mse/ np.var(y_test)
print("mse:",mse," rmse:",rmse," mae:",mae," r2:",r2)

相关公式

MSE

python之MSE、MAE、RMSE的使用

RMSE

python之MSE、MAE、RMSE的使用

MAE

python之MSE、MAE、RMSE的使用

R2

python之MSE、MAE、RMSE的使用

以上这篇python之MSE、MAE、RMSE的使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,MSE,MAE,RMSE

帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com