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:
- Read x, Read y
- Check type(x), Check type(y)
- Add
- Repeat 1,000,000 times.
- NumPy Vectorization:
- "Hey CPU, add these two blocks of memory."
- 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.
pythonMatrix (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]