Boolean array construction
Define an array whose elements use NumPy Boolean storage.
NumPy · Type Alias
np.bool_ is NumPy’s Boolean dtype and scalar type, representing True or False with byte-sized storage. Use it to declare Boolean arrays or convert array data explicitly.
np.bool_ is NumPy's Boolean scalar and dtype class. Use it when constructing Boolean arrays or converting existing data; unlike assigning a dtype attribute directly, conversion with astype expresses an intentional dtype change.
np.bool_A NumPy Boolean scalar or Boolean dtype when used as a dtype specification.
np.bool_.astype for an explicit array conversion to Boolean dtype.import numpy as np
values = np.array([0, 1, 2], dtype=np.bool_)
print(values)
print(values.dtype)
print(type(values[1]))Creates an array with NumPy's Boolean dtype and displays its values, dtype, and scalar element type.
Define an array whose elements use NumPy Boolean storage.
Convert numeric or other array data into Boolean values.
Specify a NumPy Boolean dtype in APIs that accept dtype objects.
np.bool_?np.bool_ is NumPy's Boolean type, representing True or False in a byte-sized NumPy dtype. It is available as a NumPy dtype and scalar type.
Follow-up: How would you convert an existing array? Use array.astype(np.bool_) when creating a converted array.
bool relate to np.bool_?Python's built-in bool can be used as a dtype and NumPy maps it to np.bool_. Explicitly naming np.bool_ makes the NumPy dtype choice clear.
Follow-up: What should be inspected to determine an array's type? Check the array's dtype attribute.
np.bool_ commonly applied to arrays?Use np.bool_ in array construction or with astype to obtain Boolean storage. NumPy dtypes are objects with their own characteristics, so the resulting array exposes that dtype through dtype.
Follow-up: What alternative does the documentation prefer for changing an array's dtype representation? Use astype rather than mutating the dtype attribute.
numpy.dtypenumpy.ndarray.astypenumpy.ndarray.viewbool