Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining logic statements AND in numpy array

What would be the way to select elements when two conditions are True in a matrix? In R, it is basically possible to combine vectors of booleans.

So what I'm aiming for:

A = np.array([2,2,2,2,2])
A < 3 and A > 1  # A < 3 & A > 1 does not work either

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

It should eval to:

array([True,True,True,True,True])

My workaround usually is to sum these boolean vectors and equate to 2, but there must be a better way. What is it?

like image 556
PascalVKooten Avatar asked Feb 24 '14 19:02

PascalVKooten


People also ask

What are the logical operations we can perform on NumPy arrays?

Below are the various logical operations we can perform on Numpy arrays: The numpy module supports the logical_and operator. It is used to relate between two variables. If two variables are 0 then output is 0, if two variables are 1 then output is 1 and if one variable is 0 and another is 1 then output is 0.

How to combine 1-D and 2-D arrays in NumPy?

Sometimes we need to combine 1-D and 2-D arrays and display their elements. Numpy has a function named as numpy.nditer (), which provides this facility. Syntax: numpy.nditer (op, flags=None, op_flags=None, op_dtypes=None, order=’K’, casting=’safe’, op_axes=None, itershape=None, buffersize=0) Attention geek!

What is joining in NumPy?

Joining means putting contents of two or more arrays in a single array. In SQL we join tables based on a key, whereas in NumPy we join arrays by axes.

How to concatenate elements in an array using NumPy?

After this, we use ‘.’ to access the NumPy package. Next press array then type the elements in the array. the code is: Now when we’re going to do concatenate, then we can make this happen in two ways, this along axis 0 and along axis 1. in Numpy the default setting is axis=0.


2 Answers

you could just use &, eg:

x = np.arange(10)
(x<8) & (x>2)

gives

array([False, False, False,  True,  True,  True,  True,  True, False, False], dtype=bool)

A few details:

  • This works because & is shorthand for the numpy ufunc bitwise_and, which for the bool type is the same as logical_and. That is, this could also be spelled out as
    bitwise_and(less(x,8), greater(x,2))
  • You need the parentheses because in numpy & has higher precedence than < and >
  • and does not work because it is ambiguous for numpy arrays, so rather than guess, numpy raise the exception.
like image 161
tom10 Avatar answered Dec 03 '22 13:12

tom10


There's a function for that:

In [8]: np.logical_and(A < 3, A > 1)
Out[8]: array([ True,  True,  True,  True,  True], dtype=bool)

Since you can't override the and operator in Python it always tries to cast its arguments to bool. That's why the code you have gives an error.

Numpy has defined the __and__ function for arrays which overrides the & operator. That's what the other answer is using.

like image 26
Daniel Avatar answered Dec 03 '22 13:12

Daniel