1

I have a numpy array of tuples and wish to filter them, but nothing seems to work

 possible_points = np.random.normal((0,0),0.01,size=(100,2))
 possible_points = possible_points[possible_points > (0,0) and possible_points < (1,1)]

This yields an error

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I suspect the problem is somehow related with the following. When I do, for example,

possible_points = [possible_points > (0,0)]

possible_points becomes something like

[array([[False, False],
       [ True, False],
       [ True, False],
       [ True,  True],
       [ True,  True],
       [ True,  True],
       [ True,  True],
       [False, False],
       [ True, False],
    ...

From my understanding, it should return True if possible_points > (0,0) and False otherwise, but it's evaluating each tuple's coordinate individually. There are even some points that become [True, False]. If I do

possible_points = possible_points[possible_points > (0,0)]

I stop having tuples, and have something like

array([3.36687201e-03, 7.97227921e-04, 3.44875252e-03, 8.79364101e-03,....])

And I suspect this is what yields the ValueErros.

Can anyone help me understand what I'm doing wrong and help me achieve my goal?

2
  • What is your expected output? Commented Oct 31, 2020 at 13:00
  • @MichaelSzczesny, I wish to have the elements of possible points that are bigger than (0,0) and smaller than (1,1) Commented Oct 31, 2020 at 13:01

1 Answer 1

2

IIUC, you should use the & operator together with all across the columns (axis=1), like this:

import numpy as np

# set a seed for reproducibility
np.random.seed(42)

possible_points = np.random.normal((0, 0), 0.01, size=(100, 2))

# create the filtering mask
mask = ((possible_points > (0, 0)) & (possible_points < (1, 1))).all(axis=1)

# filter
possible_points = possible_points[mask]

print(possible_points)

Output

[[0.00647689 0.0152303 ]
 [0.01579213 0.00767435]
 [0.00738467 0.00171368]
 [0.01031    0.0093128 ]
 [0.00331263 0.00975545]
 [0.00812526 0.0135624 ]
 [0.00361396 0.01538037]
 [0.00915402 0.00328751]
 [0.00097078 0.00968645]
 [0.0029612  0.00261055]
 [0.01886186 0.00174578]
 [0.0006023  0.02463242]
 [0.01142823 0.00751933]
 [0.00586857 0.02190456]
 [0.0022746  0.01307143]
 [0.00259883 0.00781823]
 [0.00521942 0.00296985]
 [0.00250493 0.00346448]
 [0.01865775 0.00473833]
 [0.00963376 0.00412781]
 [0.0082206  0.01896793]
 [0.00276691 0.00827183]
 [0.00013002 0.01453534]
 [0.00173181 0.00385317]]
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this does the trick, thank you! I'll accept the answer when I can. I'll also try to investigate why that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.