0

I am reading the mxnet tutorial on NDarray part and I am confused about the use of sum_axis function and the example is :

>>> a = mx.nd.ones((2,3))
>>> c = mx.nd.sum_axis(a, axis=1)
>>> c.asnumpy()
    array([ 3.,  3.], dtype=float32)
>>> c = mx.nd.sum_axis(a, axis=0)
>>> c.asnumpy()
    array([ 2.,  2.,  2.], dtype=float32)

What I am wondering is when the value of paramter axis is 1, I think it should output

array([ 2.,  2.,  2.], dtype=float32)

but not

array([ 3.,  3.], dtype=float32)

As when the value of paramter axis is 1, I think the sum_axis should compute the sum along the column ,but the result shows it compute the sum along the rows.

And it seems that numpy also compute like this and I really don't understand why this way. So anyone could explain this ?

Thanks!!

ningyuwhut
  • 609
  • 12
  • 27

1 Answers1

1

Numpy has described what axis is in 2D array. A 2-dimensional array has two corresponding axes: the first running vertically downwards across rows (axis 0), and the second running horizontally across columns (axis 1).

Check the link for example https://docs.scipy.org/doc/numpy-1.12.0/glossary.html This is also true for MXNet. So, in your example mentioned above: a = [[ 1., 1., 1.], [ 1., 1., 1.]]

axis 0 means, going vertically downwards. So, it will give you output [2., 2., 2.] and axis 1 means, going horizontally across columns. So, it will give you output [3., 3.]

  • Thanks! That's what I want. But I still have a question that what if there are more than 2 axes. – ningyuwhut Jun 06 '17 at 12:10
  • I think this visual representation and slicing example will help you understand it in more detail. https://stackoverflow.com/questions/40857930/how-does-numpy-sum-with-axis-work – Roshani Nagmote Jun 06 '17 at 18:18