liveBook · Manning

Discretization

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

A technique that groups numeric values into discrete ordered bins and assigns each value to a bin.

Binning groups numeric values into discrete, ordered bins and assigns each bin an integer while preserving the original order. The number of groups is commonly called k. section 5.4.3

Use binning when a continuous feature or target needs a more manageable representation:

  • Accelerate decision-tree split evaluation by testing histogram-derived thresholds instead of every unique numeric value. section 5.4.3
  • Convert a continuous target such as price into binary or multiclass classes by splitting at the mean or median, or by using deciles. section 4.2
  • Turn a numeric feature into a categorical feature for association analysis, such as comparing deciles with Cramer's V. section 2.4.3
Listing 5.14 The histogram split
import numpy as np
 
def gini_impurity(y):
    _, counts = np.unique(y, return_counts=True)
    probs = counts / len(y)
    return 1 - np.sum(probs**2)                              #1
 
def histogram_split(x, y, use_histogram, n_bins=256):
    if use_histogram:
        hist, thresholds = np.histogram(
            x, bins=n_bins, density=False
        )                                                    #2
    else:
        thresholds = np.unique(x)                            #3
    best_score = -1
    best_threshold = None                                    #4
    for threshold in thresholds:                             #5
        left_mask = x <= threshold
        right_mask = x > threshold
        left_y = y[left_mask]
        right_y = y[right_mask]                              #6
        score = (
            gini_impurity(left_y) * len(left_y) 
            + gini_impurity(right_y) * len(right_y)
        )                                                    #7
        if score > best_score: q                             #8
            best_threshold = threshold
            best_score = score
    return best_threshold, best_score                        #9

The implementation compares exact unique-value thresholds with histogram-based thresholds while calculating the best split using Gini impurity. section 5.4.3

Histogram splitting on latitude

For Airbnb latitude values, evaluating every unique threshold took about 46.9 seconds, while evaluating 256 histogram bins took about 562 milliseconds and produced the same reported split and score. section 5.4.3

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Explains ordered integer bins, threshold evaluation, Gini scoring, and histogram speedups without developing the complete XGBoost split algorithm.
Examples
Uses latitude thresholds, Airbnb price deciles, MPG deciles, and a 256-bin histogram implementation.
What you can do after
Apply it via NumPy histogram operations to reduce split candidates or discretize numeric variables for classification and association analysis.
How it connects
Connects binning with histogram splitting, decision trees, gradient boosting, classification, regression, exploratory data analysis, and Cramer's V.

Also explained in

Compare how 5 books explain discretization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.