简体   繁体   English

sklearn.preprocessing.normalize中的norm ='l2'对于矩阵归一化有什么作用?

[英]What does norm='l2' in sklearn.preprocessing.normalize do for matrix normalization?

I normalized scipy.sparse matrix sA here by using sklearn.preprocessing.normalize . 我在这里通过使用sklearn.preprocessing.normalize scipy.sparse矩阵sA了归一化。 I read the document but not understand about norm='l...' well, so I tested it. 我阅读了该文档,但是对norm='l...'理解却不够,所以我对其进行了测试。

norm='l1' went ok and I got the sum result as expected as 1 in all row. norm='l1'正常,我在所有行中得到的期望结果为1。

A = np.array([[1,2,0],[0,0,3],[1,0,4]])
sA = sp.csr_matrix(A)   
normsA = normalize(sA, norm='l1', axis=0)
print normsA
print "---"
print sum(normsA)

>>(0, 0)    0.5
  (2, 0)    0.5
  (0, 1)    1.0
  (1, 2)    0.428571428571
  (2, 2)    0.571428571429
  ---
  (0, 0)    1.0
  (0, 1)    1.0
  (0, 2)    1.0

However, when I tried l2 , I can't find how it is normalizing the matrix. 但是,当我尝试l2 ,我找不到它如何对矩阵进行归一化。 Sum of the matrix or the transposed matrix is not equal to one. 矩阵或转置矩阵的总和不等于1。 What l2 does for normalizing here? l2对这里的归一化有什么作用?

normsA2 = normalize(sA, norm='l2', axis=0)
print sum(normsA2)
print sum(normsA2.T)

>>(0, 0)    1.41421356237
  (0, 1)    1.0
  (0, 2)    1.4
  (0, 0)    1.70710678119
  (0, 1)    0.6
  (0, 2)    1.50710678119

That is using the l2 norm (or euclidean norm/distance), in other words the sum of the squares of the elements gives one. 那就是使用l2范数 (或欧几里德范数/距离),换句话说,元素平方的和为1。

The following outputs the expected vector of ones: 以下输出期望的1的向量:

sum(normsA2 ** 2, axis=0)

暂无
暂无

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

相关问题 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')” sklearn.preprocessing.normalize考虑哪个L1规范? - Which L1 norm does sklearn.preprocessing.normalize consider? numpy.linalg.norm是否可以将sklearn.preprocessing.normalize(X,norm ='l1',)替换为矩阵的L1-norm? - Can numpy.linalg.norm replace sklearn.preprocessing.normalize(X, norm='l1',) for L1-norm of matrix? sklearn.preprocessing.normalize 中的规范参数 - norm parameters in sklearn.preprocessing.normalize scipy.linalg.norm与sklearn.preprocessing.normalize不同吗? - scipy.linalg.norm different from 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? 如何使用 sklearn.preprocessing.normalize 规范化 DataFrame 的列? - How to normalize the columns of a DataFrame using sklearn.preprocessing.normalize? 在python中使用L2范数的LAD? (sklearn) - LAD with L2 norm in python? (sklearn) 在sklearn python中撤消L2规范化 - Undo L2 Normalization in sklearn python 为什么MSE sklearn库给我的平方误差不同于l2范数平方误差? - Why does the MSE sklearn library give me a different squared error compared to the l2 norm squared error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM