What does axis = 0 do in Numpy's sum function?

Question:

I am learning Python, and have encountered numpy.sum. It has an optional parameter axis. This parameter is used to get either column-wise summation or row-wise summation. When axis = 0 we imply to sum it over columns only. For example,

a = np.array([[1, 2, 3], [4, 5, 6]])
np.sum(a, axis = 0)

This snippet of code produces output: array([5, 7, 9]), fine. But if I do:

a = np.array([1, 2, 3])
np.sum(a, axis = 0)

I get result: 6, why is that? Shouldn’t I get array([1, 2, 3])?

Answers:

All that is going on is that numpy is summing across the first (0th) and only axis. Consider the following:

In [2]: a = np.array([1, 2, 3])

In [3]: a.shape
Out[3]: (3,)

In [4]: len(a.shape) # number of dimensions
Out[4]: 1

In [5]: a1 = a.reshape(3,1)

In [6]: a2 = a.reshape(1,3)

In [7]: a1
Out[7]: 
array([[1],
       [2],
       [3]])

In [8]: a2
Out[8]: array([[1, 2, 3]])

In [9]: a1.sum(axis=1)
Out[9]: array([1, 2, 3])

In [10]: a1.sum(axis=0)
Out[10]: array([6])

In [11]: a2.sum(axis=1)
Out[11]: array([6])

In [12]: a2.sum(axis=0)
Out[12]: array([1, 2, 3])

So, to be more explicit:

In [15]: a1.shape
Out[15]: (3, 1)

a1 is 2-dimensional, the “long” axis being the first.

In [16]: a1[:,0] # give me everything in the first axis, and the first part of the second
Out[16]: array([1, 2, 3])

Now, sum along the first axis:

In [17]: a1.sum(axis=0)
Out[17]: array([6])

Now, consider a less trivial two-dimensional case:

In [20]: b = np.array([[1,2,3],[4,5,6]])

In [21]: b
Out[21]: 
array([[1, 2, 3],
       [4, 5, 6]])

In [22]: b.shape
Out[22]: (2, 3)

The first axis is the “rows”. Sum along the rows:

In [23]: b.sum(axis=0)
Out[23]: array([5, 7, 9])

The second axis are the “columns”. Sum along the columns:

In [24]: b.sum(axis=1)
Out[24]: array([ 6, 15])
Answered By: juanpa.arrivillaga

If someone need this visual description:

numpy axis 0 and axis 1

Answered By: debaonline4u

The axis i in np.sum(a, axis=i) is the ith index of the shape of that array (zero-indexed).

Let’s try and understand what that means with some examples:

a = np.array([1, 2, 3])
print (a.shape) #prints (3,) 
#so axis = 0 corresponds to 3 and axis = 1 corresponds to nothing

Let’s see what axis = 0 and axis = 1 do to the sum:

sum = np.sum(a, axis=0) #sum = 6

So, sum = np.sum(a, axis=0) would sum up all numbers that the 0th index of a.shape refers to, which in this case are 3 numbers. Since numpy arrays are, by default, row-major (which is just another way of saying that the row index is specified before the column index), thus axis=0 would sum the three numbers that the shape refers to.

sum = np.sum(a, axis=1) #gives an error

Similarly, np.sum(a, axis=1) should sum up all the numbers that the 1st index of np.shape refers to, but since there is no first index of the shape, we get an error.

Let’s take another example:

b = np.array([[1,2,3],
             [4,5,6]])
print(b.shape) #prints (2,3)
#axis = 0 corresponds to 2 and axis = 1 corresponds to 3

And now, let’s see what changing the axis does:

sum = np.sum(b, axis=0) #sum = [5, 7, 9] of shape(3,)

We know that axis = 0 should sum along the first index of the shape and we expect it to find two numbers along this axis (by looking at the shape). So [1+4, 2+5, 3+6].

sum = np.sum(b, axis=1) #sum = [6, 15] of shape(2,)

Now the sum is along axis = 1, and from the shape we can see this it is an axis along which there are 3 numbers to be summed. So, [1+2+3,4+5+6]

Answered By: Apoorve

axis is the index of array index.
the array index starts from 0, counting from right to left.

hence,
np.sum(a, axis = 0) means sum along the right most index.

Answered By: Liang

Just for a easier shortcut to np.sum(a, axis) for multidimensional arrays. If you sum along an axis, the resultant shape will have that axis removed. Here’s what I mean;

 a1.shape = (2, 3)
 sum1 = a1.sum(axis=0)
 # sum1.shape will be (3,) since the 0th axis i.e 2 was removed

 sum2 = a1.sum(axis=1)
 # sum2.shape will be (2,) since the 1st axis i.e 3 was removed

From here on you know that given a shape of the form (m, n, p, …), the result when you sum along;

axis 0 is (n, p,...) # removed m
axis 1 is (m, p,...) # removed n
axis 2 is (m, n,...) # removed p

And so on.

Answered By: Anil Myne

axis=0 means other axes are fixed while only axis=0 is dynamic.

a = np.array([[1, 2, 3], [4, 5, 6]])
sum(a[:,0]) => 5
sum(a[:,1]) => 7
sum(a[:,2]) => 9
np.sum(a,axis=0) => [5,7,9]

So get a matrix of shape (3, ) if sum a matrix with shape (2, 3).

a = np.array([[1], [2], [3]])
sum(a[:,0]) => [6]
np.sum(a,axis=0) => [6]

Get a matrix of shape (1, ) if sum a matrix of shape(3, 1).

a = np.array([[1, 2, 3]])
sum(a[:,0]) => 1
sum(a[:,1]) => 2
sum(a[:,2]) => 3
np.sum(a,axis=0) => [1, 2, 3]

Get a matrix of shape (3, ) if sum a matrix of shape(1, 3).

a = np.array([1, 2, 3])
sum(a[:]) => 6
np.sum(a,axis=0) => 6

Get a number if sum a matrix of shape(3, ).

Answered By: hail

axis=0 sums all the column inputs in the array
axis=1 sums all the row inputs in the array

Answered By: Palguna Gopireddy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.