binning
**Binning (Discretization)** is a **feature engineering technique that converts continuous variables into categorical "buckets"** — transforming exact values like Age=27 into ranges like "18-35", which helps linear models capture non-linear relationships (a linear model can't natively learn "young and old are high risk, middle-aged is low risk" but can learn different weights per age bin), reduces the impact of outliers (age 150 just goes into the "60+" bucket), and can improve model interpretability by expressing features in terms that domain experts understand.
**What Is Binning?**
- **Definition**: The process of mapping continuous values to discrete intervals (bins) — converting a numeric feature with infinite possible values into a categorical feature with a fixed number of groups.
- **Why Bin?**: (1) Capture non-linear relationships for linear models, (2) Reduce noise and outlier sensitivity, (3) Handle data quality issues (exact value may be unreliable, but the bin is correct), (4) Improve interpretability for business stakeholders who think in categories ("young", "middle-aged", "senior") not exact numbers.
- **Trade-off**: Binning loses information — Age=18 and Age=34 become the same "18-35" bin. This precision loss is only worthwhile if the bin structure captures the actual relationship better than the raw value.
**Binning Strategies**
| Strategy | Method | Bin Example (Age) | Use Case |
|----------|--------|------------------|----------|
| **Equal Width** | Same range per bin | 0-25, 25-50, 50-75, 75-100 | Simple, uniform distribution assumed |
| **Equal Frequency (Quantile)** | Same count per bin | Each bin has ~1000 people | Skewed distributions |
| **Domain Knowledge** | Expert-defined thresholds | 0-17 (minor), 18-64 (adult), 65+ (senior) | When business rules matter |
| **Decision Tree Splits** | Use tree to find optimal thresholds | Split at 35 and 58 (maximizes prediction) | Data-driven optimal bins |
| **K-Means** | Cluster values into K groups | Centers at 22, 38, 55, 72 | Natural groupings in the data |
**Binning Example: Credit Risk**
| Age | Bin | Default Rate | Interpretation |
|-----|-----|-------------|---------------|
| 18-25 | Young | 15% | Higher risk — less financial history |
| 26-35 | Early Career | 8% | Moderate risk |
| 36-50 | Established | 4% | Low risk — stable income |
| 51-65 | Pre-Retirement | 5% | Low risk |
| 65+ | Retirement | 12% | Higher risk — fixed income |
A linear model with the raw Age feature can only learn "older = more/less risk" (monotonic). With bins, it learns the U-shaped relationship: young and old are higher risk.
**Python Implementation**
```python
import pandas as pd
# Equal-width bins
df['age_bin'] = pd.cut(df['age'], bins=[0, 25, 35, 50, 65, 100],
labels=['Young', 'Early', 'Mid', 'Senior', 'Elder'])
# Quantile bins (equal frequency)
df['income_bin'] = pd.qcut(df['income'], q=5, labels=['Q1','Q2','Q3','Q4','Q5'])
```
**When to Bin vs Not**
| Bin | Don't Bin |
|-----|----------|
| Linear models with non-linear relationships | Tree-based models (they find optimal splits already) |
| Noisy measurements where bins are more reliable | When exact values matter (temperature in physics) |
| Domain requires categories (age groups, income brackets) | When you have enough data for the model to learn non-linearities |
| Outlier mitigation | When precision loss is unacceptable |
**Binning is the feature engineering technique that bridges continuous and categorical thinking** — enabling linear models to capture non-linear patterns, reducing outlier impact, and expressing features in domain-meaningful categories, with the trade-off that information is lost whenever exact values are collapsed into ranges.