SciPy/NumPy:对csr_matrix进行归一化

4

我正在尝试将csr_matrix归一化:

<5400x6845 sparse matrix of type '<type 'numpy.float64'> with 91833 stored elements in Compressed Sparse Row format>

我尝试的方法是这样的:

import numpy as np
from scipy import sparse

# ve is my csr_matrix
ve_sum = ve.sum(axis=1)
ve_sums = sparse.csr_matrix(np.tile(ve_sum, (1, ve.shape[1]))) # <-- here I get MemoryError
n_ve = ve/ve_sums 

显然这不是做这种简单规范化的正确方法。

那么正确的方法是什么呢?

1个回答

4
# Normalize the rows of ve.
row_sums = np.array(ve.sum(axis=1))[:,0]
row_indices, col_indices = ve.nonzero()
ve.data /= row_sums[row_indices]

快速的谷歌搜索也可以显示这个信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接