How to create a numpy array of all True or all False?
In Python, how do I create a numpy array of arbitrary shape filled with all True or all False?
numpy already allows the creation of arrays of all ones or all zeros very easily:
numpy.ones((2, 2)) or
False are represented in Python as
0, respectively, we have only to specify this array should be boolean using the optional
dtype parameter and we are done.
numpy.ones((2, 2), dtype=bool)
array([[ True, True], [ True, True]], dtype=bool)
UPDATE: 30 October 2013
Since numpy version 1.8, we can use
full to achieve the same result with syntax that more clearly shows our intent (as fmonegaglia points out):
numpy.full((2, 2), True, dtype=bool)
UPDATE: 16 January 2017
Since at least numpy version 1.12,
full automatically casts results to the
dtype of the second parameter, so we can just write:
numpy.full((2, 2), True)