title: How to delete small elements in sparse matrix in Python's SciPy?
body: <p>I have a question that is quite similiar to Sean Laws example that you can find here: 
  <a href="https://seanlaw.github.io/2019/02/27/set-values-in-sparse-matrix/" rel="nofollow noreferrer">https://seanlaw.github.io/2019/02/27/set-values-in-sparse-matrix/</a> </p>
  
  <p>In my case, I want to delete all the elements in a sparse csr matrix, which have an absolute value smaller than some epsilon.</p>
  
  <p>First I tried something like </p>
  
  <pre><code>x[abs(x) &lt; 3] = 0
  </code></pre>
  
  <p>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.  </p>
  
  <p>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. </p>
  
  <pre><code>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()] &lt; 3)[0]
  rows = x.nonzero()[0][nonzero_mask]
  cols = x.nonzero()[1][nonzero_mask]
  
  x[rows, cols] = 0
  print(x.todense())
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>[[0. 0. 0. 0. 3.]
   [0. 0. 0. 5. 0.]]
  </code></pre>
  
  <p>But what I want is</p>
  
  <pre><code>[[0. 0. 0. 0. 3.]
   [0. -4. 0. 5. 0.]]
  </code></pre>
  
  <p>Any help is greatly appreciated, I feel like I am missing something very basic.
  Thank you in advance!</p>