The Numpy library in python consists of a large collection of high-level mathematical functions. These functions are used for handling large, multi-dimensional arrays and matrices in python and for performing various logical _and statistical operations on them. With numpy, we can perform mathematical computations at high speed in python. In this article, we shall be learning about one of the logical functions present in numpy – the numpy logical _and function.
What are logical operations?
In mathematics, logical operations are performed to obtain the relation between two entities in the form of a boolean value. It will test whether the logical relationship between two entities is True or False. In python programming, logical operations can be performed between two lists, variables, or arrays. The numpy module contains four main logical operations and logical _and is one of them.
Numpy logical _and
The numpy logical _and is a function to perform the logical AND operation in python. With this function, we can find the truth value for the AND operation between two variables or element-wise computation for two lists or arrays. The bitwise & operator can be used in place of the logical _and function when we are working with boolean values.
Syntax of numpy logical _and function
The syntax of numpy logical _and function is as follows:
numpy.logical_and(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'logical_and'>
x1, x2: These are the input arrays between whose elements the logical_and operation has to be performed. The shape of both arrays should be equal.
out: It is an n-dimensional array which is an optional parameter. The default value of ‘out’ is ‘None’. This parameter specifies the location where the output has to be stored. The length should be equal to x1 and x2. If not mentioned, by default, it will return a new array with the result.
where: It is an optional parameter that is array_like. The ‘where’ parameter mentions a condition.
y: The function returns an n-dimensional array consisting of boolean values. These boolean values have been obtained by applying AND operation between the elements from arrays x1 and x2.
The truth table of logical AND operation
The basic truth table for AND operation between two boolean values is:
Examples of numpy logical _and
Let us first understand how the numpy logical _and function works by taking few examples. First, we will import the numpy library.
Then, we shall see the output of logical AND operation between different combinations of boolean values. The logical _and function will assign output as ‘True’ only if both the values are True.
When x1 = False and x2 = False
For two ‘False’ boolean values, the output of the logical _and function will also be false.
import numpy as np np.logical_and(False, False)
When x1 = False and x2 = True
import numpy as np np.logical_and(False, True)
When x1 = True and x2 = False
import numpy as np np.logical_and(True, False)
When x1 = True and x2 = True
import numpy as np np.logical_and(True, True)
Logical _and between two arrays
We can apply the logical _and function between two arrays. The arrays can be boolean arrays, number arrays, or a combination of both. Let us take two boolean arrays:
import numpy as np x1 = [False, True, False] x2 = [True, False, False] print(np.logical_and(x1,x2))
The output is :
[False False False]
Let us take two arrays containing both – numerical values as well as boolean values. For numerical values, all non zero values will be True and zero will be False.
import numpy as np x1 = [False,7,0,4,5] x2 = [9,True,False,8,1] print(np.logical_and(x1,x2))
[False True False True True]
Logical _and on conditions
We can also write conditions instead of an array of values. Let us consider an array ‘x’, which we will obtain from the arange() function present in them. Then, we will use logical _and to print only those numbers which lie between 1 and 10.
import numpy as np x = np.arange(0,20,5) print(x) print(np.logical_and(x>1,x<10))
The output is:
[ 0 5 10 15] [False True False False]
This is because 5 is the only number from x which outputs True for both the given conditions.
Using Numpy logical _and for more than two arrays
Although the syntax of the logical _and function only allows comparing two arrays, we can use it to compare more than two arrays simultaneously.
Because of the commutative property of the AND operator, this is possible. We shall use two logical _and functions. Here, we will pass the logical _and function as the first argument to an outside logical _and function.
The code to achieve that is given below:
import numpy as np x1 = [8,9,0,1,4,6] x2 = [0,5,2,6,7,4] x3 = [1,2,1,5,9,8] print(np.logical_and(np.logical_and(x1,x2),x3))
The output is:
[False True False True True True]
Numpy logical _and along axis using reduce
We can also calculate the logical _and value between two arrays by mentioning the axis parameter using the reduce() function. For example, let us take a two-dimensional array and calculate its value with axis=0 and axis=1.
For axis = 0:
This will perform the logical _and operation column-wise among the column elements.
import numpy as np x1 = np.array([[True, False],[True, True], [True, False]]) print(np.logical_and.reduce(x1, axis=0))
The output is:
For axis = 1:
This will perform the logical _and operation row-wise for each row. Since here we have three rows, the length of the output array will be 3.
import numpy as np x1 = np.array([[True, False],[True, True], [True, False]]) print(np.logical_and.reduce(x1, axis=1))
[False True False]
FAQ’s on Numpy logical _and
The logical _and operator will compare the values of two arrays and return the logical _and comparison in the form of a boolean array. At the same time, the bitwise and operand will perform the logical _and operation on the bits. Let us take an example.
print(9 & 5) #1
Both the above statements will give different outputs. In the case of statement 1, where we have the bitwise & operator, it will apply AND on the bits of the numbers 9 and 5.
In the case of statement 2, it will apply the logical _and operation and compare the boolean values of the two numbers. Thus, the output will also be in boolean form.
Valueerror may occur while using logical _and if the size of the two arrays – x1 and x2 is not the same. Pass arrays of equal dimensions, and the array shall be resolved.
That is all, folks! If you have any questions in mind, leave them below in the comments.
Until next time, Keep Learning!