NumPy Cheatsheet

Math Operations

Use this NumPy reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Element-wise Arithmetic

All standard operators map to ufuncs and broadcast over arrays.

import numpy as np

a = np.array([1.0, 2.0, 3.0])
b = np.array([4.0, 5.0, 6.0])

a + b          # [5., 7., 9.]
a - b          # [-3., -3., -3.]
a * b          # [4., 10., 18.]
a / b          # [0.25, 0.4, 0.5]   — always float division
a // b         # [0., 0., 0.]       — floor division
a % b          # [1., 2., 3.]       — modulo
a ** 2         # [1., 4., 9.]       — element-wise power
-a             # [-1., -2., -3.]

Equivalent ufunc calls

np.add(a, b)
np.subtract(a, b)
np.multiply(a, b)
np.divide(a, b)
np.floor_divide(a, b)
np.mod(a, b)
np.power(a, b)
np.negative(a)
np.absolute(a)       # abs(a), np.abs(a)  — also for complex (returns magnitude)
np.reciprocal(a)     # 1/a (integer-safe: use on float arrays)
np.sign(a)           # -1, 0, or 1 per element
np.fabs(a)           # abs for floats only (slightly faster)

Trigonometric Functions

np.sin(a);  np.cos(a);  np.tan(a)
np.arcsin(a);  np.arccos(a);  np.arctan(a)
np.arctan2(y, x)      # atan2 — handles quadrant correctly, returns [-π, π]
np.hypot(x, y)        # sqrt(x² + y²)
np.degrees(a)         # radians → degrees
np.radians(a)         # degrees → radians (also np.deg2rad / np.rad2deg)

# Hyperbolic
np.sinh(a);  np.cosh(a);  np.tanh(a)
np.arcsinh(a);  np.arccosh(a);  np.arctanh(a)

Exponential and Logarithm

np.exp(a)        # e^a
np.exp2(a)       # 2^a
np.expm1(a)      # e^a - 1  — accurate for small a
np.log(a)        # natural log
np.log2(a)       # log base 2
np.log10(a)      # log base 10
np.log1p(a)      # log(1 + a) — accurate for small a
np.logaddexp(a, b)    # log(exp(a) + exp(b)) — numerically stable
np.logaddexp2(a, b)   # log2(2^a + 2^b)

Rounding

FunctionBehavior
np.round(a, decimals)round to nearest even (banker's rounding)
np.around(a, decimals)alias for np.round
np.floor(a)round toward −∞
np.ceil(a)round toward +∞
np.trunc(a)round toward 0 (truncate)
np.fix(a)same as trunc
np.rint(a)round to nearest integer (same as round with no decimals)
a = np.array([-1.7, -0.5, 0.5, 1.4, 2.5])
np.floor(a)    # [-2., -1.,  0.,  1.,  2.]
np.ceil(a)     # [-1.,  0.,  1.,  2.,  3.]
np.trunc(a)    # [-1.,  0.,  0.,  1.,  2.]
np.round(a)    # [-2.,  0.,  0.,  1.,  2.]  — 0.5 → 0 (banker's rounding)

Gotcha: np.round(0.5) == 0 and np.round(1.5) == 2 — NumPy uses banker's rounding (round-half-to-even).

Clipping and Extremes

np.clip(a, 0, 1)         # clamp all values to [0, 1]
np.clip(a, None, 5)      # clip only upper bound
np.maximum(a, b)         # element-wise max (propagates NaN)
np.minimum(a, b)         # element-wise min (propagates NaN)
np.fmax(a, b)            # element-wise max — ignores NaN
np.fmin(a, b)            # element-wise min — ignores NaN

Bitwise Operations

np.bitwise_and(a, b)    # a & b
np.bitwise_or(a, b)     # a | b
np.bitwise_xor(a, b)    # a ^ b
np.invert(a)            # ~a  — bitwise NOT
np.left_shift(a, 2)     # a << 2
np.right_shift(a, 2)    # a >> 2

# With operator syntax (integer arrays)
a & b;  a | b;  a ^ b;  ~a;  a << 2;  a >> 2

Logical Operations

np.logical_and(a, b)    # element-wise and
np.logical_or(a, b)
np.logical_not(a)
np.logical_xor(a, b)

# Comparison operators (return bool arrays)
a == b;  a != b;  a < b;  a <= b;  a > b;  a >= b
np.equal(a, b);  np.not_equal(a, b)
np.less(a, b);  np.less_equal(a, b)
np.greater(a, b);  np.greater_equal(a, b)

# Floating-point comparison (accounts for rounding)
np.isclose(a, b, rtol=1e-5, atol=1e-8)    # element-wise bool
np.allclose(a, b)                           # single bool
np.array_equal(a, b)                        # exact equality, shape+values
np.array_equiv(a, b)                        # broadcast-compatible equality

Special Float Utilities

np.isnan(a)       # element-wise NaN check
np.isinf(a)       # element-wise inf check (+ or -)
np.isposinf(a)
np.isneginf(a)
np.isfinite(a)    # not NaN and not inf
np.nan_to_num(a, nan=0.0, posinf=1e9, neginf=-1e9)
np.copysign(a, b) # magnitude of a, sign of b
np.signbit(a)     # True where a < 0 (including -0.0)
np.ldexp(m, e)    # m * 2**e
np.frexp(a)       # decompose into mantissa and exponent
np.modf(a)        # (fractional part, integer part)
np.divmod(a, b)   # (quotient, remainder) simultaneously

In-place Operations (ufunc out argument)

result = np.empty_like(a)
np.add(a, b, out=result)           # write into result, no temp allocation
np.multiply(a, 2, out=a)           # in-place double
np.exp(a, out=a)                   # in-place exp

# where argument — conditional application
np.sqrt(a, where=a >= 0, out=np.zeros_like(a))

Polynomials

p = np.poly1d([2, -3, 1])   # 2x² - 3x + 1
p(5)                          # evaluate at x=5 → 36
p.roots                       # [1.0, 0.5]
p.deriv()                     # 4x - 3
p.integ()                     # (2/3)x³ - (3/2)x² + x

np.polyval([2, -3, 1], [0, 1, 5])  # evaluate poly at multiple points
np.polymul([1, 2], [1, -1])        # multiply polynomials → [1, 1, -2]
np.polyadd([1, 2], [3])            # add polynomials
np.polydiv([1, 0, -1], [1, 1])     # divide: ([1., -1.], [0.])
np.polyfit(x, y, deg=2)            # least-squares polynomial fit → coefficients