Discretization
Discretization is the process of converting a continuous numeric variable into a set of discrete categories or bins - useful for simplifying data and for algorithms that work better with categorical inputs.
Why Discretize
# Converts:
Age = [22, 25, 31, 45, 52, 60]
# Into:
AgeGroup = ["Young", "Young", "Middle", "Middle", "Senior", "Senior"]
Common Discretization Methods
Equal-Width Binning: Divides the range into bins of equal size
Equal-Frequency Binning: Divides data so each bin has roughly the same number of points
Custom Binning: Uses domain knowledge to define meaningful bin edges
Implementing in Python
import pandas as pd
df = pd.DataFrame({
"Age": [22, 25, 31, 45, 52, 60, 65, 70]
})
df["AgeGroup"] = pd.cut(
df["Age"],
bins=[0, 30, 50, 100],
labels=["Young", "Middle", "Senior"]
)
print(df)
When to Use Discretization
Discretization can make patterns easier to interpret, reduce the effect of noise or outliers, and is sometimes required by algorithms that assume categorical inputs rather than continuous ones.
Choosing bin boundaries carelessly can lose valuable information - always check whether the resulting categories still make sense for the problem at hand.
Coming Up Next
Next, you'll learn about Entropy, a key concept behind how Decision Trees decide where to split.