Vectorize all the things! Using linear algebra and NumPy to make your Python code lightning fast
summary
Builds a working k‑nearest neighbors classifier in plain Python then replaces slow nested loops and list operations with NumPy arrays, vectorized subtraction, broadcasting and array-based sorting to cut runtime massively. Baseline timings: ~15s (small), 12min (medium), 38min (large). After elementwise NumPy ops: ~8s / 7min / 9min; after 3D broadcasted differences: ~1.5s / 1min / 3min; after stacking + array sort/slicing: ~13ms / 15s / 2.5min — roughly 10–13× speedup on large, ~50× on medium, 1000× on small.
AI-written summary · about
chapters (8)
- 0:00Introduction and motivation
- 1:29Linear algebra basics
- 8:28K nearest neighbors explained
- 11:34Baseline unoptimized implementation
- 16:10Vectorizing distance calculations
- 21:09Broadcasting for pairwise distances
- 28:56Optimizing K nearest neighbors
- 32:30Why arrays outperform lists
related talks
29mVectorize using linear algebra and NumPy to make your Python code fast
34mNumPy: vectorize your brain
23mPerformance Python: Seven Strategies for Optimizing Your Numerical Code
01h 31mMaking Computation Easier with Cool Numpy Tricks
35mThe "NumPy" Approach
40mSpeed Without Drag
40mTools and Tricks from a Pragmatic Data Scientist
01h 27mMaking computation easier with cool Numpy tricks
