Comprehensive Quick Reference · array programming with Python

numpy cheat sheet v2 · all modules

A single source of truth across the whole NumPy surface: the ndarray core (creation, indexing, shape, ufuncs, broadcasting, stats), plus the specialized namespaces — linalg, fft, random, polynomial, ma (masked), strings, structured arrays, datetimes & stride tricks. Learn shape, axis, broadcasting and views vs. copies once, and the 600+ routines fall into place.

create / load inspect index & search shape & combine math & ufuncs aggregate / stats submodule gotcha most common

Validated against the official NumPy 2.x API reference (numpy.org) — routines by topic + the linalg, fft, random, polynomial, ma, strings, lib & rec namespaces; cross-checked with DataCamp, Dataquest, GeeksforGeeks, Real Python, TutorialsPoint & the official beginners guide. v2 gap-analysis edition.

The NumPy module map — the core plus its specialized namespaces
numpy — the ndarray creation · indexing · shape · ufuncs · stats np.linalgmatmul · svd · solve np.fftfft · ifft · fftfreq np.randomdefault_rng() np.polynomialPolynomial · fit np.mamasked arrays np.stringsvectorized text structured / reccompound dtypes lib.stride_trickssliding windows every submodule consumes and produces plain ndarrays — one data structure throughout

I · Core: Create, Inspect & Index the ndarray — everyday array work

01Setup & Constantscreate · inspect
02Array Creationcreate
03Attributes & Inspectioninspect
04Indexing & Slicingindex & search
05Boolean & Fancy Indexingindex & search
06Index Generation & Selectionindex & search

II · Shape, Combine & Views reshape · stack · split · copies vs views

07Reshaping & Axis Movesshape & combine
08Combining & Splittingshape & combine
09Tiling & Rearrangingshape & combine
10Copies vs. Viewsinspect

III · Math, Ufuncs & Broadcasting elementwise ops · trig · reductions

11Arithmetic & Universal Functionsmath & ufuncs
12Trig & Special Mathmath & ufuncs
13Broadcastingmath & ufuncs
14Ufunc Methods & Controlmath & ufuncs

IV · Aggregate, Sort, Search & Logic reductions · NaN-aware · histograms

15Aggregations & Statisticsaggregate / stats
16NaN-Aware Stats & Histogramsaggregate / stats
17Sorting, Searching & Countingindex & search
18Logic, Comparison & Set Opsmath & ufuncs

V · Specialized Submodules linalg · fft · random · polynomial

19Linear Algebramodule · np.linalg
20Fourier Transformsmodule · np.fft
21Random Numbersmodule · Generator API
22Polynomialsmodule · np.polynomial

VI · Masked, Strings, Structured, Dates & Performance ma · strings · rec · datetime64 · stride tricks

23Masked Arraysmodule · np.ma
24String Operationsmodule · np.strings
25Structured & Record Arrayscompound dtypes · np.rec
26Datetimes & Timedeltasdatetime64
27Stride Tricks & Iterationmodule · lib.stride_tricks
28Save, Load & I/Oinspect
29Performance & Gotchashandle with care
Common dtypesanywhere you see dtype=
Axis Quick-Readanywhere you see axis=
Testing & Type Toolsnp.testing

Axis reductions, broadcasting & strides

Three ideas explain most of NumPy's behaviour and speed: how an axis collapses, how shapes broadcast, and how an array is really just a view over flat memory described by strides. Based on the official NumPy internals diagrams.

axis=0 vs axis=1 ★

The axis you name is the one that disappears: axis=0 collapses rows (per-column result); axis=1 collapses columns (per-row result).

1 2 3 4 5 6 5 7 9 sum(axis=0) → (3,) 6 15 sum(axis=1) → (2,)

Broadcasting: (3,1) + (1,4) ★

Each length-1 axis is virtually stretched to match the other operand — producing a (3,4) grid with no data actually copied.

10 20 30 + 1 2 3 4 = 11 12 13 21 22 23 31 32 33 (3,1) + (1,4) → (3,4), no memory copied

Strides: one buffer, many shapes ★

An ndarray is a flat memory buffer plus a shape and strides (bytes to step per axis). reshape & slicing just change those numbers — that's why they're free.

flat buffer in memory 0 1 2 3 4 5 6 7 shape=(2,4), strides=(32, 8) bytes ↓ 0 1 2 3 4 5 6 7 reshape / slice = new metadata over the same bytes

Views vs. copies ★

Slicing returns a view (shared memory — mutations propagate); boolean/fancy indexing returns a copy (independent).

a (buffer) a[1:4] → VIEWshares memory;edits write back to a a[a>0] → COPYown memory;independent of a

Worth memorizing

the collapsed axis vanishesaxis=0 → per-column result; axis=1 → per-row result
view vs copyslices are views; boolean/fancy indexing makes a copy
broadcasting rulecompare shapes trailing-first; dims must match or be 1
* vs @* is elementwise; @/matmul is matrix multiply
NaN != NaNuse np.isnan(), never == np.nan; and np.nanmean to skip NaN
a += 1 on int arraysin-place ops keep the dtype and silently truncate floats
default_rng()the modern random API — prefer over legacy np.random.seed
reshape is freeit only rewrites shape/strides metadata over the same buffer
np.strings > np.charthe modern ufunc-backed string namespace (2.0+)
einsum is a swiss army knifebut a plain @ is faster for ordinary matrix products
NEP 52 removed aliasesnp.int/np.float/np.bool are gone — use builtins or sized types
add.at for repeated indicesplain a[idx]+=v loses duplicates; np.add.at doesn't