Numpy Python

๐Ÿ“˜ Chapter 6: Mathematical Functions and Statistics in NumPy โ€” Crunch Numbers Like a Pro

๐Ÿง  โ€œ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

print("Mean:", np.mean(arr))  # 30.0

๐Ÿ”น np.min() & np.max() โ€“ Extremes

print("Min:", np.min(arr))  # 10
print("Max:", np.max(arr))  # 50

๐Ÿ”น np.std() โ€“ Standard deviation

print("Standard Deviation:", np.std(arr))  # Spread from the mean

๐Ÿ”น np.var() โ€“ Variance

print("Variance:", np.var(arr))

 


๐Ÿง  3. Finding Indices of Extremes

๐Ÿ”น np.argmax() โ€“ Index of the largest value

print("Index of Max:", np.argmax(arr))  # 4

๐Ÿ”น np.argmin() โ€“ Index of the smallest value

print("Index of Min:", np.argmin(arr))  # 0

 


๐ŸงŠ 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:

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

 

๐ŸŸฆ Axis 0 = Column-wise (downward)

๐ŸŸฅ Axis 1 = Row-wise (across)

๐Ÿ”ธ Sum by rows

print("Row-wise Sum:", np.sum(matrix, axis=1))  # [6 15]

๐Ÿ”ธ Sum by columns

print("Column-wise Sum:", np.sum(matrix, axis=0))  # [5 7 9]

๐Ÿ”ธ 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

arr = np.array([1, 2, 3, 4])
print("Cumulative Sum:", np.cumsum(arr))  # [1 3 6 10]

๐Ÿ”น np.cumprod() โ€“ Cumulative product

 

print("Cumulative Product:", np.cumprod(arr))  # [1 2 6 24]

 

๐Ÿ”น np.percentile() โ€“ Statistical percentiles

arr = np.array([1, 3, 5, 7, 9])
print("50th Percentile (Median):", np.percentile(arr, 50))  # 5

 

๐Ÿ”น np.median() โ€“ Middle value

print("Median:", np.median(arr))  # 5

 


๐Ÿงช 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)

arr = np.array([10, 20, 30])
normalized = arr - np.mean(arr)
print(normalized)  # [-10.  0.  10.]

Scaling to 0โ€“1 range (Min-Max Normalization)

scaled = (arr - np.min(arr)) / (np.max(arr) - np.min(arr))
print(scaled)  # [0.  0.5 1.]


โš ๏ธ 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.

Leave a Reply

Your email address will not be published. Required fields are marked *