简体   繁体   English

scipy.linalg.norm与sklearn.preprocessing.normalize不同吗?

[英]scipy.linalg.norm different from sklearn.preprocessing.normalize?

from numpy.random import rand
from sklearn.preprocessing import normalize
from scipy.sparse import csr_matrix
from scipy.linalg import norm

w = (rand(1,10)<0.25)*rand(1,10)
x = (rand(1,10)<0.25)*rand(1,10)
w_csr = csr_matrix(w)
x_csr = csr_matrix(x)
(normalize(w_csr,axis=1,copy=False,norm='l2')*normalize(x_csr,axis=1,copy=False,norm='l2')).todense()

norm(w,ord='fro')*norm(x,ord='fro')

I am working with scipy csr_matrix and would like to normalize two matrices using the frobenius norm and get their product. 我正在使用scipy csr_matrix,并希望使用frobenius范数规范化两个矩阵并获得其乘积。 But norm from scipy.linalg and normalize from sklearn.preprocessing seem to evaluate the matrices differently. 但是,来自scipy.linalg的规范和来自sklearn.preprocessing的规范化似乎对矩阵进行了不同的评估。 Since technically in the above two cases I am calculating the same frobenius norm shouldn't the two expressions evaluate to the same thing? 由于从技术上讲,在上述两种情况下,我正在计算相同的frobenius范数,所以两个表达式不应该求同一个值吗? But I get the following answer: 但是我得到以下答案:

matrix([[ 0.962341]]) 矩阵([[0.962341]])

0.4431811178371029 0.4431811178371029

for sklearn.preprocessing and scipy.linalg.norm respectively. 分别用于sklearn.preprocessing和scipy.linalg.norm。 I am really interested to know what I am doing wrong. 我真的很想知道我在做什么错。

sklearn.prepocessing.normalize divides each row by its norm. sklearn.prepocessing.normalize 每一行以其范数。 It returns a matrix with the same shape as its input. 它返回与输入形状相同的矩阵。 scipy.linalg.norm returns the norm of the matrix. scipy.linalg.norm返回矩阵的范数。 So your calculations are not equivalent. 因此,您的计算并不等效。

Note that your code is not correct as it is written. 请注意,您编写的代码不正确。 This line 这条线

(normalize(w_csr,axis=1,copy=False,norm='l2')*normalize(x_csr,axis=1,copy=False,norm='l2')).todense()

raises ValueError: dimension mismatch . 引发ValueError: dimension mismatch The two calls to normalize both return matrices with shapes (1, 10), so their dimensions are not compatible for a matrix product. 这两个调用均对形状为( normalize两个返回矩阵进行normalize ,因此它们的尺寸与矩阵乘积不兼容。 What did you do to get matrix([[ 0.962341]]) ? 您做了什么来获取matrix([[ 0.962341]])

Here's a simple function to compute the Frobenius norm of a sparse (eg CSR or CSC) matrix: 这是一个用于计算稀疏(例如CSR或CSC)矩阵的Frobenius范数的简单函数:

def spnorm(a):
    return np.sqrt(((a.data**2).sum()))

For example, 例如,

In [182]: b_csr
Out[182]: 
<3x5 sparse matrix of type '<type 'numpy.float64'>'
with 5 stored elements in Compressed Sparse Row format>

In [183]: b_csr.A
Out[183]: 
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  2.,  0.,  4.,  0.],
       [ 0.,  0.,  0.,  2.,  1.]])

In [184]: spnorm(b_csr)
Out[184]: 5.0990195135927845

In [185]: norm(b_csr.A)
Out[185]: 5.0990195135927845

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sklearn.preprocessing.normalize 中的规范参数 - norm parameters in sklearn.preprocessing.normalize numpy.linalg.norm是否可以将sklearn.preprocessing.normalize(X,norm =&#39;l1&#39;,)替换为矩阵的L1-norm? - Can numpy.linalg.norm replace sklearn.preprocessing.normalize(X, norm='l1',) for L1-norm of matrix? sklearn.preprocessing.normalize考虑哪个L1规范? - Which L1 norm does sklearn.preprocessing.normalize consider? python sklearn:“ sklearn.preprocessing.normalize(X,norm =&#39;l2&#39;)”和“ sklearn.svm.LinearSVC(penalty =&#39;l2&#39;)”之间有什么区别 - python sklearn: what is the different between “sklearn.preprocessing.normalize(X, norm='l2')” and “sklearn.svm.LinearSVC(penalty='l2')” sklearn.preprocessing.normalize中的norm =&#39;l2&#39;对于矩阵归一化有什么作用? - What does norm='l2' in sklearn.preprocessing.normalize do for matrix normalization? 如何使用 sklearn.preprocessing.normalize 规范化 DataFrame 的列? - How to normalize the columns of a DataFrame using sklearn.preprocessing.normalize? sklearn.preprocessing.normalize如何对数据进行归一化,并且可以在具有均值和标准差的新数据上进行复制吗? - How does sklearn.preprocessing.normalize normalize data, and can I replicate on new data with mean and standard deviation? KneighborsClassifier给出与linalg.norm和scipy.spatial.distance.euclidean不同的欧几里得值 - KneighborsClassifier giving different euclidean value than linalg.norm and scipy.spatial.distance.euclidean numpy.linalg.norm VS L2 规范的 scipy cdist - numpy.linalg.norm VS scipy cdist for L2 norm numpy中linalg.norm的不同结果 - Different results for linalg.norm in numpy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM