Challenge - 5 Problems
Vectorize Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of vectorized custom function with conditional logic
What is the output of this code that uses
np.vectorize() on a custom function with a condition?NumPy
import numpy as np def classify(x): if x > 0: return 'pos' elif x < 0: return 'neg' else: return 'zero' vec_classify = np.vectorize(classify) arr = np.array([-2, 0, 3]) result = vec_classify(arr) print(result)
Attempts:
2 left
💡 Hint
Think about how the function classifies negative, zero, and positive numbers.
✗ Incorrect
The function returns 'neg' for negative numbers, 'zero' for zero, and 'pos' for positive numbers. The input array is [-2, 0, 3], so the output matches ['neg', 'zero', 'pos'].
❓ data_output
intermediate2:00remaining
Resulting array shape after vectorizing a function returning tuples
What is the shape of the output array when applying
np.vectorize() to a function that returns a tuple for each input element?NumPy
import numpy as np def to_pair(x): return (x, x**2) vec_to_pair = np.vectorize(to_pair) arr = np.array([1, 2, 3]) result = vec_to_pair(arr) print(result.shape)
Attempts:
2 left
💡 Hint
Check what type of array is returned when the function returns tuples.
✗ Incorrect
The vectorized function returns an array of tuples, so the shape is (3,), a 1D array with 3 elements each being a tuple.
🔧 Debug
advanced2:00remaining
Identify the error in vectorizing a function with multiple arguments
What error does this code raise when using
np.vectorize() on a function with two arguments but passing only one array?NumPy
import numpy as np def add(x, y): return x + y vec_add = np.vectorize(add) arr = np.array([1, 2, 3]) result = vec_add(arr) print(result)
Attempts:
2 left
💡 Hint
Check how many arguments the function expects versus how many are passed.
✗ Incorrect
The function add expects two arguments, but vec_add is called with only one array, causing a TypeError about the missing argument.
🚀 Application
advanced2:00remaining
Using np.vectorize to apply a function that returns different types
What is the output of this code where the vectorized function returns either int or str based on input?
NumPy
import numpy as np def process(x): if x % 2 == 0: return x // 2 else: return 'odd' vec_process = np.vectorize(process) arr = np.array([1, 2, 3, 4]) result = vec_process(arr) print(result)
Attempts:
2 left
💡 Hint
Consider how numpy arrays handle mixed types in vectorized functions.
✗ Incorrect
The function returns mixed types (int and str), so np.vectorize creates an object dtype array. When printed, the output is ['odd' 1 'odd' 2], with integers shown without quotes.
🧠 Conceptual
expert2:00remaining
Why use np.vectorize instead of a for loop for custom functions?
Which statement best explains the main advantage of using
np.vectorize() over a Python for loop when applying a custom function to a numpy array?Attempts:
2 left
💡 Hint
Think about what np.vectorize actually does under the hood.
✗ Incorrect
np.vectorize is a convenience wrapper that applies a function element-wise using numpy syntax but does not provide true vectorized speed or parallelism.