Bird
0
0

Given a 3D NumPy array temps with shape (3, 2, 4) representing temperatures for 3 days, 2 cities, and 4 measurements per day, how do you compute the average temperature per city over all days and measurements?

hard📝 Application Q8 of 15
NumPy - Aggregation Functions
Given a 3D NumPy array temps with shape (3, 2, 4) representing temperatures for 3 days, 2 cities, and 4 measurements per day, how do you compute the average temperature per city over all days and measurements?
Anp.mean(temps, axis=(0, 2))
Bnp.mean(temps, axis=1)
Cnp.mean(temps, axis=(1, 2))
Dnp.mean(temps, axis=2)
Step-by-Step Solution
Solution:
  1. Step 1: Understand array shape

    Shape (3, 2, 4): days, cities, measurements.
  2. Step 2: Average per city over days and measurements

    Average over axis 0 (days) and axis 2 (measurements).
  3. Final Answer:

    np.mean(temps, axis=(1, 2)) -> Option C
  4. Quick Check:

    Axes 1 and 2 averaged, leaving city axis 0 [OK]
Quick Trick: Average over days and measurements axes [OK]
Common Mistakes:
  • Averaging over wrong axes
  • Using single axis instead of tuple for multiple axes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes