Bird
0
0

You have a list of lists representing daily temperatures for a week:

hard📝 Application Q15 of 15
NumPy - Creating Arrays
You have a list of lists representing daily temperatures for a week:
temps = [[20, 22, 19], [21, 23, 20], [19, 21, 18], [22, 24, 21], [20, 22, 19], [18, 20, 17], [21, 23, 20]]

How can you convert this into a numpy array and find the average temperature for each day?
Aimport numpy as np temps_arr = np.array(temps) daily_avg = temps_arr.mean(axis=1) print(daily_avg)
Bimport numpy as np temps_arr = np.array(temps) daily_avg = temps_arr.mean(axis=0) print(daily_avg)
Cimport numpy as np daily_avg = np.array(temps).sum() print(daily_avg)
Dimport numpy as np temps_arr = np.array(temps) daily_avg = temps_arr.max(axis=1) print(daily_avg)
Step-by-Step Solution
Solution:
  1. Step 1: Convert list of lists to numpy array

    Use np.array(temps) to create a 2D array where each row is a day.
  2. Step 2: Calculate average temperature per day

    Use mean(axis=1) to compute average across columns (hours) for each row (day).
  3. Final Answer:

    import numpy as np temps_arr = np.array(temps) daily_avg = temps_arr.mean(axis=1) print(daily_avg) -> Option A
  4. Quick Check:

    mean(axis=1) averages rows (days) [OK]
Quick Trick: Use mean(axis=1) to average rows (days) [OK]
Common Mistakes:
  • Using mean(axis=0) which averages columns (hours)
  • Using sum() without axis for average
  • Using max() instead of mean()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes