Priya works as a data scientist at SafeGuard Insurance in Bangalore. The company wants to predict which customers will file claims in the next year. She has a dataset of 5000 customers with 13 features: age, monthly charges, claim history, etc.
She writes a Python loop to calculate the average age and multiply feature vectors. It takes 30 seconds. Her manager says: "We need to score 500,000 customers next week." A single loop won't scale. Then her mentor shows her NumPy, and the same calculation runs in 0.3 seconds.
Creating arrays: 5 essential functions
Arrays are NumPy's core. Unlike Python lists, arrays are fixed-type and vectorized—perfect for math. Here are the 5 ways to create them:
Array operations: indexing, slicing, reshaping
Once you have an array, you access elements just like lists—but NumPy adds powerful reshape and boolean indexing.
Broadcasting: stretching smaller arrays to match shapes
Broadcasting is NumPy magic. When you add a 1D array to a 2D array, NumPy automatically stretches the smaller one to match. No loops needed.
Linear algebra: dot products and matrix multiplication
Every ML algorithm multiplies matrices: feature vectors × weights = predictions. NumPy's dot product and @ operator handle this.
Statistical functions: mean, std, median, min/max
Before building any model, you explore data statistically. NumPy provides fast functions for descriptive stats.
Random numbers: seeding and distributions
Machine learning uses randomness for train/test splits and initializing weights. NumPy's random module is reproducible with seeds.
Why NumPy? Speed and vectorization
A Python loop over 5000 rows takes seconds. NumPy's vectorized operations take milliseconds. Here's why:
In practice, you'll see 10–100× speedups, depending on the operation. For 500,000 customers, that difference is real: minutes vs seconds.
Also used in...
Domain Spotlight
How AI systems use this
Every neural network under the hood is matrix multiplication: inputs × weights = output. When ChatGPT processes your question, it's doing tens of thousands of dot products with NumPy-like libraries (PyTorch, TensorFlow). Without NumPy-level efficiency, large language models wouldn't be practical.
Task: Create a 5x3 array of random ages (18–65). Compute the mean age of each column. Find which column has the oldest average. (Bonus: filter ages > 50 and count how many per column.)
Hint 1: Use np.random.randint(18, 66, size=(5, 3))
Hint 2: Compute column means: np.mean(ages, axis=0)
Hint 3: Find the column with max mean: np.argmax(means)
Hint 4 (bonus): Filter and count: (ages > 50).sum(axis=0)
Interview questions: test your understanding
Quiz: 5 questions to test your knowledge
Cheatsheet: NumPy functions at a glance
| Function | What it does | Example |
|---|---|---|
np.array() |
Create array from list | np.array([1,2,3]) |
np.zeros() |
Array of zeros | np.zeros((3,4)) |
np.arange() |
Range with step | np.arange(0,10,2) |
np.linspace() |
Evenly spaced values | np.linspace(0,1,5) |
np.dot() |
Dot product | np.dot(a, b) |
A @ B |
Matrix multiplication | np.array(...) @ np.array(...) |
np.mean() |
Average | np.mean(data) |
np.std() |
Standard deviation | np.std(data) |
np.median() |
Median | np.median(data) |
arr.reshape() |
Change dimensions | arr.reshape(3,4) |
arr[arr > 50] |
Boolean indexing | ages[ages > 50] |
np.argmax() |
Index of maximum | np.argmax(data) |
Common mistakes: what breaks and how to fix it
.copy() if you need isolation.np.sum(arr), np.mean(arr).Key takeaways
NumPy is the foundation of ML in Python. Remember these:
- Arrays are fast: Fixed-type, contiguous memory, compiled operations in C.
- Broadcasting is magic: Automatically stretches smaller arrays; eliminates nested loops.
- Linear algebra is central: Every ML model is matrix math—np.dot(), @, matrix operations.
- Vectorize everything: Never write for loops over arrays. Use indexing, broadcasting, and vectorized functions.
- Explore statistically: np.mean(), np.std(), np.median() help you understand data before modeling.
Ready to build ML models?
Join the Best AI Program: The AI-Era Analyst — 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Analyst →