1

How do you filter out elements from a np array by using a expression? Say I had array arr = np.array(range(16)) then I want to filter out all numbers that are not perfectly divisible by 5 or 3, how would I do that? can you use np.remove() to do this? if so how? Thanks!

The shape
  • 359
  • 3
  • 10
  • 2
    There is a `np.delete`, but you specify the removals by index, not by some sort of value. But with any removal, what you are really doing is selecting the elements you want to copy to a new array. – hpaulj May 29 '21 at 20:11
  • 2
    take a look at [Filtering a NumPy Array: what is the best approach?](https://stackoverflow.com/q/58422690/14750360) – d-k-bo May 29 '21 at 20:25
  • 2
    Does this answer your question? [Filtering a NumPy Array: what is the best approach?](https://stackoverflow.com/questions/58422690/filtering-a-numpy-array-what-is-the-best-approach) – Camilo Martinez M. May 29 '21 at 20:29
  • 1
    A good general approach is to construct a boolean mask, the size of the original array, which is True for the values you want to keep, and False for the removal ones. Then `res = arr[mask]`. – hpaulj May 29 '21 at 21:01

0 Answers0