liveBook · Manning

Histogram

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

A representation that groups numeric values into bins and records their frequencies or aggregated statistics.

A histogram groups numeric values into bins, making the distribution easier to inspect than a long list of individual observations. In exploratory data analysis, it helps reveal the shape and concentration of a numeric feature and can expose values that merit closer checking. section 2.4.2

Inspecting vehicle features

For Auto MPG data, plot horsepower with 64 bins and acceleration with 24 bins. These views complement boxplots: boxplots flag possible outliers, while histograms show how observations are distributed across the feature’s range. section 2.4.2

Histograms can also accelerate decision-tree training. Rather than test every distinct feature value as a possible split, histogram splitting uses bin boundary values as candidate thresholds. This reduces split-search work when a feature has many unique values, trading exhaustive candidate evaluation for a summarized search. section 5.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 unique-value threshold search with a configurable histogram-bin search, then scores each candidate using Gini impurity. section 5.4.3

Latitude split search

For a property-price classification task, a 256-bin search over latitude produced the same reported threshold and score as the full unique-value search, while the timed run fell from 46.9 seconds to 563 milliseconds. section 5.4.3

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

How deep it goes
Explains histogram plots through feature examples and details a 256-bin split-search implementation with Gini scoring.
Examples
Uses Auto MPG horsepower and acceleration plots, plus latitude-based property-price classification split searches.
What you can do after
Apply it via pandas and NumPy to visualize numeric distributions or reduce decision-tree split-search candidates.
How it connects
Connects histograms with boxplots, outliers, exploratory data analysis, decision trees, gradient boosting, split points, and Gini impurity.

Also explained in

Compare how 11 books explain histogram →

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.