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? 回答1: numpy already allows the creation of arrays of all ones or all zeros very easily: e.g. numpy.ones((2, 2)) or numpy.zeros((2, 2)) Since True and False are represented in Python as 1 and 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) returns: array([[ True, True], [ True, True]], dtype=bool)