Cur*_*ous 7 python numpy scipy sparse-matrix
我有一个稀疏的988x1向量(a中的一列csr_matrix)scipy.sparse.有没有办法获得其均值和标准偏差而无需将稀疏矩阵转换为密集矩阵?
numpy.mean 似乎只适用于密集的载体.
由于您正在执行列切片,因此使用CSC而不是CSR存储矩阵可能更好.但这取决于你用矩阵做了什么.
要计算CSC矩阵中列的平均值,可以使用矩阵的mean()函数.
要有效地计算标准偏差将需要更多的努力.首先,假设你得到这样的稀疏列:
col = A.getcol(colindex)
Run Code Online (Sandbox Code Playgroud)
然后像这样计算方差:
N = col.shape[0]
sqr = col.copy() # take a copy of the col
sqr.data **= 2 # square the data, i.e. just the non-zero data
variance = sqr.sum()/N - col.mean()**2
Run Code Online (Sandbox Code Playgroud)