Challenge - 5 Problems
np.any() and np.all() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of np.any() on a 2D array
What is the output of this code snippet?
NumPy
import numpy as np arr = np.array([[False, False], [False, True]]) result = np.any(arr) print(result)
Attempts:
2 left
💡 Hint
np.any() returns True if any element in the array is True.
✗ Incorrect
The array contains one True value. np.any() checks if any element is True and returns a single boolean.
❓ Predict Output
intermediate1:30remaining
Using np.all() with axis parameter
What is the output of this code?
NumPy
import numpy as np arr = np.array([[True, False, True], [True, True, True]]) result = np.all(arr, axis=1) print(result)
Attempts:
2 left
💡 Hint
np.all() with axis=1 checks if all elements in each row are True.
✗ Incorrect
First row has a False, so result is False. Second row all True, so result is True.
❓ data_output
advanced1:30remaining
Result shape after np.any() with axis
Given this array, what is the shape of the result after applying np.any(arr, axis=0)?
NumPy
import numpy as np arr = np.array([[True, False, True], [False, False, True], [True, True, False]]) result = np.any(arr, axis=0) print(result.shape)
Attempts:
2 left
💡 Hint
np.any with axis=0 reduces rows, keeping columns dimension.
✗ Incorrect
Original shape is (3,3). Applying np.any with axis=0 returns one boolean per column, shape (3,).
🔧 Debug
advanced1:30remaining
Identify the error in np.all() usage
What error will this code raise?
NumPy
import numpy as np arr = np.array([1, 2, 3]) result = np.all(arr, axis=1) print(result)
Attempts:
2 left
💡 Hint
Check the dimension of the array and the axis parameter.
✗ Incorrect
The array is 1D, so axis=1 is invalid and causes AxisError.
🚀 Application
expert2:30remaining
Filter rows where all values are positive
Given this 2D array, which code snippet correctly filters rows where all values are positive?
NumPy
import numpy as np arr = np.array([[1, 2, 3], [-1, 5, 6], [4, 0, 7], [8, 9, 10]])
Attempts:
2 left
💡 Hint
Use np.all() to check all elements in each row are positive.
✗ Incorrect
Option B correctly selects rows where every element is greater than 0.
Option B selects rows with any positive value, which is incorrect.
Options C and D use axis=0, filtering columns, not rows.