- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compute the truth value of an array AND to another array element-wise based on conditions in Numpy
To compute the truth value of an array AND another array element-wise, use the numpy.logical_and() method in Python Numpy. Return value is either True or False. We have set conditions here.
Return value is the Boolean result of the logical AND operation applied to the elements of x1 and x2; the shape is determined by broadcasting. This is a scalar if both x1 and x2 are scalars.
The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.
Steps
At first, import the required library −
import numpy as np
Creating two 2D numpy array using the array() method. We have inserted elements. The True is considered value 1. The False is considered value 0 −
arr1 = np.array([[True, False, 7], [13, False, 11]]) arr2 = np.array([[False, 9, True], [19, 25, 6]])
Display the arrays −
print("Array 1...
", arr1) print("
Array 2...
", arr2)
Get the type of the arrays −
print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)
Get the dimensions of the Arrays −
print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)
Get the shape of the Arrays −
print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape)
To compute the truth value of an array AND another array element-wise, use the numpy.logical_and() method in Python Numpy. Return value is either True or False. We have set conditions here −
print("
Result (AND)...
",np.logical_and(arr1 > 10, arr2 < 15))
Example
import numpy as np # Creating two 2D numpy array using the array() method # We have inserted elements # The True is considered value 1 # The False is considered value 0 arr1 = np.array([[True, False, 7], [13, False, 11]]) arr2 = np.array([[False, 9, True], [19, 25, 6]]) # Display the arrays print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # Get the shape of the Arrays print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape) # To compute the truth value of an array AND another array elementwise, use the numpy.logical_and() method in Python Numpy # Return value is either True or False # We have set conditions here print("
Result (AND)...
",np.logical_and(arr1 > 10, arr2 < 15))
Output
Array 1... [[ 1 0 7] [13 0 11]] Array 2... [[ 0 9 1] [19 25 6]] Our Array 1 type... int64 Our Array 2 type... int64 Our Array 1 Dimensions... 2 Our Array 2 Dimensions... 2 Our Array 1 Shape... (2, 3) Our Array 2 Shape... (2, 3) Result (AND)... [[False False False] [False False True]]