numpy β Numerical Arrays#
What it is#
NumPy provides the ndarray β a fixed-type, C-backed multi-dimensional array. It is the foundation of the entire Python scientific stack (pandas, scipy, matplotlib all build on it). Vectorized operations on ndarray are typically 100β1000Γ faster than equivalent pure-Python loops.
Install#
pip install numpy
Quick example#
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a * 2)
print(f"mean={a.mean():.1f}, std={a.std():.4f}")
print(np.dot(a, a))
Output:
[ 2 4 6 8 10]
mean=3.0, std=1.4142
55
When / why to use it#
- You need fast numeric computation without pandas overhead.
- Working with images, audio, or other multi-dimensional data (images are just 3-D arrays).
- Linear algebra (matrix multiply, decompositions).
- Feeding data into machine learning libraries (all expect numpy arrays or array-like objects).
Common pitfalls#
[!WARNING]
dtypedefaults can surprise you βnp.array([1, 2, 3])createsint64, butnp.zeros((3, 3))createsfloat64. Mixing dtypes in operations silently upcasts. Be explicit:np.array([1, 2], dtype=np.float32).
[!WARNING] Views vs copies β slicing a numpy array returns a view, not a copy. Modifying the slice modifies the original. Use
.copy()when you need independence:b = a[1:3].copy().
[!TIP] Check shapes before operations:
a.shape,a.dtype,a.ndim. Mismatched shapes are the #1 cause ofValueError: operands could not be broadcast.
Array creation#
import numpy as np
np.array([1, 2, 3]) # from list
np.zeros((3, 4)) # 3Γ4 zeros (float64)
np.ones((2, 2), dtype=int) # 2Γ2 ones (int)
np.eye(3) # 3Γ3 identity
np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
np.linspace(0, 1, 5) # [0, 0.25, 0.5, 0.75, 1.0]
np.random.default_rng(42).random((3, 3)) # random 3Γ3 (seeded)
Indexing and slicing#
a = np.arange(12).reshape(3, 4)
print(a)
print(a[1, 2]) # row 1, col 2
print(a[:, 1]) # all rows, col 1
print(a[a > 5]) # boolean mask β 1-D array
Output:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
6
[1 5 9]
[ 6 7 8 9 10 11]
Richer example β matrix operations and broadcasting#
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print("Matrix product:")
print(A @ B)
print("\nElement-wise product:")
print(A * B)
# Broadcasting: add a row vector to every row of a matrix
offsets = np.array([10, 20])
print("\nBroadcast add:")
print(A + offsets)
# Trig over evenly spaced angles
x = np.linspace(0, 2 * np.pi, 5)
print("\nsin values:")
print(np.round(np.sin(x), 4))
Output:
Matrix product:
[[19 22]
[43 50]]
Element-wise product:
[[ 5 12]
[21 32]]
Broadcast add:
[[11 22]
[13 24]]
sin values:
[ 0. 1. 0. -1. -0.]
Useful functions quick reference#
| Function | Purpose |
|---|---|
np.concatenate([a, b], axis=0) | Join arrays along an axis |
np.stack([a, b], axis=0) | Stack along a new axis |
np.split(a, 3) | Split into N equal parts |
np.sort(a) | Sorted copy |
np.argsort(a) | Indices that would sort the array |
np.unique(a) | Unique values |
np.where(a > 0, a, 0) | Conditional element selection |
np.clip(a, 0, 1) | Clamp values to [0, 1] |
np.linalg.norm(a) | Euclidean norm |
np.linalg.eig(A) | Eigenvalues and eigenvectors |
np.fft.fft(signal) | Fast Fourier transform |