[英]Why does the MSE sklearn library give me a different squared error compared to the l2 norm squared error?
I was trying to compute the mean squared error as in: 我正在尝试计算均方误差,如下所示:
in python. 在python中。 I saw that scipt/sklearn had an implementation for it already. 我看到scipt / sklearn已经有一个实现。 However, when I tried comparing it to my own implementation they did NOT agree. 但是,当我尝试将其与自己的实现进行比较时,他们不同意。 Why is that? 这是为什么? My implementation simply uses the norm 2 (or the Frobenius norm not matching) and nothing else fancy. 我的实现仅使用范数2(或不匹配的Frobenius范数),而没有其他选择。
To test this I wrote the following script: 为了测试这一点,我编写了以下脚本:
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)
I literally tested every combination I could think of and I still can't have them to match. 我从字面上测试了我能想到的每种组合,但仍然无法让它们匹配。 Any ideas? 有任何想法吗?
The formula that's used is a little unusual in that it doesn't take the square root of the sum of squares, whereas LA.norm
does. 使用的公式有点不寻常,因为它不取平方和的平方根,而LA.norm
则取。
If you look carefully at the docs, you can recreate the formula 如果您仔细查看文档,则可以重新创建公式
np.sum((X_truth-X_pred)**2)/X_truth.size
gives 0.49 just like 给出0.49就像
mean_squared_error(X_truth, X_pred)
or 要么
LA.norm(X_truth - X_pred)**2/X_truth.size
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.