0
0
NumPydata~20 mins

np.any() and np.all() in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
1: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)
ATrue
BFalse
C[False False False True]
D[False False False False]
Attempts:
2 left
💡 Hint
np.any() returns True if any element in the array is True.
Predict Output
intermediate
1: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)
A[True True]
B[True False]
C[False True]
D[False False]
Attempts:
2 left
💡 Hint
np.all() with axis=1 checks if all elements in each row are True.
data_output
advanced
1: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)
A(3, 1)
B(1, 3)
C(2,)
D(3,)
Attempts:
2 left
💡 Hint
np.any with axis=0 reduces rows, keeping columns dimension.
🔧 Debug
advanced
1: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)
AAxisError: axis 1 is out of bounds for array of dimension 1
BTypeError: unsupported operand type(s) for all()
CNo error, prints True
DValueError: invalid axis value
Attempts:
2 left
💡 Hint
Check the dimension of the array and the axis parameter.
🚀 Application
expert
2: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]])
Afiltered = arr[np.any(arr > 0, axis=1)]
Bfiltered = arr[np.all(arr > 0, axis=1)]
Cfiltered = arr[np.all(arr >= 0, axis=0)]
Dfiltered = arr[np.any(arr >= 0, axis=0)]
Attempts:
2 left
💡 Hint
Use np.all() to check all elements in each row are positive.