I have this array (let's call it my_array) which comes from applying numpy.any:
my_array
array([[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
...,
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True]])
Is there a way to convert this array to a ndarray? I mean, how can I see this:
array([[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
...,
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True]],dtype=bool)
CodePudding user response:
When you show something like that to the IDE, it uses repr(obj)
, which creates a string representation of the object. Ideally this also means eval(repr(object)) == object
. The reason why you don't see the dtype=bool
part is that an array comprised fully of booleans is unambiguously boolean. Likewise, an integer array is by default of dtype int32, so that is also excluded. But if you have an integer of some other type, then it will be shown.
>>> np.array([1, 2])
array([1, 2])
>>> np.array([1, 2]).dtype
dtype('int32')
>>> np.array([1, 2], dtype=np.uint8)
array([1, 2], dtype=uint8)
Your result is already in the output you want. And the docs for numpy.any confirm that:
Returns
-------
any : bool or ndarray
A new boolean or `ndarray` is returned unless `out` is specified,
in which case a reference to `out` is returned.
But if you want to make sure everything is right, you can check type(my_array)
and my_array.dtype
(assuming the object is a numpy array).
>>> a = np.any([[1, 2], [3, 4]], axis=0)
>>> a
array([ True, True])
>>> type(a)
<class 'numpy.ndarray'>
>>> a.dtype
dtype('bool')