Bird
Raised Fist0
NumPydata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main advantage of using vectorization in numpy instead of loops?
easy
A. It makes the code run faster by doing many operations at once.
B. It uses more memory but slows down the code.
C. It makes the code harder to read and understand.
D. It requires writing more lines of code.

Solution

  1. Step 1: Understand vectorization concept

    Vectorization means applying operations on whole arrays at once, not one element at a time.
  2. Step 2: Compare speed and simplicity

    This approach uses optimized numpy functions that run faster than explicit loops.
  3. Final Answer:

    It makes the code run faster by doing many operations at once. -> Option A
  4. Quick Check:

    Vectorization = faster code [OK]
Hint: Vectorization speeds up code by handling arrays at once [OK]
Common Mistakes:
  • Thinking vectorization slows code
  • Believing vectorization needs more code
  • Confusing vectorization with loops
2. Which of the following numpy codes correctly replaces a loop to add 5 to each element in array a?
easy
A. a + 5
B. for i in a: i = i + 5
C. np.add(a, 5, out=a)
D. a.append(5)

Solution

  1. Step 1: Identify vectorized addition

    Using np.add(a, 5, out=a) adds 5 to every element in array a efficiently.
  2. Step 2: Check other options

    a + 5 works but returns a new array, for i in a: i = i + 5 does not change a, and a.append(5) is invalid for numpy arrays.
  3. Final Answer:

    np.add(a, 5, out=a) -> Option C
  4. Quick Check:

    np.add = vectorized add [OK]
Hint: Use numpy functions like np.add for vectorized operations [OK]
Common Mistakes:
  • Using loops that don't modify array
  • Trying list methods on numpy arrays
  • Confusing array addition with append
3. What is the output of this code?
import numpy as np
arr = np.array([1, 2, 3])
result = arr * 2
print(result)
medium
A. [2 4 6]
B. [1 2 3 1 2 3]
C. [3 4 5]
D. Error: cannot multiply array by int

Solution

  1. Step 1: Understand array multiplication

    Multiplying a numpy array by a number multiplies each element by that number.
  2. Step 2: Calculate element-wise multiplication

    Each element in [1, 2, 3] is multiplied by 2, resulting in [2, 4, 6].
  3. Final Answer:

    [2 4 6] -> Option A
  4. Quick Check:

    Array * number = element-wise multiply [OK]
Hint: Array * number multiplies each element [OK]
Common Mistakes:
  • Thinking it concatenates arrays
  • Expecting an error on multiplication
  • Confusing element-wise with matrix multiply
4. Identify the error in this code that tries to add 10 to each element of arr using a loop:
import numpy as np
arr = np.array([1, 2, 3])
for i in arr:
    i = i + 10
print(arr)
medium
A. Syntax error in for loop.
B. Loop variable i does not modify original array elements.
C. Array arr is not defined.
D. Cannot add integer to numpy array elements.

Solution

  1. Step 1: Understand loop variable behavior

    Variable i is a copy of each element, so changing i does not change arr.
  2. Step 2: Explain why array remains unchanged

    The original array arr stays the same because the loop does not assign back to array elements.
  3. Final Answer:

    Loop variable i does not modify original array elements. -> Option B
  4. Quick Check:

    Loop var copy ≠ array change [OK]
Hint: Loop vars are copies; assign to array to change [OK]
Common Mistakes:
  • Assuming loop changes array automatically
  • Thinking syntax error exists
  • Believing numpy disallows int addition
5. You have a numpy array data = np.array([1, 2, 3, 4, 5]). You want to create a new array where each element is squared if it is even, otherwise doubled. Which vectorized code achieves this?
hard
A. data * 2 if data % 2 else data ** 2
B. [x**2 if x%2==0 else x*2 for x in data]
C. for i in range(len(data)): data[i] = data[i]**2 if data[i]%2==0 else data[i]*2
D. np.where(data % 2 == 0, data**2, data*2)

Solution

  1. Step 1: Understand vectorized conditional with np.where

    np.where(condition, x, y) returns array choosing x where condition is true, else y.
  2. Step 2: Apply condition for even and odd elements

    Condition data % 2 == 0 checks even numbers; squares them, else doubles.
  3. Step 3: Check other options

    [x**2 if x%2==0 else x*2 for x in data] uses list comprehension (not vectorized), C uses loop (not vectorized), D is invalid syntax.
  4. Final Answer:

    np.where(data % 2 == 0, data**2, data*2) -> Option D
  5. Quick Check:

    np.where = vectorized if-else [OK]
Hint: Use np.where for vectorized if-else conditions [OK]
Common Mistakes:
  • Using loops or list comprehensions instead of vectorization
  • Writing invalid syntax for conditional operations
  • Confusing element-wise operations with scalar
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard