Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 bit-wise XOR of two boolean arrays element-wise in Numpy
To compute the bit-wise XOR of two boolean arrays element-wise, use the numpy.bitwise_xor() method in Python Numpy.
Computes the bit-wise XOR of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ^.
The 1st and 2nd parameter are the arrays, only integer and boolean types are handled. If x1.shape != x2.shape, they must be broadcastable to a common shape.
Steps
At first, import the required library −
import numpy as np
Creating two numpy boolean arrays using the array() method −
arr1 = np.array([[False, False, False], [True, False, True]]) arr2 = np.array([[False, True, False], [False, False, False]])
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 bit-wise XOR of two boolean arrays element-wise, use the numpy.bitwise_xor() −
print("
Result (Bit-wise XOR)...
",np.bitwise_xor(arr1, arr2))
Example
import numpy as np
# Creating two numpy boolean arrays using the array() method
arr1 = np.array([[False, False, False],
[True, False, True]])
arr2 = np.array([[False, True, False],
[False, False, False]])
# 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 bit-wise XOR of two arrays element-wise, use the numpy.bitwise_xor() method in Python Numpy
print("
Result (Bit-wise XOR)...
",np.bitwise_xor(arr1, arr2))
Output
Array 1... [[False False False] [ True False True]] Array 2... [[False True False] [False False False]] Our Array 1 type... bool Our Array 2 type... bool Our Array 1 Dimensions... 2 Our Array 2 Dimensions... 2 Our Array 1 Shape... (2, 3) Our Array 2 Shape... (2, 3) Result (Bit-wise XOR)... [[False True False] [ True False True]]
Advertisements