Python : Create boolean Numpy array with all True or all False or random boolean values

How to create boolean Numpy array with all True or all False or random boolean values in Python ?

This article is about creating boolean Numpy array with all True or all False or random boolean values. Here we will discuss various ways of creating boolean Numpy array. So let’s start the topic.

Approach-1 : Creating 1-D boolean Numpy array with random boolean values

Python’s Numpy module provide random.choice( ) function which will create a boolean Numpy array with some random values.

Syntax : numpy.random.choice(a, size=None, replace=True, p=None)

where,

  • a: A Numpy array from which random values will be generated
  • size: Shape of array that is to b generated
  • replace: Here the sample is with or without replacement

To create a 1-D Numpy array with random true and false values, we will initially take a bool array and pass the array to numpy.random.choice() with some size which will generate a array with random true and false values.

So, let’s see how it actually works.

#Program :

import numpy as sc

# bool Array for random sampling with only 2 values i.e. True anf False
sample_Barr = [True, False]

# To create a numpy array with random true and false with size 5
bool_Narr = sc.random.choice(sample_Barr, size=5)

print('Numpy Array with random values: ')
print(bool_Narr)
Output :
Numpy Array with random values:
[False False False  True False]

Approach-2 : Creating 2-D boolean Numpy array with random values :

For implementing this we can pass the size of 2D array as tuple in random.choice( ) function.

So, let’s see how it actually works.

#Program :

import numpy as sc

# bool Array for random sampling with only 2 values i.e. True anf False
sample_Barr = [True, False]

# To create a 2D numpy array with random true and false values
bool_Narr = sc.random.choice(sample_Barr, size=(3,3))

print('2D Numpy Array with random values: ')
print(bool_Narr)
Output :
2D Numpy Array with random values:
[[False False False]
[False  True  True]
[ True False  True]]

Approach-3 : Create a 1-D Bool array with all True values :

We can use numpy.ones( ) function to form boolean Numpy array with all True values. nump.ones( ) creates a numpy array with initializing value with 1, later dtype argument is passed as bool which converts all 1 to True.

So, let’s see how it actually works.

#Program :

import numpy as sc

# To create a 1D numpy array with all true values
bool_Narr = sc.ones(5, dtype=bool)

print('1D Numpy Array with all true values: ')
print(bool_Narr)

Output :
1D Numpy Array with all true values:
[ True  True  True  True  True]

Approach-4 : Create a 1-D Bool array with all False values :

We can use numpy.zeros( ) function to form boolean Numpy array with all True values. nump.zeros( ) creates a numpy array with initializing value with 0, later dtype argument is passed as bool which converts all 0 to False.

So, let’s see how it actually works.

# Program :

import numpy as sc

# To create a 1D numpy array with all false values
bool_Narr = sc.zeros(5, dtype=bool)

print('1D Numpy Array with all false values: ')
print(bool_Narr)
Output :
1D Numpy Array with all false values:
[False False False False False]

Approach-5 :  Creating 2-D Numpy array with all True values :

Here also numpy.ones( ) including passing the dtype argument as bool can be used to generate all values as true in a 2D  numpy array.

So, let’s see how it actually works.

# Program :

import numpy as sc

# To create a 2D numpy array with all true values
bool_Narr = sc.ones((3,4), dtype=bool)

print('2D Numpy Array with all true values: ')
print(bool_Narr)
Output :
2D Numpy Array with all true values:
[[ True  True  True  True]
[ True  True  True  True]
[ True  True  True  True]]

Approach-6 :  Creating 2D Numpy array with all False values :

Here also numpy.zeros( ) including passing the dtype argument as bool can be used to generate all values as false in a 2D  numpy array.

So, let’s see how it actually works.

#Program :

import numpy as sc

# To create a 2D numpy array with all true values
bool_Narr = sc.zeros((3,4), dtype=bool)

print('2D Numpy Array with all true values: ')
print(bool_Narr)
Output :
2D Numpy Array with all false values:
[[False False False False]
[False False False False]
[False False False False]]

Converting a List to bool Numpy array :

Approach-7 : Convert a list of integers to boolean numpy array :

Here we will pass dtype arguement as bool in numpy.array( ) function where each and every elements in the list will be converted as true/ false values, i.e. all 0s will be converted to false and any integer other than 0 to true.

So, let’s see how it actually works.

#Program :

import numpy as sc

# Integers list
list_ele = [8,55,0,24,100,0,0,-1]

# To convert a list of integers to boolean array
bool_Narr = sc.array(list_ele, dtype=bool)

print('Boolean Numpy Array: ')
print(bool_Narr)
Output :
Numpy Array:
[ True  True  False  True  True  False False  True]

Approach-8 : Convert a heterogeneous list to boolean numpy array :

In python, list can contain elements with different data types i.e. heterogeneous in nature. But Numpy arrays are homogeneous in nature i.e. elements with same data type, so it will convert all 0s to false and any other values which can be of any data type to true.

So, let’s see how it actually works.

#Program :

import numpy as sc

# Integers list
list_ele = [8,55,0,24.56,100,0,-85.6,"Satya"]

# To convert a list of integers to boolean array
bool_Narr = sc.array(list_ele, dtype=bool)

print('Boolean Numpy Array: ')
print(bool_Narr)
Output :
Boolean Numpy Array:
[ True  True  False  True  True False  True  True]