Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to operate logic operation of all columns of a 2D numpy array

Tags:

python

numpy

Let's say I have the following 2D NumPy array consisting of four rows and three columns:

>>> a = numpy.array([[True, False],[False, False], [True, False]])
>>> array([[ True, False],
       [False, False],
       [ True, False]], dtype=bool)

What would be an efficient way to generate a 1D array that contains the logic or of all columns (like [True, False])?

I searched the web and found someone referring to sum(axis=) to calculate the sum.

I wonder if there is some similar way for logic operation?

like image 879
noodles guo Avatar asked Dec 02 '16 07:12

noodles guo


People also ask

How do I select a column in NumPy 2D array?

We can use [][] operator to select an element from Numpy Array i.e. Example 1: Select the element at row index 1 and column index 2. Or we can pass the comma separated list of indices representing row index & column index too i.e.

Which function creates a 2D array with all values 1?

We can create a 2D array by passing a list to the array() function of NumPy.

How do I get the sum of all columns in NumPy?

Use the numpy. sum() Function to Find the Sum of Columns of a Matrix in Python. The sum() function calculates the sum of all elements in an array over the specified axis. If we specify the axis as 0, then it calculates the sum over columns in a matrix.

How do you make a 2D NumPy array?

In Python to declare a new 2-dimensional array we can easily use the combination of arange and reshape() method. The reshape() method is used to shape a numpy array without updating its data and arange() function is used to create a new array.

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 do you do arithmetic operations in NumPy?

In addition to arithmetic operators, Numpy also provides functions to perform arithmetic operations. You can use functions like add, subtract, multiply, divide to perform array operations. For example: Let’s add a one-dimensional array to the two-dimensional array.

What is a 2D array in NumPy?

In this we are specifically going to talk about 2D arrays. 2D Array can be defined as array of an array. 2D array are also called as Matrices which can be represented as collection of rows and columns. In this article, we have explored 2D array in Numpy in Python.

What is logical_and and logical_or in NumPy?

Each of the values in the resulting array represents the lowest value for that particular row. Numpy provides logic functions like logical_and, logical_or etc., in a similar pattern to perform logical operations. For example: In addition to arithmetic operators, Numpy also provides functions to perform arithmetic operations.


2 Answers

Yes, there is. Use any:

>>> a = np.array([[True, False],[False, False], [True, False]])
>>> a
array([[ True, False],
       [False, False],
       [ True, False]], dtype=bool)
>>> a.any(axis=0)
array([ True, False], dtype=bool)

Note what happens when you change the argument axis to 1:

>>> a.any(axis=1)
array([ True, False,  True], dtype=bool)
>>> 

If you want logical-and use all:

>>> b.all(axis=0)
array([False, False], dtype=bool)
>>> b.all(axis=1)
array([ True, False, False], dtype=bool)
>>> 

Also note that if you leave out the axis keyword argument, it works across every element:

>>> a.any()
True
>>> a.all()
False
like image 100
juanpa.arrivillaga Avatar answered Oct 23 '22 15:10

juanpa.arrivillaga


NumPy has also a reduce function which is similar to Python's reduce. It's possible to use it with NumPy's logical operations. For example:

>>> a = np.array([[True, False],[False, False], [True, False]])
>>> a
array([[ True, False],
       [False, False],
       [ True, False]])
>>> np.logical_or.reduce(a)
array([ True, False])
>>> np.logical_and.reduce(a)
array([False, False])

It also has the axis parameter:

>>> np.logical_or.reduce(a, axis=1)
array([ True, False,  True])
>>> np.logical_and.reduce(a, axis=1)
array([False, False, False])

The idea of reduce is that it cumulatively applies a function (in our case logical_or or logical_and) to each row or column.

like image 6
Georgy Avatar answered Oct 23 '22 15:10

Georgy