0
0
NumPydata~5 mins

np.sum() and axis parameter in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the np.sum() function do in NumPy?

np.sum() adds up all the numbers in an array. It can add all elements or add along a specific direction (axis).

Click to reveal answer
beginner
What is the purpose of the axis parameter in np.sum()?

The axis parameter tells np.sum() which direction to add numbers along. For example, axis=0 adds down columns, axis=1 adds across rows.

Click to reveal answer
beginner
If you have a 2D array, what does np.sum(array, axis=0) do?

It adds the numbers down each column and returns a 1D array with sums for each column.

Click to reveal answer
beginner
What happens if you call np.sum() without specifying the axis?

It adds all the numbers in the whole array and returns a single number.

Click to reveal answer
beginner
Explain axis=1 in np.sum() for a 2D array.

axis=1 means add numbers across each row. The result is a 1D array with the sum of each row.

Click to reveal answer
What does np.sum(array, axis=0) do for a 2D array?
AAdds numbers across each row
BAdds numbers down each column
CAdds all numbers in the array
DSubtracts numbers in each column
If you want to add all elements in a NumPy array, what should you do?
AUse <code>np.sum(array, axis=0)</code>
BUse <code>np.sum(array, axis=1)</code>
CUse <code>np.mean(array)</code>
DUse <code>np.sum(array)</code> without axis
What does axis=1 mean in np.sum() for a 2D array?
AAdd across rows
BAdd down columns
CAdd all elements
DMultiply elements
What type of result does np.sum() return when you specify an axis?
AA list of original numbers
BA single number
CAn array with sums along that axis
DA boolean value
Which axis sums down the columns in a 2D NumPy array?
Aaxis=0
Baxis=1
Caxis=2
Daxis=-1
Describe how the axis parameter changes the behavior of np.sum() when working with 2D arrays.
Think about adding numbers in rows vs columns.
You got /4 concepts.
    Explain what happens when you use np.sum() on a 3D array with axis=1.
    Imagine stacking multiple 2D arrays and summing across the middle dimension.
    You got /4 concepts.