Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

NumPy — From Basics to Advanced

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.


Files

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

How to Use

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.py

No setup beyond NumPy itself is required.


Requirements

numpy >= 1.20
python >= 3.8

Install NumPy if you don't have it:

pip install numpy

What You'll Learn

Basics

  • 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)

Intermediate

  • Broadcasting rules and practical patterns (column/row normalisation, outer products)
  • np.newaxis and expand_dims for 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.ma for missing-data handling
  • Saving and loading with .npy, .npz, and .csv

Advanced

  • C-order vs Fortran-order memory layout and cache performance
  • Strides: how NumPy really works under the hood
  • as_strided for 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, asarray vs array, chained indexing, keepdims

Structure

numpy/
├── 01_numpy_basics.py
├── 02_numpy_intermediate.py
├── 03_numpy_advanced.py
└── README.md

License

MIT

About

NumPy from basics to advanced — structured, well-commented Python reference

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages