| by Arround The Web | No comments

## Numpy Logical_AND

Like other frameworks and languages, NumPy also supports Logical operators and their operations like AND, OR etc.  In this numPy guide, we will discuss the numPy “Logical_And” Operation.  Logical_And Operator calculates the truth value of a1 AND a2 elementwise. The np.logical_and() method is a mathematical array method that is used to calculate the output of xi and yi for every component xi of array1 concerning element yi of array2. It gives us the output in an array form. For the np.logical_and() function to work, we must pass it to the input arrays of the same shape

## Syntax

This is the syntax for our method.

numpy.logical_and(x1, x2, out=None, Where =  True, dType=None)

## It has the Following Parameters

x1,x2 are the input arrays. These two must be of the same shape, which will also be the same as the output.

out is the location where the output is stored. We can provide the shape manually otherwise it will allocate a fresh array.

where is a condition that is an optional parameter. If the condition is true, the result array which is the out array will be set to unfunc output. Otherwise, the output array will stay in its actual form holding its actual value.

dtype defines the type of output array.

## Example # 01:

Now, we will take two arrays with the same and some different values. The AND Operator returns True when both the values are the same or both the conditions are True and returns False in all the other cases. This is how AND Operator works. In our example, we will also check whether our AND operator, using this method, works the same way or not.

After successfully importing our Numpy library as np, we have assigned values “1”, “1”, “0 “, and “0” to our first array, and “1”, “0”, “1”, and “0” to our second array. We will perform the AND operator on our arrays and we will see the output by printing our output array which we have initialized by ourselves as our third array. We will execute our code:

import numpy as np

arr_x = [1, 1, 0, 0]

arr_y = [1, 0, 1, 0]

arr_z = np.logical_and(arr_x, arr_y)

print(arr_z)

The following is the outcome we will get from our code. In this output, we can see that the function has returned the output as True and False. It has only returned True where the values of both arrays were the same. So, by the output, we can conclude that the numPy logical operator functions work the same as any other operators and give us the exact output.

## Example # 02:

Before moving on to the next example let us make one thing clear: zeros and ones are not only the values used during Logical Operations. In some cases, we also use values True and False where True is equal to “1” and False is equal to “0”. Now in the example below, we will pass the values True and False to our input arrays and we will perform the logical_and operation on those arrays and will store the result in the third array. By printing the third array, we will see whether our operator works on values like True and False or if it only works on zero and one values.

import numpy as np

arr_a = [False, False, True, True]

arr_b = [False, True, False, True]

arr_c = np.logical_and(arr_a, arr_b)

print(arr_c)

Let’s first import our NumPy library, the reason is we are going to perform some operations that are provided by the NumPy library. In the next line, we will declare two arrays to which we will be storing the array values as True and False as we explain above. After declaring both arrays that are “arr_a” and “arr_b”, we will declare another array that will be holding the result of our AND operation performed between the arrays passed to our logical AND operation. In the end, we will print the resulting array using the print() statement.

Here, the method has returned to us the output and it is the exact output we were expecting. Only True is returned where both arrays are True. That is how the AND Operator works in actuality as well. So, we can imply that it not only works on 0 and 1 but also works on True and False as well.

## Example # 03:

After working on the logical_and operator a question will arise in your mind: what happens if we do not pass the arrays of the same shape to our operator? To answer your question, we have done this example in which we have passed two arrays of different shapes to our function to see what output our function returns in that case will. We have declared two arrays, one with “4” elements in it and the other with “5” elements in it so that they do not have the same shape. We have stored the output in the third array and we will print it to check the output. Now, we will execute our code to check what happens.

import numpy as np

small_arr = [0, 0, 1, 1]

large_arr = [1, 0, 1, 0, 1]

out_arr = np.logical_and(small_arr, large_arr)

print(out_arr)

Now, first import the NumPy library and then define two arrays with different sizes naming them “small_arr” which contains “4” elements and “large_arr” which contains “5” elements. Then, we will define another array that will contain the resulting array after performing the logical AND operation. Lastly, we will print the resulting array using the print() statement.

Unfortunately, the code has given us an error. But by this example, we have learned that we can only pass arrays with the same shape to our function; otherwise, it will give an error. If we read the last line of error, we can understand that the system is telling us that we cannot broadcast shapes 4 and 5 together. The 4 and 5 are the number of elements that we have passed to our small_arr and large_arr respectively.

## Conclusion

We have learned about the NumPy np.logical_and() method in this guide. We have discussed how easily we can perform logical operations on our arrays with the help of the np.logical_and() function of numPy. We also showed how the system behaves when we pass it the arrays of different shapes. We have applied the “where” parameter to our function in this guide which helps us a lot in understanding the concept.

Source: linuxhint.com