Boolean Array in NumPy – Python
Last Updated :
29 Apr, 2025
The goal here is to work with Boolean arrays in NumPy, which contain only True or False values. Boolean arrays are commonly used for conditional operations, masking and filtering elements based on specific criteria. For example, given a NumPy array [1, 0, 1, 0, 1], we can create a Boolean array where 1 becomes True and 0 becomes False. Let’s explore different efficient methods to achieve this.
Using astype()
astype() method is an efficient way to convert an array to a specific data type. When converting an integer array to a Boolean array, astype(bool) converts all non-zero values to True and zeros to False. This method is preferred for its directness and speed.
Python
import numpy as np
a = np.array([1, 0, 1, 0, 0, 1, 0])
b = a.astype(bool)
print(b)
Output[ True False True False False True False]
Explanation: astype(bool) method converts non-zero values to True and zeros to False. So, the array [1, 0, 1, 0, 0, 1, 0] becomes [True, False, True, False, False, True, False].
Using dtype=’bool’
When creating a NumPy array, you can specify the dtype=’bool’ argument to directly convert the data type to Boolean. This method is efficient because it processes the conversion during the array creation step, providing a clean and fast approach to generating a Boolean array from integers.
Python
import numpy as np
a = np.array([1, 0, 1, 0, 0, 1, 0])
b = np.array(a, dtype=bool)
print(b)
Output[ True False True False False True False]
Explanation: dtype=bool argument convert the array a into Boolean values. It treats non-zero values as True and zeros as False.
Using np.where()
np.where() function in NumPy is a versatile tool for conditional operations. It checks each element against a condition (e.g., arr == 1) and returns True or False. While less efficient than direct type conversion, it allows for custom conditions and complex logic.
Python
import numpy as np
a = np.array([1, 0, 1, 0, 0, 1, 0])
b = np.where(a == 1, True, False)
print(b)
Output[ True False True False False True False]
Explanation: np.where() function checks each element of the array a against the condition a == 1. If the condition is true (i.e., the element is 1), it returns True otherwise, it returns False.
Using a comparison with == 1
Using == 1 creates a Boolean array by checking if elements are equal to 1, returning True for 1 and False otherwise. While simple and efficient, it’s slightly less efficient than astype() or dtype=’bool’ due to the explicit element check.
Python
import numpy as np
a = np.array([1, 0, 1, 0, 0, 1, 0])
b = a == 1
print(b)
Output[ True False True False False True False]
Explanation: a == 1 checks each element of the array a to see if it is equal to 1. It returns a Boolean array where True corresponds to elements that are 1 and False for all other values.
Similar Reads
Python - Boolean Array in NumPy
The goal here is to work with Boolean arrays in NumPy, which contain only True or False values. Boolean arrays are commonly used for conditional operations, masking and filtering elements based on specific criteria. For example, given a NumPy array [1, 0, 1, 0, 1], we can create a Boolean array wher
3 min read
numpy.array_equal() in Python
numpy.array_equal(arr1, arr2) : This logical function that checks if two arrays have the same shape and elements. Parameters : arr1 : [array_like]Input array or object whose elements, we need to test. arr2 : [array_like]Input array or object whose elements, we need to test. Return : True, if both ar
1 min read
NumPy Array in Python
NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C
2 min read
numpy.array_equiv() in Python
numpy.array_equiv(arr1, arr2) : This logical function that checks if two arrays have the same elements and shape consistent. Shape consistent means either they are having the same shape, or one input array can be broadcasted to create the same shape as the other one. Parameters : arr1 : [array_like]
2 min read
numpy.asarray() in Python
numpy.asarray()function is used when we want to convert input to an array. Input can be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and arrays. Syntax : numpy.asarray(arr, dtype=None, order=None) Parameters : arr : [array_like] Input data, in any form that can be converted to a
2 min read
numpy.asfarray() in Python
numpy.asfarray()function is used when we want to convert input to a float type array. Input includes scalar, lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Syntax : numpy.asfarray(arr, dtype=type 'numpy.float64') Parameters : arr : [array_like] Input data, in any for
2 min read
numpy.array_str() in Python
numpy.array_str()function is used to represent the data of an array as a string. The data in the array is returned as a single string. This function is similar to array_repr, the difference being that array_repr also returns information on the kind of array and its data type. Syntax : numpy.array_st
2 min read
Python | Numpy numpy.ndarray.__ne__()
With the help of numpy.ndarray.__ne__() method of Numpy, We can find that which element in an array is not equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ne__($self, value, /) Return: self!=
1 min read
numpy.array_repr() in Python
numpy.array_repr()function is used to convert an array to a string. Syntax : numpy.array_repr(arr, max_line_width=None, precision=None, suppress_small=None) Parameters : arr : [array_like] Input array. max_line_width : [int, optional] The maximum number of columns the string should span. Newline cha
2 min read
numpy.asanyarray() in Python
numpy.asanyarray()function is used when we want to convert input to an array but it pass ndarray subclasses through. Input can be scalars, lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Syntax : numpy.asanyarray(arr, dtype=None, order=None) Parameters : arr : [array_
2 min read