I normalized scipy.sparse
matrix sA
here by using sklearn.preprocessing.normalize
. I read the document but not understand about norm='l...'
well, so I tested it.
norm='l1'
went ok and I got the sum result as expected as 1 in all row.
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. Sum of the matrix or the transposed matrix is not equal to one. What l2
does for normalizing here?
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.
The following outputs the expected vector of ones:
sum(normsA2 ** 2, axis=0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.