NumPy Cheatsheet

Arrays, indexing, math, linear algebra & broadcasting

Data / Python
Contents
📦

Creating Arrays

import numpy as np

a = np.array([1, 2, 3])              # 1D
b = np.array([[1,2],[3,4]])            # 2D
np.zeros((3, 4))                      # 3×4 zeros
np.ones((2, 3))                       # 2×3 ones
np.full((3, 3), 7)                    # filled with 7
np.eye(4)                             # 4×4 identity
np.arange(0, 10, 2)                  # [0,2,4,6,8]
np.linspace(0, 1, 5)                 # 5 evenly spaced
np.empty((2, 3))                      # uninitialized
np.zeros_like(a)                      # same shape as a
🔢

Indexing & Slicing

a = np.array([10,20,30,40,50])
a[0]                # 10
a[-1]               # 50
a[1:3]              # [20, 30]
a[::2]              # [10, 30, 50]

# 2D
b = np.array([[1,2,3],[4,5,6],[7,8,9]])
b[0, 1]             # 2
b[0]                # [1, 2, 3] (first row)
b[:, 0]             # [1, 4, 7] (first column)
b[0:2, 1:3]         # submatrix

# Boolean indexing
a[a > 25]           # [30, 40, 50]

# Fancy indexing
a[[0, 2, 4]]        # [10, 30, 50]
📐

Shape & Reshape

a.shape              # (5,)
a.ndim               # 1
a.size               # 5
a.dtype              # int64

# Reshape
a.reshape(5, 1)     # column vector
a.reshape(-1, 1)    # auto-compute rows
a.flatten()          # 1D copy
a.ravel()            # 1D view
a.T                  # transpose

# Stack
np.vstack([a, a])    # vertical stack
np.hstack([a, a])    # horizontal stack
np.concatenate([a, a], axis=0)

# Split
np.split(a, 5)       # split into 5

Math Operations

# Element-wise
a + b    a - b    a * b    a / b
a ** 2   np.sqrt(a)   np.exp(a)   np.log(a)
np.sin(a)   np.cos(a)   np.abs(a)

# Comparison (returns bool array)
a > 3    a == 5    np.all(a > 0)    np.any(a > 5)

# Aggregate
a.sum()    a.min()    a.max()    a.mean()
a.sum(axis=0)    a.sum(axis=1)
np.cumsum(a)     np.diff(a)

# Dot product
np.dot(a, b)     a @ b
📊

Statistics

np.mean(a)        np.median(a)
np.std(a)         np.var(a)
np.percentile(a, 75)
np.corrcoef(x, y)
np.histogram(a, bins=10)
np.unique(a, return_counts=True)
np.argmax(a)      np.argmin(a)
np.sort(a)        np.argsort(a)
np.where(a > 5, a, 0)   # conditional
np.clip(a, 0, 10)        # clamp values
📏

Linear Algebra

np.linalg.inv(A)       # inverse
np.linalg.det(A)       # determinant
np.linalg.eig(A)       # eigenvalues
np.linalg.svd(A)       # SVD
np.linalg.norm(a)      # vector norm
np.linalg.solve(A, b)  # solve Ax=b
np.linalg.lstsq(A, b)  # least squares
A @ B                    # matrix multiply
🎲

Random

rng = np.random.default_rng(42)       # seeded
rng.random((3, 3))                     # uniform [0,1)
rng.integers(0, 10, size=(5,))         # random ints
rng.normal(0, 1, size=(100,))          # normal dist
rng.choice([1,2,3], size=5)            # random choice
rng.shuffle(a)                          # in-place shuffle
rng.permutation(a)                      # shuffled copy
💾

I/O

np.save("arr.npy", a)          # binary
a = np.load("arr.npy")

np.savez("data.npz", x=a, y=b)  # multiple
data = np.load("data.npz")
data["x"]

np.savetxt("out.csv", a, delimiter=",")
a = np.loadtxt("out.csv", delimiter=",")
a = np.genfromtxt("data.csv", delimiter=",", skip_header=1)