0
0
NumPydata~10 mins

np.vectorize() for custom functions in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.vectorize() for custom functions
Define custom function
Create vectorized function with np.vectorize
Input: array or list
Apply vectorized function element-wise
Output: array with results
You define a function, then use np.vectorize to apply it to each element of an array easily.
Execution Sample
NumPy
import numpy as np

def add_one(x):
    return x + 1

vfunc = np.vectorize(add_one)
result = vfunc([1, 2, 3])
This code adds 1 to each element of the list [1, 2, 3] using np.vectorize.
Execution Table
StepInput ElementFunction CallFunction ResultOutput Array State
11add_one(1)2[2]
22add_one(2)3[2, 3]
33add_one(3)4[2, 3, 4]
4N/AAll elements processedN/A[2, 3, 4]
💡 All input elements processed, vectorized function returns final output array.
Variable Tracker
VariableStartAfter 1After 2After 3Final
input_array[1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3]
output_array[][2][2, 3][2, 3, 4][2, 3, 4]
Key Moments - 2 Insights
Why do we use np.vectorize instead of just a for loop?
np.vectorize lets us write code that looks like it works on arrays directly, making it simpler and cleaner, as shown in the execution_table where the function is applied element-wise automatically.
Does np.vectorize make the function run faster?
No, np.vectorize is mostly for convenience. It still calls the function once per element, like a loop, as seen in each step of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output array after processing the second element?
A[2, 3]
B[3, 4]
C[1, 2]
D[2]
💡 Hint
Check the Output Array State column at Step 2 in the execution_table.
At which step does the function add_one get called with input 3?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Function Call column in the execution_table.
If the input array was [4, 5], how many rows would the execution_table have before exit?
A5
B3
C4
D2
💡 Hint
The table has one row per element plus one exit row, see current table with 3 elements and 4 rows.
Concept Snapshot
np.vectorize(func) creates a vectorized version of func.
It applies func element-wise to arrays or lists.
Use it to simplify applying custom functions to arrays.
It does NOT speed up execution, just convenience.
Returns a new array with results.
Full Transcript
This lesson shows how np.vectorize lets you apply a custom function to each element of an array easily. First, you define your function, like add_one which adds 1 to a number. Then you create a vectorized version using np.vectorize. When you call this vectorized function with an array, it applies your function to each element one by one. The execution table traces each step: input element, function call, result, and how the output array grows. The variable tracker shows input and output arrays at each step. Key moments clarify that np.vectorize is for convenience, not speed. The quiz checks understanding of the output array state and function calls. Finally, the snapshot summarizes how to use np.vectorize for custom functions.