In case someone needs this visual description:

In case someone needs this visual description:

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])
Not terribly new to Python or programming in general, but I'm looking at some initial NumPy exercises for Introduction to Statistical Learning with Applications in Python and I'm rather confused about the axis parameter to some methods.
Here's the relevant supporting code from the ISLP Lab Notebook:
rng = np.random.default_rng(3)
X = rng.standard_normal((10, 3))
# array([[ 0.22578661, -0.35263079, -0.28128742],
# [-0.66804635, -1.05515055, -0.39080098],
# [ 0.48194539, -0.23855361, 0.9577587 ],
# [-0.19980213, 0.02425957, 1.54582085],
# [ 0.54510552, -0.50522874, -0.18283897],
# [ 0.54052513, 1.93508803, -0.26962033],
# [-0.24355868, 1.0023136 , -0.88645994],
# [-0.29172023, 0.88253897, 0.58035002],
# [ 0.0915167 , 0.67010435, -2.82816231],
# [ 1.02130682, -0.95964476, -1.66861984]])
X.mean(axis=0)
# array([ 0.15030588, 0.14030961, -0.34238602])The accompanying MD text here says:
The np.mean(), np.var(), and np.std() functions can also be applied to the rows and columns of a matrix. To see this, we construct a matrix of random variables, and consider computing its row sums.
Since arrays are row-major ordered, the first axis, i.e. axis=0, refers to its rows. We pass this argument into the mean() method for the object X.
I know axis=0 is rows and axis=1 is columns (for this example, at least). Since the example is passing axis 0 (rows) to the mean method, I would expect an output array of length 10 that calculates the mean of 3 elements in each row.
But the example appears to be calculating the mean of 10 elements in each of 3 columns, given the output array of length 3.
Further, when I calculate the column mean using X.mean(axis=1), I get the result I expected for axis=0, so the outputs are being switched.
The NumPy documentation didn't provide any additional clarity.
Hoping someone can provide an explanation for what's going on here. Is there possibly a setting (perhaps embedded in the notebook, that I can't see) that allows axes to be switched, where rows are axis=1 and columns are axis=0?
Thanks in advance for any help!
I think the right way to interpret the axis parameter is what axis you sum 'over' (or 'across'), rather than the 'direction' the sum is computed in. Specifying axis = 0 computes the sum over the rows, giving you a total for each column; axis = 1 computes the sum across the columns, giving you a total for each row.
I was a reading the source code in pandas project, and I think that this come from Numpy, in this library is used in that way(0 sum vertically and 1 horizonally), and additionally Pandas use under the hood numpy in order to make this sum.
In this link you could check that pandas use numpy.cumsum function to make the sum.
And this link is for numpy documentation.
If you are looking a way to remember how to use the axis parameter, the 'anant' answer, its a good approach, interpreting the sum over the axis instead across. So when is specified 0 you are computing the sum over the rows(iterating over the index in order to be more pandas doc complaint). When axis is 1 you are iterating over the columns.