Faulty numpy.all axis parameter?
I have the following array.
a = np.array([[0, 5, 0, 5],
[0, 9, 0, 9]])
>>>a.shape
Out[72]: (2, 4)
>>>np.all(a,axis=0)
Out[69]:
array([False, True, False, True], dtype=bool)
>>>np.all(a,axis=1)
Out[70]:
array([False, False], dtype=bool)
Since axis 0 means the first axis (row by row) in a 2D array,
I expected, when asked np.all(a,axis=0)
, it checks if the whole element is True or not, for each row.
But it looks like checking for a column causes output as 4 items like array([False, True, False, True], dtype=bool)
.
What am I misunderstanding about the functioning of np.all?
source to share
axis=0
means AND the elements together along the 0- axis , so a[0, 0]
gets ANDed with a[1, 0]
, a[0, 1]
gets ANDed with a[1, 1]
, etc. The specified axis gets destroyed.
You are probably thinking what it takes np.all(a[0])
, np.all(a[1])
etc., by selecting subarrays, indexing along the 0 axis, and doing np.all
for each subarray. This is the opposite of how it works; which will compress every axis but specified.
There are not many advantages with 2D arrays for one convention, but with 3D and higher NumPy choices it is much more convenient.
source to share