Efficient way to normalize a Scipy Sparse Matrix

后端 未结 5 1767
孤独总比滥情好
孤独总比滥情好 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:39

    I found this as an elegant way of doing it without using inbuilt functions.

    import scipy.sparse as sp
    
    def normalize(W):
        #Find the row scalars as a Matrix_(n,1)
        rowSumW = sp.csr_matrix(W.sum(axis=1))
        rowSumW.data = 1/rowSumW.data
    
        #Find the diagonal matrix to scale the rows
        rowSumW = rowSumW.transpose()
        scaling_matrix = sp.diags(rowSumW.toarray()[0])
    
        return scaling_matrix.dot(W)  
    

提交回复
热议问题