DEV Community

Bodhisatva Tiwari
Bodhisatva Tiwari

Posted on

NumPy: An Engineer-Level Guide to Arrays, Math, Randomness, and Linear Algebra

This article is not a reference dump.
It explains what matters, why it exists, and where it is used in real workflows.

  1. Creating Arrays — The Foundation Everything in NumPy starts with ndarray.

np.array()

np.array([1, 2, 3, 4])
Enter fullscreen mode Exit fullscreen mode

Converts Python sequences into contiguous, homogeneous memory blocks.

Why it matters
Enables vectorized operations
Predictable performance
Eliminates Python loop overhead

np.zeros(shape)

np.zeros((3, 4))
Enter fullscreen mode Exit fullscreen mode

Creates an array filled with zeros.

Used for
Preallocation in performance-critical loops
Placeholder tensors in ML pipelines
Numerical solvers and simulations

np.ones(shape)

np.ones((3, 3))
Enter fullscreen mode Exit fullscreen mode

Creates an array filled with ones.

Common use
Normalization
Bias initialization
Sanity checks and testing

np.full(shape, value)

np.full((2, 3), 7)
Enter fullscreen mode Exit fullscreen mode

Creates an array with a constant value.

Why
Sentinel values
Mask initialization
Controlled default states

np.arange(start, stop, step)

np.arange(0, 10, 2)
Enter fullscreen mode Exit fullscreen mode

Creates evenly spaced values (stop excluded).

Best for
Index-based loops
Discrete ranges
Performance-critical iteration

np.linspace(start, stop, num)

np.linspace(0, 10, 5)
Enter fullscreen mode Exit fullscreen mode

Creates evenly spaced values (stop included).

Used in
Plotting
Simulations
Continuous mathematical domains

  1. Array Properties & Shape Control Understanding shape and memory is non-negotiable. Core attributes
arr.shape   # dimensions
arr.ndim    # number of axes
arr.size    # total elements
arr.dtype   # data type

Enter fullscreen mode Exit fullscreen mode

Why this matters
Bugs in NumPy are usually shape bugs
Performance depends on correct dimensionality

reshape()

arr.reshape(3, 4)
Enter fullscreen mode Exit fullscreen mode

Changes shape without copying data.

Rule:
Total elements must remain constant.

flatten() vs ravel()

flatten() → returns a copy
ravel() → returns a view when possible
Enter fullscreen mode Exit fullscreen mode

Rule
Use ravel() for performance
Use flatten() when isolation is required

  1. Stacking & Axis Manipulation Combining arrays is common in real pipelines. Stacking
np.hstack()   # column-wise
np.vstack()   # row-wise
np.dstack()   # depth-wise
Enter fullscreen mode Exit fullscreen mode

Used when assembling datasets, images, feature blocks.

Transposition

arr.T
Enter fullscreen mode Exit fullscreen mode

Swaps axes (rows ↔ columns).

swapaxes(axis1, axis2)

Used for 3D+ tensors, common in:
Computer vision
Deep learning
Physics simulations

  1. Broadcasting — Why NumPy Is Fast Broadcasting lets NumPy operate on arrays of different shapes without copying memory.

Rules (simplified)
Dimensions must match or
One dimension must be 1

Example:
(3, 3) + (3,)
The smaller array is virtually expanded, not duplicated.
This is why NumPy avoids Python loops.

  1. Mathematical Operations (Vectorized) All operations are element-wise by default.

Arithmetic

np.add(a, b)
np.subtract(a, b)
np.multiply(a, b)
np.divide(a, b)

Enter fullscreen mode Exit fullscreen mode

Powers & roots

np.power(a, 2)
np.sqrt(a)
Enter fullscreen mode Exit fullscreen mode

Exponentials & logs

np.exp(a)
np.log(a)
Enter fullscreen mode Exit fullscreen mode

Trigonometry

np.sin(a)
np.cos(a)
np.tan(a)
Enter fullscreen mode Exit fullscreen mode

Key idea
No loops. Ever.

  1. Statistical Functions Central tendency
np.mean(a)
np.median(a)
Enter fullscreen mode Exit fullscreen mode

Spread

np.var(a)
np.std(a)
Enter fullscreen mode Exit fullscreen mode

Variance → data spread

Std deviation → distance from mean

Aggregation

np.max(a)
np.min(a)
np.sum(a)
np.cumsum(a)
Enter fullscreen mode Exit fullscreen mode

Index-based results

np.argmax(a)
np.argmin(a)

Enter fullscreen mode Exit fullscreen mode

Returns indices, not values—critical in optimization and ML.

  1. Correlation & Relationships Variance across datasets
np.var(a)
Enter fullscreen mode Exit fullscreen mode

Correlation matrix

np.corrcoef(x, y)
Enter fullscreen mode Exit fullscreen mode

Properties:
Values ∈ [-1, +1]
Diagonal = 1
Measures linear relationship strength

Used in:
Feature selection
Financial analysis
Signal processing

  1. Random Number Generation Uniform distribution
np.random.rand()
Enter fullscreen mode Exit fullscreen mode

Normal distribution

np.random.randn()
Enter fullscreen mode Exit fullscreen mode

Mean = 0, Std = 1

Used in:
Natural processes
ML weight initialization

Random integers

np.random.randint(1, 10, (3, 3))
Enter fullscreen mode Exit fullscreen mode

Sampling

np.random.choice(data, size=4, replace=True, p=None)
Enter fullscreen mode Exit fullscreen mode

Supports:
Replacement
Probability bias

Shuffling

np.random.shuffle(a)       # in-place
np.random.permutation(a)   # copy

Enter fullscreen mode Exit fullscreen mode

Reproducibility

np.random.seed(42)
Enter fullscreen mode Exit fullscreen mode

Mandatory for:
Experiments
Debugging
Scientific results

  1. File Handling

np.loadtxt()
Fast
Strict
Numeric only
No missing values

np.genfromtxt()
Handles missing values
Mixed dtypes
Can fill NaNs

NaN detection

np.isnan(a)
np.memmap()

Enter fullscreen mode Exit fullscreen mode

Used for datasets larger than RAM.

Critical in:
Big data
Genomics
Financial tick data

  1. Linear Algebra — The Core Power Dot product / multiplication
np.dot(a, b)
Enter fullscreen mode Exit fullscreen mode

Handles:
Vector dot product
Matrix multiplication
1D 2D combinations

Strict matrix multiplication

np.matmul(a, b)
Enter fullscreen mode Exit fullscreen mode

Solving linear systems

np.linalg.solve(A, B)
Enter fullscreen mode Exit fullscreen mode

Solves:
AX = B

Inverse & determinant

np.linalg.inv(A)
np.linalg.det(A)
Enter fullscreen mode Exit fullscreen mode

Eigen decomposition

np.linalg.eig(A)
Enter fullscreen mode Exit fullscreen mode

Returns:
Eigenvalues
Eigenvectors

Used in:
PCA
Stability analysis
Physics models

Singular Value Decomposition

U, S, Vt = np.linalg.svd(A)
Enter fullscreen mode Exit fullscreen mode

Interpretation:
U → input space rotation
S → importance (strength)
Vt → output space rotation

Foundation of:
PCA
Compression
Noise reduction

  1. Norms — Magnitude & Distance Vector norms
np.linalg.norm(v)            # L2
np.linalg.norm(v, ord=1)     # L1
np.linalg.norm(v, ord=np.inf)
Enter fullscreen mode Exit fullscreen mode

Matrix norms
Frobenius norm
Max row / column sum
Spectral norm (via SVD)

Used in:
Optimization
Regularization
Model stability

Top comments (0)