NumPy Detailed Tutorial: Beginner to Advanced with Real Examples
Master NumPy step by step: ndarrays, indexing, broadcasting, vectorization, linear algebra, random sampling, performance, and practical exercises.
NumPy Detailed Tutorial (Beginner to Advanced)
NumPy is the foundation of Python data and AI workflows. If you understand NumPy deeply, Pandas, Matplotlib, and ML libraries become much easier.
1) Install and Setup
pip install numpyimport numpy as np2) Core Concept: ndarray
a = np.array([10, 20, 30, 40])
print(a.shape) # (4,)
print(a.dtype) # usually int64Important attributes:
shape: dimensionsndim: number of dimensionsdtype: data typesize: total elements
3) Creating Arrays
np.zeros((2, 3))
np.ones((3, 3))
np.arange(0, 10, 2)
np.linspace(0, 1, 5)
np.eye(3)Use linspace when you need exact number of points, not step size.
4) Indexing, Slicing, and Filtering
x = np.array([5, 8, 12, 3, 9, 15])
print(x[2]) # 12
print(x[1:4]) # [ 8 12 3]
print(x[x > 8]) # [12 9 15]2D example:
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m[0, 2]) # 3
print(m[:, 1]) # column 1 -> [2 5]5) Broadcasting (Most Important Intermediate Skill)
Broadcasting allows arithmetic between arrays of compatible shapes.
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([10, 20, 30])
print(a + b)Output:
[[11 22 33]
[14 25 36]]Rule summary:
- Compare shapes from right to left
- Dimensions are compatible if equal, or one of them is
1
6) Vectorization (Avoid Python Loops)
# slow style (loop)
arr = np.arange(1_000_000)
out = [x * 2 for x in arr]
# fast NumPy style
fast = arr * 2Vectorized operations are typically much faster and cleaner.
7) Aggregations and Statistics
data = np.array([3, 5, 8, 10, 12])
print(data.sum())
print(data.mean())
print(data.std())
print(data.min(), data.max())Axis-based example:
mat = np.array([[1, 2, 3], [4, 5, 6]])
print(mat.sum(axis=0)) # column sums
print(mat.sum(axis=1)) # row sums8) Reshape, Stack, and Split
a = np.arange(12)
b = a.reshape(3, 4)
c = np.vstack([b, b])
d = np.hstack([b, b])Be careful: reshape requires same total element count.
9) Linear Algebra Basics
A = np.array([[2, 1], [1, 3]])
B = np.array([[1, 0], [0, 1]])
print(A @ B) # matrix multiplication
print(np.linalg.det(A)) # determinant
print(np.linalg.inv(A)) # inverse
print(np.linalg.eigvals(A)) # eigenvalues10) Random Sampling for Simulation
rng = np.random.default_rng(42)
print(rng.integers(0, 10, size=5))
print(rng.normal(loc=0, scale=1, size=1000))Use default_rng instead of old global random APIs for cleaner reproducibility.
11) Practical Mini Project
Build a simple sales analytics array pipeline:
- Generate random daily sales for 3 products over 30 days
- Compute total and average sales by product
- Find top 5 highest sales days overall
- Normalize each product series with z-score
12) Common Mistakes
- Mixing Python lists and arrays unintentionally
- Forgetting axis in reductions
- Confusing element-wise
*with matrix multiplication@ - Using loops where vectorization is possible
Next Step
After NumPy, move to:
Found this helpful?
Leave a comment
Have a question, correction, or just found this helpful? Leave a note below.