归一化稀疏行概率矩阵

Normalize sparse row probability matrix

我有一个包含几个元素的稀疏矩阵。现在我想对它进行行归一化。然而,当我这样做时,它被转换为一个 numpy 数组,从性能的角度来看这是不可接受的。

为了使事情更具体,请考虑以下示例:

x = csr_matrix([[0, 1, 1], [2, 3, 0]])  # sparse
normalization = x.sum(axis=1)  # dense, this is OK

x / normalization  # this is dense, not OK, can be huge

有没有一种优雅的方法可以做到这一点而不必诉诸 for 循环?

编辑

是的,这可以使用 sklearn.preprocessing.normalize 使用 'l1' 标准化来完成,但是,我不想依赖 sklearn。

您始终可以使用 csr 内部结构:

>>> import numpy as np
>>> from scipy import sparse
>>> 
>>> x = sparse.csr_matrix([[0, 1, 1], [2, 3, 0]]) 
>>> 
>>> x.data = x.data / np.repeat(np.add.reduceat(x.data, x.indptr[:-1]), np.diff(x.indptr))
>>> x
<2x3 sparse matrix of type '<class 'numpy.float64'>'
        with 4 stored elements in Compressed Sparse Row format>
>>> x.A
array([[0. , 0.5, 0.5],
       [0.4, 0.6, 0. ]])