Ich habe ein M
rumhängen:
In [241]: M
Out[241]:
<6x3 sparse matrix of type '<class 'numpy.uint8'>'
with 6 stored elements in Compressed Sparse Row format>
In [242]: M.A
Out[242]:
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[0, 1, 0],
[0, 0, 1],
[1, 0, 0]], dtype=uint8)
In [243]: M.sum(1) # dense matrix
Out[243]:
matrix([[1],
[1],
[1],
[1],
[1],
[1]], dtype=uint32)
In [244]: M/M.sum(1) # dense matrix - full size of M
Out[244]:
matrix([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.],
[ 0., 1., 0.],
[ 0., 0., 1.],
[ 1., 0., 0.]])
, dass der Speicherfehler erklären - wenn M
so groß ist, dass M.A
einen Speicherfehler erzeugt.
In [262]: S = sparse.csr_matrix(M.sum(1))
In [263]: S.shape
Out[263]: (6, 1)
In [264]: M.shape
Out[264]: (6, 3)
In [265]: M/S
....
ValueError: inconsistent shapes
Ich bin nicht ganz sicher, was hier vor sich geht.
Element weise Multiplikation arbeitet
In [266]: M.multiply(S)
Out[266]:
<6x3 sparse matrix of type '<class 'numpy.uint32'>'
with 6 stored elements in Compressed Sparse Row format>
So sollte es funktionieren, wenn ich S
als S = sparse.csr_matrix(1/M.sum(1))
konstruieren Wenn einige der Zeilen auf Null summieren, haben Sie eine Division durch Null Problem.
Wenn ich M
ändern 0 Reihe
In [283]: M.A
Out[283]:
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 0]], dtype=uint8)
In [284]: S = sparse.csr_matrix(1/M.sum(1))
/usr/local/bin/ipython3:1: RuntimeWarning: divide by zero encountered in true_divide
#!/usr/bin/python3
In [285]: S.A
Out[285]:
array([[ 1.],
[ 1.],
[ inf],
[ 1.],
[ 1.],
[ 1.]])
In [286]: M.multiply(S)
Out[286]:
<6x3 sparse matrix of type '<class 'numpy.float64'>'
with 5 stored elements in Compressed Sparse Row format>
In [287]: _.A
Out[287]:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.],
[ 1., 0., 0.]])
Dies ist zu haben nicht die beste M
dies auf zeigen, aber es schlägt einen sinnvollen Ansatz. Die Zeilensumme ist dicht, so dass Sie ihre Inverse mit den üblichen dichten Array-Ansätzen aufräumen können.
Ja, also ich suche nach Methoden, die keine dichte Matrix verwenden. – Paulo
Die Umkehrung der dichten Summe zuerst und dann spärlich machen ist der vielversprechendste Ansatz. – hpaulj
Vielen Dank für Ihre Antwort! Ich folge :) – Paulo