Bird
0
0

You have a 2D NumPy array representing exam scores. How can you use np.where() to create a new array where scores below 50 are replaced with 'Fail' and others with 'Pass'?

hard📝 Application Q8 of 15
NumPy - Indexing and Slicing
You have a 2D NumPy array representing exam scores. How can you use np.where() to create a new array where scores below 50 are replaced with 'Fail' and others with 'Pass'?
Anp.where(scores >= 50, 'Fail', 'Pass')
Bnp.where(scores < 50, 'Fail', 'Pass')
Cnp.where(scores == 50, 'Fail', 'Pass')
Dnp.where(scores < 50, scores, 'Pass')
Step-by-Step Solution
Solution:
  1. Step 1: Define condition for failing scores

    Scores below 50 are failing, so condition is scores < 50.
  2. Step 2: Use np.where to assign 'Fail' or 'Pass'

    Use np.where(scores < 50, 'Fail', 'Pass') to create new array with labels.
  3. Final Answer:

    np.where(scores < 50, 'Fail', 'Pass') -> Option B
  4. Quick Check:

    np.where(condition, 'Fail', 'Pass') labels scores [OK]
Quick Trick: Use condition for Fail, else Pass in np.where [OK]
Common Mistakes:
  • Reversing condition
  • Using equality instead of less than
  • Mixing numeric and string outputs incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes