I have a question that is quite similiar to Sean Laws example that you can find here: https://seanlaw.github.io/2019/02/27/set-values-in-sparse-matrix/
In my case, I want to delete all the elements in a sparse csr matrix, which have an absolute value smaller than some epsilon.
First I tried something like
x[abs(x) < 3] = 0
but SciPy's warning about inefficiency lead me to Sean Laws explanation in the link above. I then tried manipulating his example code, but cannot find a solution to my problem.
Here is the code, with some negative entries added. The example code would remove all negative entries as they are smaller than 3. I tried around with np.abs() and also with adding a second logical operator but did not succeed up to now.
import numpy as np
from scipy.sparse import csr_matrix
x = csr_matrix(np.array([[1, 0.1, -2, 0, 3],
[0, -4, -1, 5, 0]]))
nonzero_mask = np.array(x[x.nonzero()] < 3)[0]
rows = x.nonzero()[0][nonzero_mask]
cols = x.nonzero()[1][nonzero_mask]
x[rows, cols] = 0
print(x.todense())
gives
[[0. 0. 0. 0. 3.]
[0. 0. 0. 5. 0.]]
But what I want is
[[0. 0. 0. 0. 3.]
[0. -4. 0. 5. 0.]]
Any help is greatly appreciated, I feel like I am missing something very basic. Thank you in advance!