A structured, well-commented Python reference covering NumPy from the ground up.
Each file is self-contained and executable — run it and read the output alongside the code.
| File | Level | Topics Covered |
|---|---|---|
01_numpy_basics.py |
Beginner | Array creation, dtypes, indexing, slicing, element-wise math, aggregations, shape manipulation, stacking, sorting, utilities |
02_numpy_intermediate.py |
Intermediate | Broadcasting, np.newaxis, fancy indexing, np.ix_, advanced sorting (lexsort, partition), set operations, linear algebra (linalg), statistics, structured arrays, masked arrays, polynomials, datetime, saving/loading |
03_numpy_advanced.py |
Advanced | Memory layout & strides, as_strided sliding windows, views vs copies, einsum, custom ufuncs, advanced broadcasting patterns, performance & memory tricks, memory-mapped files, FFT, advanced linear algebra (QR, Cholesky, SVD, lstsq), financial analytics, Numba teaser, common gotchas |
Each file is a runnable script with rich print output and inline explanations.
python 01_numpy_basics.py
python 02_numpy_intermediate.py
python 03_numpy_advanced.pyNo setup beyond NumPy itself is required.
numpy >= 1.20
python >= 3.8
Install NumPy if you don't have it:
pip install numpy- Why NumPy is faster than Python lists (vectorisation benchmark included)
- Creating arrays with
np.array,np.zeros,np.ones,np.arange,np.linspace - Understanding
shape,ndim,size,dtype,itemsize - Indexing and slicing in 1D and 2D (including the view vs copy distinction)
- Element-wise math and universal functions (
np.sin,np.exp,np.sqrt, ...) - Boolean indexing and conditional selection
- Aggregations:
sum,mean,std,min,max,argmin,argmax - Shape manipulation:
reshape,flatten,ravel,transpose - Stacking (
hstack,vstack) and splitting (hsplit,vsplit)
- Broadcasting rules and practical patterns (column/row normalisation, outer products)
np.newaxisandexpand_dimsfor dimension management- Fancy indexing with integer arrays and
np.where - Deep-dive sorting:
argsort,lexsort,partition - Set operations:
intersect1d,union1d,setdiff1d,isin - Linear algebra: matrix multiply (
@),det,inv,solve,eig, SVD, norms - Statistics: percentiles, covariance, correlation, histograms
- Modern random API:
np.random.default_rng - Structured arrays (typed, named fields — like a lightweight table)
- Masked arrays with
numpy.mafor missing-data handling - Saving and loading with
.npy,.npz, and.csv
- C-order vs Fortran-order memory layout and cache performance
- Strides: how NumPy really works under the hood
as_stridedfor zero-copy sliding windows (1D and 2D)- Complete view vs copy truth table (
shares_memory) einsum— matrix multiply, dot, outer, trace, batch ops, and more- Ufunc internals:
.reduce,.accumulate,.outer np.vectorize,np.frompyfunc,np.piecewise,apply_along_axis- Advanced broadcasting: distance matrices, softmax, one-hot encoding
- Performance:
out=parameter,keepdims, dtype choice, cache-friendly indexing - Memory-mapped arrays (
np.memmap) for out-of-RAM datasets - FFT: signal decomposition, dominant frequency detection, 2D FFT
- Advanced decompositions: QR, Cholesky, pseudo-inverse, least-squares,
eigh - Financial analytics: log returns, Sharpe ratio, max drawdown, VaR
- Six common gotchas: integer overflow, float equality, view mutation,
asarrayvsarray, chained indexing,keepdims
numpy/
├── 01_numpy_basics.py
├── 02_numpy_intermediate.py
├── 03_numpy_advanced.py
└── README.md
MIT