This article is not a reference dump.
It explains what matters, why it exists, and where it is used in real workflows.
- Creating Arrays — The Foundation Everything in NumPy starts with ndarray.
np.array()
np.array([1, 2, 3, 4])
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))
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))
Creates an array filled with ones.
Common use
Normalization
Bias initialization
Sanity checks and testing
np.full(shape, value)
np.full((2, 3), 7)
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)
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)
Creates evenly spaced values (stop included).
Used in
Plotting
Simulations
Continuous mathematical domains
- 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
Why this matters
Bugs in NumPy are usually shape bugs
Performance depends on correct dimensionality
reshape()
arr.reshape(3, 4)
Changes shape without copying data.
Rule:
Total elements must remain constant.
flatten() vs ravel()
flatten() → returns a copy
ravel() → returns a view when possible
Rule
Use ravel() for performance
Use flatten() when isolation is required
- Stacking & Axis Manipulation Combining arrays is common in real pipelines. Stacking
np.hstack() # column-wise
np.vstack() # row-wise
np.dstack() # depth-wise
Used when assembling datasets, images, feature blocks.
Transposition
arr.T
Swaps axes (rows ↔ columns).
swapaxes(axis1, axis2)
Used for 3D+ tensors, common in:
Computer vision
Deep learning
Physics simulations
- 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.
- 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)
Powers & roots
np.power(a, 2)
np.sqrt(a)
Exponentials & logs
np.exp(a)
np.log(a)
Trigonometry
np.sin(a)
np.cos(a)
np.tan(a)
Key idea
No loops. Ever.
- Statistical Functions Central tendency
np.mean(a)
np.median(a)
Spread
np.var(a)
np.std(a)
Variance → data spread
Std deviation → distance from mean
Aggregation
np.max(a)
np.min(a)
np.sum(a)
np.cumsum(a)
Index-based results
np.argmax(a)
np.argmin(a)
Returns indices, not values—critical in optimization and ML.
- Correlation & Relationships Variance across datasets
np.var(a)
Correlation matrix
np.corrcoef(x, y)
Properties:
Values ∈ [-1, +1]
Diagonal = 1
Measures linear relationship strength
Used in:
Feature selection
Financial analysis
Signal processing
- Random Number Generation Uniform distribution
np.random.rand()
Normal distribution
np.random.randn()
Mean = 0, Std = 1
Used in:
Natural processes
ML weight initialization
Random integers
np.random.randint(1, 10, (3, 3))
Sampling
np.random.choice(data, size=4, replace=True, p=None)
Supports:
Replacement
Probability bias
Shuffling
np.random.shuffle(a) # in-place
np.random.permutation(a) # copy
Reproducibility
np.random.seed(42)
Mandatory for:
Experiments
Debugging
Scientific results
- 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()
Used for datasets larger than RAM.
Critical in:
Big data
Genomics
Financial tick data
- Linear Algebra — The Core Power Dot product / multiplication
np.dot(a, b)
Handles:
Vector dot product
Matrix multiplication
1D 2D combinations
Strict matrix multiplication
np.matmul(a, b)
Solving linear systems
np.linalg.solve(A, B)
Solves:
AX = B
Inverse & determinant
np.linalg.inv(A)
np.linalg.det(A)
Eigen decomposition
np.linalg.eig(A)
Returns:
Eigenvalues
Eigenvectors
Used in:
PCA
Stability analysis
Physics models
Singular Value Decomposition
U, S, Vt = np.linalg.svd(A)
Interpretation:
U → input space rotation
S → importance (strength)
Vt → output space rotation
Foundation of:
PCA
Compression
Noise reduction
- Norms — Magnitude & Distance Vector norms
np.linalg.norm(v) # L2
np.linalg.norm(v, ord=1) # L1
np.linalg.norm(v, ord=np.inf)
Matrix norms
Frobenius norm
Max row / column sum
Spectral norm (via SVD)
Used in:
Optimization
Regularization
Model stability
Top comments (0)