Bird
0
0

Given a numpy array data = np.array([5, 10, 15, 20, 25]), how can you use a boolean mask to select only values greater than 12 and less than 22?

hard📝 Application Q15 of 15
NumPy - Array Data Types
Given a numpy array data = np.array([5, 10, 15, 20, 25]), how can you use a boolean mask to select only values greater than 12 and less than 22?
Amask = (data > 12) & (data < 22); result = data[mask]
Bmask = data > 12 or data < 22; result = data[mask]
Cmask = data > 12 and data < 22; result = data[mask]
Dmask = (data > 12) | (data < 22); result = data[mask]
Step-by-Step Solution
Solution:
  1. Step 1: Create correct boolean mask with logical AND

    To select values greater than 12 and less than 22, use element-wise AND: (data > 12) & (data < 22).
  2. Step 2: Apply mask to data array

    Use the mask to index data: data[mask] returns values meeting both conditions.
  3. Final Answer:

    mask = (data > 12) & (data < 22); result = data[mask] -> Option A
  4. Quick Check:

    Use '&' for element-wise AND in numpy [OK]
Quick Trick: Use '&' for AND, '|' for OR in numpy boolean masks [OK]
Common Mistakes:
  • Using 'and' or 'or' instead of '&' or '|'
  • Forgetting parentheses around conditions
  • Using '|' (OR) instead of '&' (AND) for both conditions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes