EasyFeature Engineering

Binning

Feature Engineering · Data Processing

Easy

Problem

Binning (also called discretization) converts continuous numeric features into discrete categories by dividing the value range into equal-width intervals. Each value is assigned to its corresponding bin. This technique can reduce noise, handle outliers, and make features compatible with algorithms that work better with categorical data.

Given a list of numeric values and a number of bins, assign each value to a bin index using equal-width binning.

Algorithm

  1. Compute the bin width from the data range:

w = \frac{\max(x) - \min(x)}{\text{num\_bins}}

  1. Assign each value to a bin:

\text{bin}(x_i) = \min\left(\left\lfloor \frac{x_i - \min(x)}{w} \right\rfloor, \; \text{num\_bins} - 1\right)

The maximum value is clamped to the last bin. If all values are equal, all are assigned to bin 0.

Return one integer from zero through num_bins minus one for each value.

Theory

Binning, also called discretization or bucketing, transforms continuous numerical variables into discrete categorical ones by grouping values into intervals (bins). This technique converts a continuous feature like age (0-100) into categories like "young", "middle-aged", and "senior".


Why Discretize Continuous Data?


Types of Binning

Equal-Width Binning

Divides the range into intervals of equal size.

Formula: Given minimum value x_{min}, maximum value x_{max}, and k bins:

\text{bin\_width} = \frac{x_{max} - x_{min}}{k}

Bin assignment: For a value x, its bin index is:

\text{bin}(x) = \lfloor \frac{x - x_{min}}{\text{bin\_width}} \rfloor

Characteristics:

Example: Ages 0-100 with 5 equal-width bins creates boundaries at 0, 20, 40, 60, 80, 100


Equal-Frequency Binning (Quantile Binning)

Each bin contains approximately the same number of samples.

Approach: With N samples and k bins, each bin gets roughly N/k samples. Bin edges are determined by quantiles (percentiles).

Characteristics:

Example: For 4 bins, use the 25th, 50th, and 75th percentiles as boundaries (quartile binning)


Custom Binning

Domain knowledge defines meaningful boundaries.

Examples:

Advantages: Bins have semantic meaning aligned with domain expertise


Mathematical Formulation

For equal-width binning with k bins:

Bin edges: e_i = x_{min} + i \cdot \frac{x_{max} - x_{min}}{k} for i = 0, 1, ..., k

Bin assignment function:

b(x) = \min\left(\lfloor \frac{x - x_{min}}{\text{width}} \rfloor, k-1\right)

The \min operation ensures the maximum value falls into the last bin rather than creating an out-of-bounds index.


Edge Cases and Boundary Handling


Worked Example: Equal-Width Binning

Data: [2, 7, 12, 18, 23, 31, 45, 52, 67, 89]

Step 1 - Compute range:

Step 2 - Calculate bin width for 4 bins:

Step 3 - Determine bin edges:

Step 4 - Assign values:

Result: [0, 0, 0, 0, 0, 1, 1, 2, 2, 3]

Observation: Distribution is uneven - 5 samples in Bin 0 but only 1 in Bin 3


Worked Example: Quantile Binning

Data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 100] (note the outlier)

For 4 bins, compute quartile boundaries:

Bin assignments:

Observation: The outlier (100) is grouped with normal high values, preventing a sparse outlier bin. Each bin has roughly equal count despite extreme skew.


Information Loss Considerations

Binning sacrifices granularity for simplicity:

Tradeoff: Fewer bins = more generalization, less overfitting, but potential underfitting. More bins = finer granularity but approaching the original continuous feature.


Where Binning Shows Up

Examples

Example 1

Input
values = [0, 25, 50, 75, 100], num_bins = 4
Output
[0, 1, 2, 3, 3]
Explanation
The width is 25, and the maximum is clamped into the final bin.

Example 2

Input
values = [1, 2, 3, 4, 5, 6], num_bins = 2
Output
[0, 0, 0, 1, 1, 1]

Hints

  1. Compute bin width from the observed minimum and maximum.
  2. Convert each offset into an integer bin and clamp the maximum to the final index.

Requirements

Constraints

Starter Code

def binning(values: list, num_bins: int) -> list:
    """
    Returns the equal-width bin index of every value.
    """
    # Write code here
    pass

Test Cases

CaseMatches
4binspublic
2binspublic