Python Boolean array in NumPy

In this post, I will be writing about how you can create boolean arrays in NumPy and use them in your code.

Overview

Boolean arrays in NumPy are simple NumPy arrays with array elements as either ‘True’ or ‘False’. Other than creating Boolean arrays by writing the elements one by one and converting them into a NumPy array, we can also convert an array into a ‘Boolean’ array in some easy ways, that we will look at here in this post.
In this process, all elements other than 0, None and False all are considered as True.

Boolean Array using dtype=’bool’ in NumPy – Python

Let’s take an example:

import numpy as np
import random

array = []
for _ in range(10):
    num = random.randint(0,1)
    array.append(num)
print(f'Original Array={array}')  # prints the original array with 0's and 1's
nump_array = np.array(array,dtype='bool')
print(f'numpy boolean array:{nump_array}')  # prints the converted boolean array

Here the output will look somewhat like this:
output:
Python Boolean array in NumPy

Boolean Array using comparison in NumPy

Example:

import numpy as np
import random

array = np.arange(10,30)
print('1st array=',array,'\n')
array_bool = array > 15
print(f'First boolean array by comparing with an element:\n{array_bool}\n\n')

array_2 = [random.randint(10,30) for i in range(20)]  # second array using list comprehension
print(f'Second array:\n{array_2}')
array2_bool = array_2 > array
print(f'second boolean array by comparing second array with 1st array:\n{array2_bool}')

In the above piece of code, I have formed the ‘array’ is created using numpy.arrange() function. And the elements are from 10 to 30 (20 elements).
Now form the boolean array (array_bool) by comparing it with 15 if the elements are greater than 15 they are noted as True else False.

The second array is created using simple, ‘List comprehension’ technique. And of the same length as the ‘array’ and elements are random in the range 10 to 30(inclusive). Now the second boolean array is created using comparison between the elements of the first array with the second array at the same index.

Output:
Boolean Array using comparison in NumPy

**Note: This is known as ‘Boolean Indexing’ and can be used in many ways, one of them is used in feature extraction in machine learning. Or simply, one can think of extracting an array of odd/even numbers from an array of 100 numbers.

Converting to numpy boolean array using .astype(bool)

For example, there is a feature array of some images, and you want to just store the bright pixels and eliminate the dark pixels(black=0). You can do this by converting the pixels array to boolean and use the Boolean array indexing to eliminate the black pixels!
Example:

import numpy
import random

random.seed(0)
arr_1 = [random.randint(0,1) for _ in range(20)]
print(f'Original Binary array:\n{arr_1}\n')
arr_bool = numpy.array(arr_1).astype(bool)
print(f'Boolean Array:\n{arr_bool}')

Output:
Converting to numpy boolean array using .astype(bool)

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *