0
0
NumPydata~10 mins

Vectorization over loops in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Vectorization over loops
Start with array data
Apply vectorized operation
Operation runs on whole array at once
Get result array
Use result for next steps or output
Vectorization means applying operations to whole arrays at once instead of using loops, making code faster and simpler.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
squared = arr ** 2
print(squared)
This code squares each element of the array using vectorized power operation.
Execution Table
StepActionInputOperationOutput
1Create array[1, 2, 3, 4]np.array()[1 2 3 4]
2Square array[1 2 3 4]arr ** 2[1 4 9 16]
3Print result[1 4 9 16]print()[1 4 9 16]
💡 All elements squared at once, no loop needed, execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
arrundefined[1 2 3 4][1 2 3 4][1 2 3 4]
squaredundefinedundefined[1 4 9 16][1 4 9 16]
Key Moments - 2 Insights
Why don't we see a loop in the code but all elements get squared?
Because numpy applies the operation to the whole array at once internally, as shown in execution_table step 2, no explicit loop is needed.
Is the output array a new array or does it change the original?
The output is a new array 'squared' as seen in variable_tracker, the original 'arr' stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 2?
A[1 2 3 4]
B[2 4 6 8]
C[1 4 9 16]
D[1 8 27 64]
💡 Hint
Check the 'Output' column in row with Step 2 in execution_table.
According to variable_tracker, what is the value of 'arr' after step 2?
Aundefined
B[1 2 3 4]
C[1 4 9 16]
DNone
💡 Hint
Look at the 'arr' row under 'After Step 2' in variable_tracker.
If we replaced 'arr ** 2' with a for loop squaring each element, what would change in the execution_table?
AMore steps showing each loop iteration
BOutput would be different
CInput array would change
DNo change at all
💡 Hint
Vectorization replaces loops, so a loop version would show multiple iterations in execution_table.
Concept Snapshot
Vectorization means applying operations to whole arrays at once.
Use numpy arrays and operators like **, +, * directly.
No explicit loops needed, code is simpler and faster.
Output is a new array, original stays unchanged.
Great for math on big data sets.
Full Transcript
Vectorization over loops means using numpy to apply operations to entire arrays at once instead of looping through elements. For example, squaring an array with arr ** 2 applies the square to every element internally. This is faster and simpler than writing a loop. The original array stays unchanged and the result is a new array. The execution table shows creating the array, applying the vectorized square, and printing the result. The variable tracker shows how variables change after each step. Beginners often wonder how all elements get processed without a loop; numpy handles this internally. Another common question is whether the original array changes; it does not. If we replaced vectorization with a loop, the execution table would have more steps for each iteration. Vectorization is a key technique in data science for efficient array math.