简体   繁体   English

为什么MSE sklearn库给我的平方误差不同于l2范数平方误差?

[英]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.

相关问题 dataframe 中的计算均方误差 (MSE) 问题 - Mean squared error (MSE) problem with calculation in dataframe python sklearn:“ sklearn.preprocessing.normalize(X,norm ='l2')”和“ sklearn.svm.LinearSVC(penalty ='l2')”之间有什么区别 - python sklearn: what is the different between “sklearn.preprocessing.normalize(X, norm='l2')” and “sklearn.svm.LinearSVC(penalty='l2')” 如何评估均方误差(MSE)是否合理? - How do I evaluate whether the mean squared error (MSE) is reasonable or not? 在python中使用L2范数的LAD? (sklearn) - LAD with L2 norm in python? (sklearn) sklearn.preprocessing.normalize中的norm ='l2'对于矩阵归一化有什么作用? - What does norm='l2' in sklearn.preprocessing.normalize do for matrix normalization? Sklearn:如何在对训练数据进行分类时获得均方误差 - Sklearn: how to get mean squared error on classifying training data sklearn.metrics.mean_squared_error 越大越好(否定)吗? - Is sklearn.metrics.mean_squared_error the larger the better (negated)? 为什么不使用均方误差来解决分类问题? - Why not use mean squared error for classification problems? 平方误差是否取决于隐藏层的数量? - Does the squared error depends on the number of hidden layers? Numpy 中的均方误差? - Mean Squared Error in Numpy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM