Reputation: 7338
If I have a dense matrix d
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]])
How to I transform / filter values that are below a threshold and turn these into 0.0 so I can make d
into a sparse csr matrix? So in the example above, the first step would be something like:
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]])
And then turn d into a sparse csr matrix:
scipy.sparse.csr_matrix(d)
>>> <3x3 sparse matrix of type '<class 'numpy.int32'>'
with 4 stored elements in Compressed Sparse Row format>
EDIT: I have seen methods like:
d = d[np.where(d>0.8)]; d
>>> array([ 0.95969707, 0.91678543 , 0.81314236])
but that does not preserve the shape of d
, so it won't work to turn into a csr matrix
Upvotes: 0
Views: 275