NumPy
Fundamental array computing library for Python — the backbone of the entire scientific Python ecosystem.
NumPy provides multi-dimensional arrays and a library of mathematical operations that run in optimised C and Fortran code. It is the common data exchange format of the scientific Python stack — Pandas, SciPy, scikit-learn, PyTorch, and TensorFlow all interoperate with NumPy arrays. Vectorised operations over arrays replace explicit Python loops, giving 10–100× speedups for numerical work.
Quick start
pip install numpy
import numpy as np
# Create arrays
a = np.array([1, 2, 3, 4, 5])
b = np.arange(1, 6) # [1, 2, 3, 4, 5]
c = np.zeros((3, 4)) # 3x4 matrix of zeros
d = np.random.rand(3, 3) # 3x3 matrix of random floats
# Vectorised operations (no loop needed)
print(a * 2) # [2, 4, 6, 8, 10]
print(a + b) # [2, 4, 6, 8, 10]
print(a.mean(), a.std())
# 2D array operations
matrix = np.array([[1, 2], [3, 4]])
print(matrix.T) # transpose
print(np.linalg.det(matrix)) # determinant: -2.0
print(np.linalg.inv(matrix)) # inverse
# Broadcasting
x = np.array([[1, 2, 3],
[4, 5, 6]])
print(x + np.array([10, 20, 30])) # add row-wise
# Boolean indexing
scores = np.array([72, 45, 88, 91, 60])
print(scores[scores > 70]) # [72, 88, 91]
When to use
NumPy is essential for any Python numerical or scientific computing work. It’s a dependency of virtually every data science library and you’ll use it even when working at higher levels (Pandas, PyTorch). Use NumPy directly for linear algebra, signal processing, and custom numerical algorithms. For tabular data and time series, Pandas adds labelled indexing on top. For GPU-accelerated array computing, CuPy provides a NumPy-compatible API.
// features
- N-dimensional arrays (`ndarray`) as the universal data container
- Vectorised operations — replace loops with fast C/Fortran kernels
- Broadcasting — operate on arrays of different shapes without loops
- Linear algebra: `np.linalg` for matrix operations, eigenvalues, solvers
- Random number generation with seeded reproducibility
- Fast Fourier transforms via `np.fft`
- Integration with Pandas, SciPy, scikit-learn, PyTorch, TensorFlow
- Memory-mapped arrays for datasets too large to fit in RAM
// installation
conda install numpy
pip install numpy