Ich versuche, den Mittelwert von Nicht-Null-Werten in jeder Zeile einer Sparse-Zeile Matrix zu berechnen. Unter Verwendung der mittleren Methode der Matrix ist es nicht tun:Mittelwert von Nicht-Null-Werten in Sparse-Matrix?
>>> from scipy.sparse import csr_matrix
>>> a = csr_matrix([[0, 0, 2], [1, 3, 8]])
>>> a.mean(axis=1)
matrix([[ 0.66666667],
[ 4. ]])
Folgende Werke, sondern ist für große Matrizen langsam:
>>> 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.])
Könnte jemand bitte sagen Sie mir, ob es eine schnellere Methode ist?
Ausgezeichnet, danke – batsc