【问题标题】:Python Pure RMSE vs SklearnPython 纯 RMSE 与 Sklearn
【发布时间】:2017-03-18 13:49:29
【问题描述】:

我相信我在纯 python 中计算 RMSE 时出错了。下面是代码。

y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
e = abs(np.matrix(y_pred) - np.matrix(y_true)).A1
ee = np.dot(e,e)
np.sqrt(ee.sum()/3)

This returns: 0.707

但是当我尝试使用 Sklearn 时

mean_squared_error(np.matrix(y_true),np.matrix(y_pred))**0.5
This returns: 0.612

知道发生了什么吗?很确定我的python代码是正确的。

【问题讨论】:

    标签: python pandas numpy statistics scikit-learn


    【解决方案1】:

    你没有犯错误。你除以3sklearn 除以4

    y_true = [3, -0.5, 2, 7]
    y_pred = [2.5, 0.0, 2, 8]
    e = abs(np.matrix(y_pred) - np.matrix(y_true)).A1
    ee = np.dot(e,e)
    np.sqrt(ee.sum()/4)
    
    0.61237243569579447
    

    除以n-1 为您提供无偏估计,并在计算样本的二阶矩时使用。在计算人口的这些相同时刻时,我们除以n。以下是可能相关的链接WikipediaSome other link

    【讨论】:

    • @cloud36 : 看看我的回答
    【解决方案2】:

    RMSE 的正确公式是:

    或者在你的情况下,n=len(y_pred)=len(y_true)=4。 所以为了得到正确的结果,把np.sqrt(ee.sum()/3)改成np.sqrt(ee.sum()/len(y_pred))

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 2021-06-21
      • 2019-03-22
      • 1970-01-01
      • 2017-02-19
      • 2014-06-15
      • 2020-03-19
      • 2021-03-24
      相关资源
      最近更新 更多