Aggregation Functions in NumPy (sum, min, max Explained)

πŸ“Œ Introduction

When working with large datasets, you often need to summarize data quickly.

That’s where aggregation functions in NumPy become essential.


πŸ” What are Aggregation Functions?

Aggregation functions perform operations on a dataset and return a single value.

πŸ‘‰ Example:

  • Total sum
  • Minimum value
  • Maximum value

πŸ§ͺ Example Dataset

import numpy as npdata = np.array([10, 20, 30, 40])

βž• 1. Sum

print(np.sum(data))

πŸ‘‰ Output:

100

πŸ”½ 2. Minimum Value

print(np.min(data))

πŸ‘‰ Output:

10

πŸ”Ό 3. Maximum Value

print(np.max(data))

πŸ‘‰ Output:

40

πŸ”’ 4. Product of Elements

print(np.prod(data))

πŸ‘‰ Output:

240000

πŸ“Š 5. Mean (Average)

print(np.mean(data))

πŸ‘‰ Output:

25.0

πŸ“ 6. Aggregation in 2D Arrays

data = np.array([[1,2,3],[4,5,6]])print(np.sum(data, axis=0))  # column-wise
print(np.sum(data, axis=1)) # row-wise

⚑ Why Use Aggregation?

Using NumPy aggregation:

  • Saves time
  • Avoids loops
  • Handles large datasets efficiently

πŸ“¦ Real-World Example

sales = np.array([1000, 2000, 3000])total_sales = np.sum(sales)
print("Total:", total_sales)

βœ” Used in:

  • Business analytics
  • Finance reports
  • Data science

πŸ”— Works with Other Libraries

Aggregation functions are widely used in:

  • Pandas
  • Scikit-learn
  • TensorFlow

πŸ“Š Summary Table

FunctionDescription
sum()Total
min()Smallest value
max()Largest value
prod()Product
mean()Average

🧠 Pro Tips

  • Use axis for multi-dimensional arrays
  • Combine aggregation functions for better insights
  • Use aggregation in data preprocessing

πŸ”š Conclusion

Aggregation functions in NumPy are essential for quickly summarizing and analyzing data.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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