为什么MSE sklearn库与l2范数平方误差相比给出了不同的平方误差?

时间:2016-08-05 18:11:36

标签: python numpy machine-learning scipy scikit-learn

我试图计算均方误差,如:

factory_boy docs:

在python中

。我看到enter image description here已经实现了它。但是,当我尝试将它与我自己的实现进行比较时,他们确实 NOT 同意。这是为什么?我的实现只使用标准2(或Frobenius规范不匹配)而没有别的想法。

为了测试这个,我编写了以下脚本:

import sklearn
from sklearn.decomposition import PCA
from sklearn.metrics import mean_squared_error
import numpy as np
from numpy import linalg as LA

X_truth = np.ones((5,6))
X_pred = 1.7*np.ones((5,6))

print 'LA error: ', (1.0/5)*LA.norm(X_truth - X_pred)**2
print 'LA error: ', (1.0/X_truth.shape[0])*LA.norm(X_truth - X_pred)**2
print 'LA error:: ', (1.0/5)*LA.norm(X_truth - X_pred, 'fro')**2
print 'LA error: ', LA.norm(X_truth - X_pred)**2
print 'LA error: ', LA.norm(X_truth - X_pred)
print 'LA error: ', (1.0/X_truth.shape[0])*LA.norm(X_truth - X_pred)
print 'sklearn MSE error: ', mean_squared_error(X_truth, X_pred)

我真的测试了我能想到的每一个组合,但我仍然无法让它们匹配。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

使用的公式有点不寻常,因为它没有取平方和的平方根,而LA.norm则没有。

如果仔细查看文档,可以重新创建公式

np.sum((X_truth-X_pred)**2)/X_truth.size

给出了0.49,就像

一样

mean_squared_error(X_truth, X_pred)

LA.norm(X_truth - X_pred)**2/X_truth.size

相关问题