Numpy Sum Axis Intuition

Aerin Kim
Aerin Kim
Aug 26, 2017 · 2 min read

I’ve always thought that axis 0 is row-wise, and 1 is column-wise.

 row-wise (axis 0) --->  [[ 0  1]   
[ 0 5]]

|

column-wise (axis 1)

However, the result of numpy.sum is the exact opposite of what I was thinking.

>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])

So what’s going on here? Am I the only one who’s wondering this?


The way to understand the “axis” of numpy sum is that it collapses the specified axis. So when it collapses the axis 0 (the row), it becomes just one row (it sums column-wise).


Ok, sure. But why did numpy choose to behave this way?

For 2-d arrays, it might be confusing, however when we talk about 3-d, 4-d, n-d, this is a more straightforward way to define the axis.

# Let's experiment with 3-d array.    
In [5]: x = np.array([[[1,2],[3,4]],[[1,2],[3,4]]])

In [6]: x
Out[6]:
array([[[1, 2],
[3, 4]],

[[1, 2],
[3, 4]]])

In [7]: x.shape
Out[7]: (2, 2, 2)

In [8]: x[0] # axis-0
Out[8]:
array([[1, 2],
[3, 4]])

In [9]: x[1] # still axis-0
Out[9]:
array([[1, 2],
[3, 4]])

In [10]: x[0][0] # axis-1
Out[10]: array([1, 2])

In [11]: x[0][0][0] # axis-2
Out[11]: 1

In [12]: np.sum(x, axis=0) # Notice that it eliminated the specified axis.
Out[12]:
array([[2, 4],
[6, 8]])

In [13]: np.sum(x, axis=1)
Out[13]:
array([[4, 6],
[4, 6]])

In [14]: np.sum(x, axis=2)
Out[14]:
array([[3, 7],
[3, 7]])

Same logic goes for Tensorflow.

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0) # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 1) # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
# tensor t3 with shape [2, 3]
# tensor t4 with shape [2, 3]
tf.shape(tf.concat([t3, t4], 0)) # [4, 3]
tf.shape(tf.concat([t3, t4], 1)) # [2, 6]

If you like my post, could you please clap? It gives me the motivation to write more. :)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store