How axis are defined in 2d matrix in numpy array?

Viewed 300

I have recently learned NumPy array and I am confused axis=0 and axis =1 represent? I have searched on the internet and got that axis=0 represent rows and axis=1 represent columns but when I start doing some practice I got confused about how axis work differently on two different function np.delete() and np.sum()

 #input

import numpy as np
arr = np.array([(1,2,3,4),(5,6,7,8),(9,10,11,12)])
print(arr)
print(np.sum(arr,0)[1])

#output

 [[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]
 18    

if axis=0 represent row then it should add 2nd row(row of index 1 ) i.e 5+6+7+8

but instead, it is adding 2nd column i.e 2+6+10

but when I use np.delete()

   #input

   print(np.delete(arr,2,0))

   #output

  [[1 2 3 4]
   [5 6 7 8]]

here it is deleting 3rd row(row of index 2).

In the first case, axis=0 is working as a column and in the second it is working as row

can you tell me where am I wrong?

1 Answers

This is a matter of perspective on how you think about row/column operations. Look at the following diagram (source):

enter image description here

According to numpy.sum, the axis argument represents the "axis or axes along which a sum is performed". Writing numpy.sum(arr, axis=0) translates to add numbers along each row (0th axis). The result is a sum of each column, but the operation is performed row-wise - for each row.

The docs for numpy.delete state that the axis argument represents "the axis along which to delete the subarray". Writing numpy.delete(arr, 2, axis=0) means delete the third element (index=2) from the perspective of the rows (0th axis).

It's not a matter of inconsistency: both numpy.sum and numpy.delete with axis=0 act over rows (0th index). The first sums the elements across all rows, while the latter removes elements of a specific row.

Related