【发布时间】:2015-12-14 11:21:25
【问题描述】:
我正在尝试计算稀疏行矩阵每一行中非零值的平均值。使用矩阵的均值方法不行:
>>> from scipy.sparse import csr_matrix
>>> a = csr_matrix([[0, 0, 2], [1, 3, 8]])
>>> a.mean(axis=1)
matrix([[ 0.66666667],
[ 4. ]])
以下方法有效,但对于大型矩阵很慢:
>>> import numpy as np
>>> b = np.zeros(a.shape[0])
>>> for i in range(a.shape[0]):
... b[i] = a.getrow(i).data.mean()
...
>>> b
array([ 2., 4.])
谁能告诉我是否有更快的方法?
【问题讨论】:
标签: python scipy sparse-matrix