Efficient way to normalize a Scipy Sparse Matrix

后端 未结 5 1732
孤独总比滥情好
孤独总比滥情好 2020-12-29 20:49

I\'d like to write a function that normalizes the rows of a large sparse matrix (such that they sum to one).

from pylab import *
import scipy.sparse as sp

d         


        
5条回答
  •  醉梦人生
    2020-12-29 21:33

    This has been implemented in scikit-learn sklearn.preprocessing.normalize.

    from sklearn.preprocessing import normalize
    w_normalized = normalize(w, norm='l1', axis=1)
    

    axis=1 should normalize by rows, axis=0 to normalize by column. Use the optional argument copy=False to modify the matrix in place.

提交回复
热议问题