0
Wenn ich eine dichte Matrix d
Filter dichte Matrix und verwandeln sich in spärliche CSR-Matrix?
d = np.random.rand(4,4); d
>>> array([[ 0.95969707, 0.91678543, 0.28401043, 0.27390336],
[ 0.50924662, 0.37591608, 0.32424021, 0.56422093],
[ 0.61126002, 0.42979466, 0.67529606, 0.4462593 ],
[ 0.12900253, 0.81314236, 0.40393894, 0.79878679]])
Wie I/Filterwerte umwandeln, die unter einem Schwellenwert sind und drehen diese in 0.0 so kann ich d
in eine spärliche csr Matrix machen? So in dem obigen Beispiel der erste Schritt so etwas wie wäre:
threshold = 0.8
d = d.filter(lambda x: x > threshold); d # pseudo code - filter values below 0.8
>>> array([[ 0.95969707, 0.91678543, 0.0, 0.0],
[ 0.0, 0.0, 0.0, 0.0],
[ 0.0, 0.0, 0.0, 0.0],
[ 0.0, 0.81314236, 0.0, 0.0]])
Und dann d in eine spärliche csr Matrix drehen:
scipy.sparse.csr_matrix(d)
>>> <3x3 sparse matrix of type '<class 'numpy.int32'>'
with 4 stored elements in Compressed Sparse Row format>
EDIT: Ich habe Methoden gesehen wie:
d = d[np.where(d>0.8)]; d
>>> array([ 0.95969707, 0.91678543 , 0.81314236])
aber nicht beibehalten die Form d
, so wird es nicht in eine csr Matrix