Bird
Raised Fist0
NumPydata~20 mins

np.mean() for average in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Mean Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of np.mean() on 2D array with axis=0
What is the output of this code snippet using np.mean() on a 2D array with axis=0?
NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = np.mean(arr, axis=0)
print(result)
A[2.5 3.5 4.5]
B[2. 3. 4.]
C[3. 4. 5.]
D[1.5 2.5 3.5]
Attempts:
2 left
๐Ÿ’ก Hint
Remember, axis=0 means averaging down the rows for each column.
โ“ data_output
intermediate
1:30remaining
Number of elements in np.mean() output with axis=1
Given a 3x4 numpy array, how many elements does the output have after applying np.mean() with axis=1?
NumPy
import numpy as np
arr = np.arange(12).reshape(3,4)
result = np.mean(arr, axis=1)
print(result)
print(len(result))
A12
B3
C4
D1
Attempts:
2 left
๐Ÿ’ก Hint
Axis=1 means averaging across columns for each row.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in np.mean() usage
What error does this code raise when trying to compute the mean?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = np.mean(arr, axis=1)
print(result)
ATypeError: unsupported operand type(s) for +: 'int' and 'str'
BValueError: could not broadcast input array from shape (3,) into shape (1,)
CAxisError: axis 1 is out of bounds for array of dimension 1
DNo error, prints 2.0
Attempts:
2 left
๐Ÿ’ก Hint
Check the shape of the array and the axis parameter.
๐Ÿง  Conceptual
advanced
1:30remaining
Effect of np.mean() with keepdims=True
What is the shape of the output when applying np.mean() on a 2D array with axis=1 and keepdims=True?
NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = np.mean(arr, axis=1, keepdims=True)
print(result.shape)
A(2, 1)
B(2,)
C(1, 3)
D(3, 2)
Attempts:
2 left
๐Ÿ’ก Hint
Keepdims keeps the reduced dimension as size 1.
๐Ÿš€ Application
expert
2:30remaining
Calculate weighted average using np.mean()
You have values [10, 20, 30] and weights [1, 2, 3]. Which code correctly calculates the weighted average using numpy?
Anp.mean([10, 20, 30], weights=[1, 2, 3])
Bnp.average([10, 20, 30], weights=[1, 2, 3])
Cnp.mean([10, 20, 30]) * np.mean([1, 2, 3])
Dnp.sum(np.array([10, 20, 30]) * np.array([1, 2, 3])) / np.sum([1, 2, 3])
Attempts:
2 left
๐Ÿ’ก Hint
np.mean() does not support weights parameter; use np.average or manual calculation.

Practice

(1/5)
1. What does the function np.mean() do when applied to a list or array?
easy
A. Counts the number of elements in the list or array
B. Finds the maximum value in the list or array
C. Sorts the list or array in ascending order
D. Calculates the average (mean) value of the numbers

Solution

  1. Step 1: Understand the purpose of np.mean()

    The function np.mean() is designed to calculate the average value of numbers in a list or array.
  2. Step 2: Compare with other options

    Other options describe different functions: maximum value, count, or sorting, which are not what np.mean() does.
  3. Final Answer:

    Calculates the average (mean) value of the numbers -> Option D
  4. Quick Check:

    np.mean() = average [OK]
Hint: Mean = sum of values รท number of values [OK]
Common Mistakes:
  • Confusing mean with max or count
  • Thinking it sorts the data
  • Using sum instead of mean
2. Which of the following is the correct syntax for np.mean() to calculate the mean of a NumPy array named arr?
easy
A. np.mean(arr)
B. mean(arr)
C. arr.mean()
D. np.average(arr)

Solution

  1. Step 1: Identify the standard NumPy function for mean

    The correct function to calculate mean in NumPy is np.mean(), which takes the array as an argument.
  2. Step 2: Check other options

    mean(arr) is not defined without importing; arr.mean() is valid but not the function asked; np.average(arr) is similar but not the exact function requested.
  3. Final Answer:

    np.mean(arr) -> Option A
  4. Quick Check:

    Use np.mean(array) for average [OK]
Hint: Use np.mean(array) to get average quickly [OK]
Common Mistakes:
  • Calling mean() without np prefix
  • Using arr.mean() instead of np.mean(arr)
  • Confusing np.mean() with np.average()
3. What is the output of the following code?
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = np.mean(arr, axis=0)
print(result)
medium
A. [1.5 2.5 3.5]
B. [3. 4. 5.]
C. [2.5 3.5 4.5]
D. [3.5 4.5 5.5]

Solution

  1. Step 1: Understand the array and axis parameter

    The array is 2 rows by 3 columns: [[1,2,3],[4,5,6]]. Using axis=0 means calculating the mean down each column.
  2. Step 2: Calculate mean for each column

    Column 1: (1+4)/2 = 2.5, Column 2: (2+5)/2 = 3.5, Column 3: (3+6)/2 = 4.5. So the result is [2.5, 3.5, 4.5].
  3. Step 3: Check options carefully

    [2.5 3.5 4.5] matches the calculated values exactly.
  4. Final Answer:

    [2.5 3.5 4.5] -> Option C
  5. Quick Check:

    Mean along axis=0 averages columns [OK]
Hint: Axis=0 averages columns, axis=1 averages rows [OK]
Common Mistakes:
  • Mixing axis=0 and axis=1
  • Calculating mean of entire array instead of axis
  • Misreading array shape
4. The following code throws an error. What is the problem?
import numpy as np
arr = [1, 2, 3, 4]
result = np.mean(arr, axis=1)
print(result)
medium
A. np.mean() cannot be used on lists
B. The list is 1D, so axis=1 is invalid
C. Missing parentheses in np.mean
D. The variable name 'arr' is reserved

Solution

  1. Step 1: Check the array shape

    The variable arr is a 1D list with shape (4,). Axis 1 does not exist in 1D data.
  2. Step 2: Understand axis parameter

    Axis=1 means operate along columns in 2D or higher arrays. Using axis=1 on 1D data causes an error.
  3. Final Answer:

    The list is 1D, so axis=1 is invalid -> Option B
  4. Quick Check:

    Axis must exist in array shape [OK]
Hint: Check array dimensions before using axis [OK]
Common Mistakes:
  • Using axis parameter on 1D arrays
  • Thinking np.mean can't handle lists
  • Confusing axis indexing
5. You have a 3D NumPy array representing daily temperatures for 2 cities over 3 days with 4 measurements each day:
temps = np.array([
  [[20, 22, 21, 23], [19, 20, 22, 21]],
  [[21, 23, 22, 24], [20, 21, 23, 22]],
  [[22, 24, 23, 25], [21, 22, 24, 23]]
])

Which code correctly calculates the average temperature for each city over all days and measurements?
hard
A. np.mean(temps, axis=(0,2))
B. np.mean(temps, axis=0)
C. np.mean(temps, axis=1)
D. np.mean(temps, axis=2)

Solution

  1. Step 1: Understand the array shape and axes

    The array shape is (3, 2, 4): 3 days, 2 cities, 4 measurements each day.
  2. Step 2: Identify axes to average over

    To get average per city over all days and measurements, we average over days (axis 0) and measurements (axis 2), keeping axis 1 (cities).
  3. Step 3: Use axis=(0,2) in np.mean

    Using np.mean(temps, axis=(0,2)) averages over days and measurements, resulting in average per city.
  4. Final Answer:

    np.mean(temps, axis=(0,2)) -> Option A
  5. Quick Check:

    Average over days and measurements = axis 0 and 2 [OK]
Hint: Use tuple axis to average multiple dimensions [OK]
Common Mistakes:
  • Averaging over wrong axes
  • Using single axis instead of tuple
  • Confusing city axis with day or measurement axis
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard