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,

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,

Output:

How did it work?

First we create a bool array with only 2 values i.e. True & false,

Then we passed this array to numpy.random.choice() along with argument size=10,

This function generates a 10 random elements based on the values in sample_arr i.e. either True or False,

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,

Output:

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,

Output:

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,

Output:

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,

Output:

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,

Output:

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

Output:

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,

Output:

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,

Output:

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.