0
0
NumPydata~5 mins

np.mean() for average in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.mean() do in numpy?

np.mean() calculates the average value of numbers in an array. It adds all numbers and divides by how many there are.

Click to reveal answer
beginner
How do you calculate the average of a list [2, 4, 6, 8] using np.mean()?

Use np.mean([2, 4, 6, 8]). It returns 5.0 because (2+4+6+8)/4 = 20/4 = 5.

Click to reveal answer
intermediate
What is the difference between np.mean() and Python's built-in sum()/len() for average?

np.mean() works directly on numpy arrays and is faster for large data. sum()/len() works on lists but is slower and less flexible.

Click to reveal answer
intermediate
How can you calculate the average along a specific axis in a 2D numpy array using np.mean()?

Use the axis parameter. For example, np.mean(arr, axis=0) averages columns, np.mean(arr, axis=1) averages rows.

Click to reveal answer
advanced
What happens if you use np.mean() on an empty array?

It returns nan (not a number) and usually shows a warning because you cannot find an average of no numbers.

Click to reveal answer
What does np.mean() calculate?
AThe average of numbers
BThe sum of numbers
CThe maximum number
DThe minimum number
How do you calculate the average of each row in a 2D numpy array arr?
Anp.mean(arr, axis=0)
Bnp.mean(arr, axis=1)
Cnp.mean(arr)
Dnp.mean(arr, axis=2)
What is the output of np.mean([10, 20, 30])?
A10
B30
C20
D60
Which data type does np.mean() work best with?
APython lists only
BDictionaries
CStrings
DNumpy arrays
What does np.mean([]) return?
Anan
B0
CError
D1
Explain how to use np.mean() to find the average of numbers in a numpy array and how to specify the axis parameter.
Think about how you find average in a list and how axis changes that in 2D arrays.
You got /4 concepts.
    Describe what happens when you use np.mean() on an empty array and why.
    Consider what average means and what happens if there are no numbers.
    You got /4 concepts.