Central tendency metrics describe the center of a numerical data distribution. Selecting the correct metric depends on data distribution skewness and the presence of outliers.
flowchart TD
A["Central Tendency Metrics"] --> B["Mean (Arithmetic Average)"]
A --> C["Median (Middle Value)"]
A --> D["Mode (Most Frequent Value)"]
B --> E["Sensitive to Outliers!"]
C --> F["Robust Against Outliers!"]
D --> G["Ideal for Categorical Data"]
import numpy as np
from scipy import stats
from typing import Dict, Any
def calculate_central_tendency(data: np.ndarray) -> Dict[str, Any]:
mean_val = float(np.mean(data))
median_val = float(np.median(data))
mode_result = stats.mode(data, keepdims=True)
mode_val = float(mode_result.mode[0])
return {
"mean": mean_val,
"median": median_val,
"mode": mode_val
}
if __name__ == "__main__":
# Data with a heavy outlier (1000)
incomes = np.array([45000, 48000, 50000, 52000, 55000, 1000000])
metrics = calculate_central_tendency(incomes)
print("--- Income Distribution Metrics ---")
print(f"Mean Income : ${metrics['mean']:,.2f} (Distorted by outlier)")
print(f"Median Income : ${metrics['median']:,.2f} (Robust representative)")
print(f"Mode Income : ${metrics['mode']:,.2f}")
stats.mode() returns the smallest modal value if multiple modes exist.Calculate the mean and median of [10, 20, 30, 40, 500] using np.mean() and np.median(). Explain the difference.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With