2

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?

  • Does this answer your question? [How does the axis parameter from NumPy work?](https://stackoverflow.com/questions/22320534/how-does-the-axis-parameter-from-numpy-work) – Joe May 12 '20 at 12:49
  • The role of `axis` in `sum` (and other 'reduce' functions) can be confusing when working with a 2d array. It's easier to visualize when working with a 1d or 3d. With 3d, `sum` on one axis leaves the other two. `delete` is not a reduction function, so won't help in understanding `sum`. – hpaulj May 12 '20 at 16:05

1 Answers1

2

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.

jfaccioni
  • 7,099
  • 1
  • 9
  • 25
  • So basically it means python reads np.sum(arr,0)[1] as sum of 2nd element of each row in arr and np.delete(arr,1,0) as delete the second row in arr. – suraj rawat May 12 '20 at 12:57
  • More specifically, it reads it as "add all of the elements of each row, and then show me what's the second value for the resulting array" (corresponds to the second column of the original array). – jfaccioni May 12 '20 at 12:58