## python中的均方误差内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (1522)

```def rmse(predictions, targets):
return np.sqrt(((predictions - targets) ** 2).mean())```

## 什么是RMSE？也称为MSE或RMS。它解决了什么问题？

RMSE回答了这个问题：“列表1到列表2中的数字平均有多相似？”。这两个列表必须大小相同。我想“洗掉任何两个给定元素之间的噪音，洗掉收集到的数据的大小，并得到一个数字随着时间的推移而变化的感觉”。

## 在python中计算根均方误差的示例：

```import numpy as np
d = [0.000, 0.166, 0.333]
p = [0.000, 0.254, 0.998]

print("d is: " + str(["%.8f" % elem for elem in d]))
print("p is: " + str(["%.8f" % elem for elem in p]))

def rmse(predictions, targets):
return np.sqrt(((predictions - targets) ** 2).mean())

rmse_val = rmse(np.array(d), np.array(p))
print("rms error is: " + str(rmse_val))```

```d is: ['0.00000000', '0.16600000', '0.33300000']
p is: ['0.00000000', '0.25400000', '0.99800000']
rms error between lists d and p is: 0.387284994115```

## 数学符号：

RMSE是用小步骤完成的，因此可以理解：

```def rmse(predictions, targets):

differences = predictions - targets                       #the DIFFERENCEs.

differences_squared = differences ** 2                    #the SQUAREs of ^

mean_of_differences_squared = differences_squared.mean()  #the MEAN of ^

rmse_val = np.sqrt(mean_of_differences_squared)           #ROOT of ^

return root_of_of_the_mean_of_the_differences_squared     #get the ^```

## RMSE的每一步是如何工作的：

```8 - 5 = 3         #distance between 8 and 5 is 3
-20 - 10 = -30    #distance between -20 and 10 is +30```

```3*3     = 9   = positive
-30*-30 = 900 = positive```

## RMSE对不属于的离群数据点具有零容忍度。

`sklearn.metrics`有一个`mean_squared_error`功能。RMSE只是它返回的任何东西的平方根。

```from sklearn.metrics import mean_squared_error
from math import sqrt

rms = sqrt(mean_squared_error(y_actual, y_predicted))```