[Python] scikit-learn之mean_squared_error函数(Mean Squared Error(MSE))介绍和使用案例

本文介绍了均方误差(MSE)和均方根误差(RMSE)作为回归模型性能评估指标的概念,强调了它们在预测精度中的作用,以及如何使用scikit-learn库中的mean_squared_error函数进行计算,并给出了两个使用案例。
摘要由CSDN通过智能技术生成

什么是均方误差(MSE)和均方根误差(RMSE)?

MSE 是均方误差(Mean Squared Error)的缩写,是一种常用的衡量回归模型预测精度的指标。它表示预测值与真实值之间差异的平方和的平均值,通常用于评估回归模型的性能。

MSE = \frac{1}{n}\sum_{i=1}^{n}(y_i - y_p)^2

RMSE 是均方根误差(Root Mean Squared Error)的缩写,是一种常用的衡量回归模型预测精度的指标。它表示预测值与真实值之间差异的平均大小,通常用于评估回归模型的性能。

RMSE = \sqrt{\frac{1}{n}\sum_{i=1}^{n}(y_i - y_p)^2}
其中,y_i 是第 i 个样本的真实值,y_p 是模型对第 i 个样本的预测值,n 是样本数量。

MSE,RMSE 越小,说明模型的预测精度越高。但是需要注意的是,MSE,RMSE 受到异常值的影响较大,因此在实际应用中需要结合其他指标(比如最大误差max_error)进行综合评估。

scikit-learn库mean_squared_error函数介绍

https://scikit-learn.org/stable/modules/model_evaluation.html#mean-squared-error

sklearn.metrics.mean_squared_error — scikit-learn 1.3.2 documentation

mean_squared_error函数使用案例

案例1

from sklearn.metrics import mean_squared_error
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
print(mean_squared_error(y_true, y_pred))

y_true = [[0.5, 1], [-1, 1], [7, -6]]
y_pred = [[0, 2], [-1, 2], [8, -5]]
print(mean_squared_error(y_true, y_pred))

输出:

0.375
0.7083333333333334

案例2

from sklearn.metrics import mean_squared_error
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
print(mean_squared_error(y_true, y_pred))
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
print(mean_squared_error(y_true, y_pred, squared=False))
y_true = [[0.5, 1],[-1, 1],[7, -6]]
y_pred = [[0, 2],[-1, 2],[8, -5]]
print(mean_squared_error(y_true, y_pred))
print(mean_squared_error(y_true, y_pred, squared=False))
print(mean_squared_error(y_true, y_pred, multioutput='raw_values'))
print(mean_squared_error(y_true, y_pred, multioutput=[0.3, 0.7]))

输出:

0.375
0.6123724356957945
0.7083333333333334
0.8227486121839513
[0.41666667 1.        ]
0.825
  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老狼IT工作室

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值