TensorLearn
Back to Course
Data Intelligence: NumPy & Pandas
Module 3 of 15

3. Vectorization

1. SIMD (Single Instruction, Multiple Data)

Processors are smart. They can add 4 numbers to 4 other numbers in ONE clock cycle. Python loops cannot do this. NumPy can.

Visual Proof

Imagine adding two lists of 1,000,000 numbers.

  • Python Loop:
    1. Read x, Read y
    2. Check type(x), Check type(y)
    3. Add
    4. Repeat 1,000,000 times.
  • NumPy Vectorization:
    1. "Hey CPU, add these two blocks of memory."
    2. Done.
python
# Slow [x + 1 for x in huge_list] # Fast (Vectorized) huge_array + 1

2. Broadcasting

NumPy matches dimensions automagically. It stretches the smaller array to fit the larger one.

python
Matrix (3x3) + Vector (3) = Result (3x3) [1, 2, 3] [1] [2, 3, 4] [4, 5, 6] + [1] -> [5, 6, 7] [7, 8, 9] [1] [8, 9, 10]

Mark as Completed

TensorLearn - AI Engineering for Professionals