Vectorise all the things! How basic linear algebra can speed up your data science code
summary
Implements K‑nearest neighbors in pure Python, then replaces list loops with numpy arrays arithmetic, then swaps nested loops for a broadcasted 3D subtraction using broadcasting, and finally uses numpy’s argsort/array slicing instead of Python sorts and list comprehensions. Baseline runs ~15s (small), 12min (medium), 40min (large); final version hits ~13ms, 15s, 2min respectively — roughly 1,000×, 50× and 15× speedups, showing the concrete code changes and timings that actually work.
AI-written summary · about
chapters (25)
- 0:00Intro
- 0:34Why vectorised operations
- 1:06Outline
- 2:08Vectors
- 2:57Vector spaces
- 4:05Matrix
- 4:33Numpy
- 6:05Vector neighbours
- 6:36Manhattan distance
- 7:44Canaras neighbors
- 8:36First implementation
- 12:15Arithmetic operations
- 13:29Input types
- 14:17Relative Improvement
- 14:38Nested for Loop
- 15:43Nested for Loop example
- 15:57Using matrices
- 16:29Using 3D arrays
- 17:44Broadcasting
- 18:44Vector Subtraction
- 19:45Vector Subtraction Implementation
- 21:06Baseline Implementation
- 21:22Kn Implementation
- 21:58Python Sorting
- 23:24Results
related talks
37mAccelerating Scientific Computing in Python with Numba: A Real-World Example
35mFast Pandas Performance Tricks You Wish You Knew Earlier
33m3 Ways to Speed up Your Regression Modeling in Python
28mIdiomatic Pandas
35mIdiomatic Pandas
35mWorking with data in Python for the Aspiring Data Scientists
44mHigh Perfomance Pandas
21mPolars: A highly optimized dataframe library
