Bird
0
0

You have a large dataset stored as a NumPy array data. You want to compute the mean of each column efficiently. Which approach is best?

hard📝 Application Q15 of 15
SciPy - Integration with Scientific Ecosystem
You have a large dataset stored as a NumPy array data. You want to compute the mean of each column efficiently. Which approach is best?
AUse a for loop to sum each column and divide by number of rows
BUse <code>np.mean(data)</code> without axis parameter
CConvert array to list and use Python's built-in <code>sum</code> and <code>len</code>
DUse <code>np.mean(data, axis=0)</code> to compute means vectorized
Step-by-Step Solution
Solution:
  1. Step 1: Understand mean calculation per column

    Mean per column requires averaging along rows (axis=0).
  2. Step 2: Identify efficient vectorized method

    np.mean with axis=0 computes column means efficiently without loops.
  3. Step 3: Evaluate other options

    Use a for loop to sum each column and divide by number of rows uses slow loops, C converts to list (slow), D computes overall mean, not per column.
  4. Final Answer:

    Use np.mean(data, axis=0) to compute means vectorized -> Option D
  5. Quick Check:

    Vectorized mean per column = np.mean(data, axis=0) [OK]
Quick Trick: Use np.mean with axis=0 for column-wise mean [OK]
Common Mistakes:
  • Using loops instead of vectorized functions
  • Forgetting axis parameter in np.mean
  • Converting arrays to lists unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes