Let’s learn how to convert a Numpy array to a boolean value. We will use the Numpy astype method for that purpose.
Making use of the atype method
Having an example array, we are able to convert dtype to boolean thanks to the astype function. Just put dtype=boolean as an argument like in the example below.
import numpy as np my_array = np.array([1, 0, 1, 5, 0, 1]) print(f"My array is: \n{my_array}") my_array = my_array.astype(dtype=bool) print(f"My boolean array is: \n{my_array}")
As you may notice, zeros got changed to 0 and other numbers to 1.