In this article we will discuss different ways to create a boolean Numpy array. We will start by creating Numpy arrays with random boolean values. Then we will see ways to create a Numpy array with all True or all False.
Create boolean Numpy array with random boolean values
To create a boolean numpy array with random values we will use a function random.choice() from python’s numpy module,
numpy.random.choice(a, size=None, replace=True, p=None)
Arguments:
- a: A Numpy array from which random sample will be generated
- size : Shape of the array to be generated
- replace : Whether the sample is with or without replacement
It generates a random sample from a given 1-D array.
Let’s use this function to create a boolean numpy array of size 10 with random bool values,
# Array for random sampling sample_arr = [True, False] # Create a numpy array with random True or False of size 10 bool_arr = np.random.choice(sample_arr, size=10) print('Numpy Array: ') print(bool_arr)
Output:
Numpy Array: [ True True True False False True False False False True]
How did it work?
Frequently Asked:
- Check if all elements in NumPy Array are False
- Apply a function to every element in NumPy Array
- Python Numpy: flatten() vs ravel()
- Remove Array elements that are in Another Array – Python
First we create a bool array with only 2 values i.e. True & false,
# Array for random sampling sample_arr = [True, False]
Then we passed this array to numpy.random.choice() along with argument size=10,
# Create a numpy array with random True or False of size 10 bool_arr = np.random.choice(sample_arr, size=10)
This function generates a 10 random elements based on the values in sample_arr i.e. either True or False,
[ True True True False False True False False False True]
So this is how we generated a random boolean Numpy array.
Creating 2D boolean Numpy array with random values
To create a 2D boolean Numpy array with random True or false values, we can use the same function by passing the size of 2D array as a tuple,
# Array for random sampling sample_arr = [True, False] # Create a 2D numpy array or matrix of 3 rows & 4 columns with random True or False values bool_arr = np.random.choice(sample_arr, size=(3,4)) print('2D Numpy Array: ') print(bool_arr)
Output:
2D Numpy Array: [[ True True True True] [ True True True False] [ True True True True]]
Create a Bool array with all True
To Create a boolean numpy array with all True values, we can use numpy.ones() with dtype argument as bool,
# Create a Numpy array of 10 True values bool_arr = np.ones(10, dtype=bool) print('Numpy Array: ') print(bool_arr)
Output:
Numpy Array: [ True True True True True True True True True True]
numpy.ones() creates a numpy array of given size and initializes all values with 1. But if dtype argument is passed as bool then it converts all 1 to bool i.e. True.
Create a Bool array with all False
To Create a boolean numpy array with all False values, we can use numpy.zeros() with dtype argument as bool,
# Create a Numpy array of 10 False values bool_arr = np.zeros(10, dtype=bool) print('Numpy Array: ') print(bool_arr)
Output:
Numpy Array: [False False False False False False False False False False]
numpy.zeros() creates a numpy array of given size and initializes all values with 0. But if dtype argument is passed as bool then it converts all 0 to bool i.e. False.
So, this is how we can generate a numpy array of 10 False values. If we want 2D Numpy Array with all True or False values then we can pass a tuple as shape argument along with dtype as bool,
Creating 2D Numpy array with all True,
# Create a 2D Numpy array of 3 rows & 4 columns with all True values bool_arr = np.ones((3,4), dtype=bool) print('2D Numpy Array: ') print(bool_arr)
Output:
2D Numpy Array: [[ True True True True] [ True True True True] [ True True True True]]
We used numpy.ones() to generate a numpy array of given shape (3,4) i.e. 3 rows and 4 columns. As ones() generates all 1s, but we passed the dtype as bool, due to which all these 1s got implicitly converted to True
Creating 2D Numpy array with all False,
# Create a 2D Numpy array of 3 rows & 4 columns with all False values bool_arr = np.zeros((3,4), dtype=bool) print('2D Numpy Array: ') print(bool_arr)
Output:
2D Numpy Array: [[False False False False] [False False False False] [False False False False]]
We used numpy.zeros() to generate a numpy array of given shape (3,4) i.e. 3 rows and 4 columns. As zeros() generates all 0�s, but we passed the dtype as bool, due to which all these 0s got implicitly converted to False.
Converting a List to bool Numpy array
Convert a list of integers to boolean numpy array
# List of integers list_of_elems = [1, 2, 3, 0, 9, 0, 8, 0] # Convert a list of integers to bool array bool_arr = np.array(list_of_elems, dtype=bool) print('Numpy Array: ') print(bool_arr)
Output:
[ True True True False True False True False]
As we passed the dtype argument as bool in the numpy.array() function, therefore all integers in the list were converted into True or False implicitly. Integers other than 0 were converted to True and all 0s were converted to False.
Convert a heterogeneous list to boolean numpy array
Lists are heterogeneous in python. It means it can contain elements of different data types. But Numpy Arrays in python are homogeneous, it means they can contain elements of the same data type. So, to convert a heterogeneous list to boolean numpy array, we will pass dtype argument as bool in the numpy.array() function,
# heterogeneous List ( contains different type of elements) list_of_elems = [8, 0, 'Hi', '', 0.4] # Convert a heterogeneous list to bool numpy array bool_arr = np.array(list_of_elems, dtype=bool) print(bool_arr)
Output:
[ True False True False True]
As we passed the dtype argument as bool in the numpy.array() function, therefore all integers or strings or other types of elements in the list were converted into True or False implicitly.
Integers other than 0 were converted to True and all 0s were converted to False.
All empty strings were converted to False and other strings were converted to True.
The complete example is as follows,
import numpy as np def main(): print('*** Create a Boolean Numpy Array with random boolean values ***') print('Create a 1D Numpy boolean array of size 10 with random value') # Array for random sampling sample_arr = [True, False] # Create a numpy array with random True or False of size 10 bool_arr = np.random.choice(sample_arr, size=10) print('Numpy Array: ') print(bool_arr) print('Create a 2D Numpy boolean array (3 rows & 4 columns) of random bool values') # Array for random sampling sample_arr = [True, False] # Create a 2D numpy array or matrix of 3 rows & 4 columns with random True or False values bool_arr = np.random.choice(sample_arr, size=(3,4)) print('2D Numpy Array: ') print(bool_arr) print('**** Create a Bool array with all True ****') # Create a Numpy array of 10 True values bool_arr = np.ones(10, dtype=bool) print('Numpy Array: ') print(bool_arr) print('*** Create a Bool array with all False ***') # Create a Numpy array of 10 False values bool_arr = np.zeros(10, dtype=bool) print('Numpy Array: ') print(bool_arr) print('*** Creating 2D Numpy array with all True ***') # Create a 2D Numpy array of 3 rows & 4 columns with all True values bool_arr = np.ones((3,4), dtype=bool) print('2D Numpy Array: ') print(bool_arr) print('*** Creating 2D Numpy array with all False ***') # Create a 2D Numpy array of 3 rows & 4 columns with all False values bool_arr = np.zeros((3,4), dtype=bool) print('2D Numpy Array: ') print(bool_arr) print('**** Converting a List to bool Numpy array ****') # List of integers list_of_elems = [1, 2, 3, 0, 9, 0, 8, 0] # Convert a list of integers to bool array bool_arr = np.array(list_of_elems, dtype=bool) print('Numpy Array: ') print(bool_arr) # heterogeneous List ( contains different type of elements) list_of_elems = [8, 0, 'Hi', '', 0.4] # Convert a heterogeneous list to bool numpy array bool_arr = np.array(list_of_elems, dtype=bool) print(bool_arr) if __name__ == '__main__': main()
Output:
*** Create a Boolean Numpy Array with random boolean values *** Create a 1D Numpy boolean array of size 10 with random value Numpy Array: [False True True False True False True False True True] Create a 2D Numpy boolean array (3 rows & 4 columns) of random bool values 2D Numpy Array: [[ True True True True] [ True True False True] [False True False True]] **** Create a Bool array with all True **** Numpy Array: [ True True True True True True True True True True] *** Create a Bool array with all False *** Numpy Array: [False False False False False False False False False False] *** Creating 2D Numpy array with all True *** 2D Numpy Array: [[ True True True True] [ True True True True] [ True True True True]] *** Creating 2D Numpy array with all False *** 2D Numpy Array: [[False False False False] [False False False False] [False False False False]] **** Converting a List to bool Numpy array **** Numpy Array: [ True True True False True False True False] [ True False True False True]