0
0
NumPydata~20 mins

np.min() and np.max() in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.min() and np.max() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.min() on a 2D array with axis parameter
What is the output of the following code?
import numpy as np
arr = np.array([[3, 7, 5], [1, 6, 9]])
result = np.min(arr, axis=0)
print(result)
NumPy
import numpy as np
arr = np.array([[3, 7, 5], [1, 6, 9]])
result = np.min(arr, axis=0)
print(result)
A[1 6 5]
B[1 7 5]
C[3 7 5]
D[1 6 9]
Attempts:
2 left
💡 Hint
Think about what axis=0 means for a 2D array.
data_output
intermediate
2:00remaining
Number of elements in np.max() result with keepdims=True
Given the code below, how many elements are in the resulting array?
import numpy as np
arr = np.array([[4, 2, 8], [7, 1, 5]])
result = np.max(arr, axis=1, keepdims=True)
print(result)
print(result.size)
NumPy
import numpy as np
arr = np.array([[4, 2, 8], [7, 1, 5]])
result = np.max(arr, axis=1, keepdims=True)
print(result)
print(result.size)
A3
B6
C2
D1
Attempts:
2 left
💡 Hint
Keepdims keeps the dimension, but does not change the number of rows.
🔧 Debug
advanced
2:00remaining
Identify the error in np.min() usage
What error does the following code raise?
import numpy as np
arr = np.array([1, 2, 3])
result = np.min(arr, axis=1)
print(result)
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = np.min(arr, axis=1)
print(result)
ATypeError: 'int' object is not iterable
BAxisError: axis 1 is out of bounds for array of dimension 1
CValueError: operands could not be broadcast together
DNo error, output is 1
Attempts:
2 left
💡 Hint
Check the shape of the array and the axis parameter.
🚀 Application
advanced
2:00remaining
Find the maximum value ignoring NaNs
Which code correctly finds the maximum value in the array ignoring NaN values?
import numpy as np
arr = np.array([3, np.nan, 7, 2])
NumPy
import numpy as np
arr = np.array([3, np.nan, 7, 2])
Anp.max(arr, where=~np.isnan(arr))
Bnp.max(arr)
Cnp.max(arr[~np.isnan(arr)])
Dnp.nanmax(arr)
Attempts:
2 left
💡 Hint
Look for a numpy function that ignores NaNs automatically.
🧠 Conceptual
expert
3:00remaining
Effect of np.min() and np.max() on empty arrays
What happens when you call np.min() or np.max() on an empty numpy array?
import numpy as np
empty_arr = np.array([])
min_val = np.min(empty_arr)
max_val = np.max(empty_arr)
NumPy
import numpy as np
empty_arr = np.array([])
min_val = np.min(empty_arr)
max_val = np.max(empty_arr)
ABoth calls raise a ValueError: zero-size array to reduction operation minimum/maximum which has no identity
Bmin_val is 0, max_val is 0
CBoth min_val and max_val are np.inf and -np.inf respectively
Dmin_val and max_val are None
Attempts:
2 left
💡 Hint
Think about what minimum or maximum means for no elements.