π 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
| Function | Description |
|---|---|
| sum() | Total |
| min() | Smallest value |
| max() | Largest value |
| prod() | Product |
| mean() | Average |
π§ Pro Tips
- Use
axisfor 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.