In case someone needs this visual description:

Answer from Debashis Sahoo on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-sum-in-python
numpy.sum() in Python - GeeksforGeeks
January 30, 2026 - np.sum(arr, axis=1, keepdims=True) preserves the reduced dimension, returning a column-shaped result. Comment · Python Fundamentals · Introduction1 min read · Input & Output2 min read · Variables4 min read · Operators4 min read · Keywords2 ...
🌐
Medium
medium.com › intuitionmath › numpy-sum-axis-intuition-6eb94926a5d1
Numpy Sum Axis Intuition
March 6, 2023 - >>> np.sum([[0, 1], [0, 5]], axis=0) array([0, 6]) >>> np.sum([[0, 1], [0, 5]], axis=1) array([1, 5])
🌐
Medium
medium.com › @whyamit101 › understanding-numpy-sum-with-axis-parameter-1fc543fe9fa2
Understanding numpy.sum() with Axis Parameter | by why amit | Medium
February 9, 2025 - Think of rows as horizontal lines in your array. When you sum along axis=1, you’re adding up all the elements in each row, one at a time.
🌐
Reddit
reddit.com › r/learnpython › numpy axis confusion
r/learnpython on Reddit: NumPy Axis Confusion
December 19, 2024 -

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!

Top answer
1 of 1
2
If you have the following: import numpy as np vec = np.array([1, 2, 3, 4]) row = np.array([[1, 2, 3, 4]]) col = np.array([[1], [2], [3], [4]]) mat = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) book = np.array([[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]], [[1.1, 2.1, 3.1, 4.1], [5.1, 6.1, 7.1, 8.1]], [[1.2, 2.2, 3.2, 4.2], [5.2, 6.2, 7.2, 8.2]]]) The shape tuples are: > vec.shape (4 columns, ) >row.shape (1 row, 4 columns) > col.shape (4 rows, 1 column) > mat.shape (2 rows, 4 columns) > book.shape (3 sheets, 2 rows, 4 columns) axis 0, is element at index 0 in the shape tuple. axis=0 is columns for a 1d array, rows for a 2d array, sheets for a 3d array and relates to the outer []. axis 1, is element at index 1 in the shape tuple. axis=1 is columns for a 2d array, rows for a 2d array. Instead of approaching the shape tuple from left to right, approach the shape tuple from right to left. The last element axis=-1 is always columns, axis=-2 is always rows, axis=-3 is always sheets and so on... If we look at mat and think of the axis parameter as the following arrows: axis=-1 → operates along column axis np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) axis=-2 ↓ np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) An operation along axis=-1, operates along columns and therefore returns a column: > mat.sum(axis=-1, keepdims=True) array([[10], [26]]) An operation along axis=-2, operates along rows and therefore returns a row: mat.sum(axis=-2, keepdims=True) > array([[ 6, 8, 10, 12]]) I've put a bit more detail in this markdown tutorial here covering the axis parameter and dimensionality of ndarrays. GitHub: numpy library: axis, shape and negative index . I'm still working on this tutorial but the section that covers the axis parameter should be helpful if you need a bit more information.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.matrix.sum.html
numpy.matrix.sum — NumPy v2.1 Manual
Returns the sum of the matrix elements, along the given axis. Refer to numpy.sum for full documentation. See also · numpy.sum · Notes · This is the same as ndarray.sum, except that where an ndarray would be returned, a matrix object is returned instead. Examples · >>> x = np.matrix([[1, ...
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.matrix.sum.html
numpy.matrix.sum — NumPy v2.2 Manual
Returns the sum of the matrix elements, along the given axis. Refer to numpy.sum for full documentation. See also · numpy.sum · Notes · This is the same as ndarray.sum, except that where an ndarray would be returned, a matrix object is returned instead. Examples · >>> x = np.matrix([[1, ...
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.sum.html
numpy.sum — NumPy v2.5 Manual
Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Meaning of the axis parameter (0, 1, -1) | note.nkmk.me
January 18, 2024 - In a two-dimensional array, axis=0 operates column-wise, and axis=1 operates row-wise. For example, use np.sum() to calculate the sum.
🌐
Sharp Sight
sharpsight.ai › blog › numpy-sum
How to Use the Numpy Sum Function - Sharp Sight
February 6, 2024 - So for example, if we set axis = 0, we are indicating that we want to sum up the rows. Remember, axis 0 refers to the row axis. Likewise, if we set axis = 1, we are indicating that we want to sum up the columns.
🌐
AskPython
askpython.com › python › examples › numpy-sum
NumPy Sum - A Complete Guide - AskPython
November 19, 2022 - columns sum = np.sum(a, dtype=float, axis=0) print("a =", a) print("Sum of the array =", sum) ... import numpy as np a = [[3, 12, 4], [3, 5, 1]] # sum along axis=1 i.e.
🌐
Codecademy
codecademy.com › docs › python:numpy › ndarray › .sum()
Python:NumPy | ndarray | .sum() | Codecademy
October 31, 2025 - Here, axis=0 sums each column, and axis=1 sums each row. In this example, the .sum() method demonstrates how to use an initial value for the sum and how to include elements using the where parameter: ... Note: For integer arrays, large sums ...
🌐
W3Schools
w3schools.com › python › numpy › numpy_ufunc_summations.asp
NumPy ufuncs - Summations
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([1, 2, 3]) newarr = np.sum([arr1, arr2], axis=1) print(newarr) Try it Yourself »
🌐
SciPy
docs.scipy.org › doc › numpy-1.9.1 › reference › generated › numpy.sum.html
numpy.sum — NumPy v1.10 Manual
October 18, 2015 - Sum of array elements over a given axis. ... Equivalent method. ... Cumulative sum of array elements. ... Integration of array values using the composite trapezoidal rule. ... Arithmetic is modular when using integer types, and no error is raised on overflow. The sum of an empty array is the neutral element 0: ... >>> np.sum([0.5, 1.5]) 2.0 >>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32) 1 >>> np.sum([[0, 1], [0, 5]]) 6 >>> np.sum([[0, 1], [0, 5]], axis=0) array([0, 6]) >>> np.sum([[0, 1], [0, 5]], axis=1) array([1, 5])
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › python-pandas-dataframe-sum
Pandas dataframe.sum() - GeeksforGeeks
July 11, 2025 - Explanation: This code creates ... adds all values in each column separately. When summing along rows (axis=1), it adds values in each row....
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Sum, mean, max, min for entire array, column/row-wise | note.nkmk.me
January 20, 2024 - a = np.arange(12).reshape(3, 4) print(a) # [[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11]] print(np.sum(a)) # 66 ... Setting axis=0 returns the sum for each column (column-wise), and axis=1 for each row (row-wise).