0
0
NumPydata~20 mins

np.vectorize() for custom functions in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vectorize Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A['zero' 'neg' 'pos']
B['neg' 'pos' 'zero']
C['pos' 'zero' 'neg']
D['neg' 'zero' 'pos']
Attempts:
2 left
💡 Hint
Think about how the function classifies negative, zero, and positive numbers.
data_output
intermediate
2: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)
A(2, 3)
B(3, 2)
C(3,)
D(2,)
Attempts:
2 left
💡 Hint
Check what type of array is returned when the function returns tuples.
🔧 Debug
advanced
2: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)
ATypeError: add() missing 1 required positional argument: 'y'
BNo error, outputs [2 4 6]
CSyntaxError: invalid syntax
DValueError: operands could not be broadcast together
Attempts:
2 left
💡 Hint
Check how many arguments the function expects versus how many are passed.
🚀 Application
advanced
2: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)
A['odd' '1' 'odd' '2']
B['odd' 1 'odd' 2]
C['odd' '1' 'odd' 2]
D['odd' 1 'odd' '2']
Attempts:
2 left
💡 Hint
Consider how numpy arrays handle mixed types in vectorized functions.
🧠 Conceptual
expert
2: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?
Anp.vectorize allows applying functions element-wise with numpy syntax but does not guarantee speed improvement.
Bnp.vectorize automatically converts the function to use GPU acceleration.
Cnp.vectorize provides true parallel execution and speeds up computation significantly.
Dnp.vectorize compiles the function to machine code for faster execution.
Attempts:
2 left
💡 Hint
Think about what np.vectorize actually does under the hood.