๐ง โWhen it comes to numbers, NumPy doesnโt just play with them โ it commands them.โ
Welcome back to the sixth chapter of your NumPy learning journey! So far, weโve learned how to create arrays, slice and dice them, and even perform powerful arithmetic operations using broadcasting.
But whatโs the next logical step?
๐ Analysis.
Whether you’re working with small datasets or big data, youโll often need to summarize, analyze, and draw insights. Thatโs where NumPyโs mathematical and statistical functions come into play.
In this chapter, you’ll learn:
-
How to use NumPyโs built-in functions like
sum(),mean(),std(),min(),max(),argmax(), and more. -
How to apply these functions along specific axes of multi-dimensional arrays.
-
Real-life scenarios for using them effectively.
๐ 1. The Need for Mathematical Functions
Whether youโre building machine learning models, performing data cleaning, or running simulations โ you need to:
-
Find averages
-
Determine ranges and extremes
-
Understand data variability
-
Identify max/min positions
Instead of writing your own formulas, NumPy provides built-in, optimized functions โ ready to use and blazing fast.
๐งฎ 2. Basic Mathematical Functions
Letโs create an array to work with:
import numpy as np arr = np.array([10, 20, 30, 40, 50])
๐น np.sum() โ Total sum
print("Sum:", np.sum(arr)) # 150
๐น np.mean() โ Average value
๐น np.min() & np.max() โ Extremes
๐น np.std() โ Standard deviation
print("Standard Deviation:", np.std(arr)) # Spread from the mean
๐น np.var() โ Variance
๐ง 3. Finding Indices of Extremes
๐น np.argmax() โ Index of the largest value
๐น np.argmin() โ Index of the smallest value
๐ง 4. Axis-Based Computations
When working with 2D or higher arrays, you can specify the axis along which to perform operations.
Letโs create a matrix:
๐ฆ Axis 0 = Column-wise (downward)
๐ฅ Axis 1 = Row-wise (across)
๐ธ Sum by rows
๐ธ Sum by columns
๐ธ Mean by rows
print("Row-wise Mean:", np.mean(matrix, axis=1))
๐ธ Max per column
print("Max per column:", np.max(matrix, axis=0)) # [4 5 6]
๐งฉ 5. Other Useful Mathematical Functions
๐น np.cumsum() โ Cumulative sum
๐น np.cumprod() โ Cumulative product
print("Cumulative Product:", np.cumprod(arr)) # [1 2 6 24]
๐น np.percentile() โ Statistical percentiles
๐น np.median() โ Middle value
๐งช 6. Real-Life Applications
| Task | Function |
|---|---|
| Calculate average customer spend | np.mean() |
| Find the month with highest sales | np.argmax() |
| Detect outliers | np.std(), np.percentile() |
| Normalize test scores | arr - np.mean(arr) |
| Analyze image brightness | np.mean() on image matrix |
๐งฐ Bonus: Normalization with NumPy
Zero-centering an array (subtract the mean)
Scaling to 0โ1 range (Min-Max Normalization)
โ ๏ธ 7. Common Mistakes to Avoid
| Mistake | Solution |
|---|---|
| Using axis=1 on 1D arrays | Only use axis with 2D+ arrays |
Assuming mean() returns int |
It returns float, even for int arrays |
| Ignoring axis direction | Remember: axis=0 = column, axis=1 = row |
Confusing argmax() with max() |
argmax returns index, not value |
๐งพ Summary Table: NumPy Math & Stats
| Function | Purpose |
|---|---|
sum() |
Total of all elements |
mean() |
Arithmetic mean |
std() |
Standard deviation |
var() |
Variance |
min(), max() |
Min and max values |
argmin(), argmax() |
Indices of min/max |
cumsum() |
Cumulative sum |
median() |
Middle value |
percentile() |
Statistical percentile |
๐ Wrapping Up Chapter 6
Congratulations! ๐
You now have the tools to extract deep insights from any dataset using NumPyโs statistical arsenal. Whether it’s finding an average, spotting an outlier, or identifying the max performer โ NumPy makes it effortless.
These are essential skills for:
-
Data science
-
AI/ML
-
Financial modeling
-
Scientific computing
-
Signal/image analysis
๐ Coming Next in Chapter 7:
Weโll cover:
-
Linear Algebra with NumPy โ dot product, matrix multiplication, transpose, inverse, and more.
