liveBook · Manning

Kernel density estimate

As explained in Outlier Detection in Python

A kernel density estimate is a nonparametric probability-density estimate formed by smoothing contributions from sample observations.

A kernel density estimate (KDE) describes where numeric observations are concentrated without requiring one fixed distributional shape. Place a small kernel, such as a Gaussian-shaped curve, at each observation and combine the curves. The resulting density is high where observations gather and low in sparse regions. section 2.1.6

For outlier detection, low density can identify values between groups as well as values beyond the extremes. Consider purchases concentrated near $10 and $500: a $100 purchase may be unusual even though it is not exceptionally small or large. KDE, histograms, and nearest-neighbor calculations can handle such internal outliers more flexibly than tests built around a single average and variance. A low-density value is a candidate for investigation, not proof of a problem. section 2.1.6 section 2.9

Figure 2.8 Given a dataset, here containing 10 points, KDE can estimate the distribution by creating a small kernel—in this case, a Gaussian shape—at each point and summing these. The three panes show the effect of adjusting the widths of the kernel. With narrow kernels, there is less summing of the kernels; the final KDE estimate resembles the individual kernels and fits the data tightly. With wider kernels, the KDE estimate becomes smoother. We wish to find a balance with an appropriate bandwidth. In this case, the first or second panes or somewhere in between may work best.

The ten-point illustration shows why bandwidth matters: narrow kernels follow individual observations closely, while wider kernels produce a smoother estimate. Neither extreme is automatically the right fit. section 2.1.6

Score sparse values in one column

For the hue-mean column of the segment dataset, reshape the values into one-column input and fit scikit-learn’s KernelDensity with a Gaussian kernel and bandwidth of 0.2. Score each observation, then flag scores below a threshold set at the first score quartile minus 2.2 times the interquartile range. Plotting the flagged values against a histogram checks whether they lie in sparse regions. The bandwidth and threshold are choices to inspect, not universal settings; seaborn’s histplot with kde=True offers a quick view of the density shape. section 2.1.6

A single density estimate across a whole dataset may be misleading when regions have different densities. One alternative is to cluster the data, estimate density within each cluster, and compare a point’s KDE with the average KDE of its cluster. Ratios make those comparisons more meaningful across clusters than raw scores. Because clustering can change across runs or plausible cluster counts, averaging results from multiple clusterings can improve stability, though it does not remove dependence on clustering quality. section 7.8.2

KDE also has a different use: generating synthetic values that resemble an observed feature’s distribution. Generating features independently this way does not, by itself, preserve associations between columns. For detection through a common interface, PyOD’s KDE detector wraps scikit-learn’s KernelDensity and exposes fitted training scores through decision_scores_. section 11.2.2 section 6.1

How Outlier Detection in Python covers it

How deep it goes
Explains kernels and bandwidth visually, then works through fitting, scoring, and thresholding; does not derive the estimator mathematically.
Examples
Uses segment-data hue-mean scores, bandwidth illustrations, and repeated k-means clusterings.
What it assumes
Builds the kernel and bandwidth intuition from observations while assuming familiarity with numeric columns, quartiles, and Python data workflows.
What you can do after
Apply it via scikit-learn’s KernelDensity to score one column, or via PyOD’s KDE detector to obtain training scores.
How it connects
Connects KDE to histograms, nearest-neighbor outlier detection, clustering, and synthetic data generation.

Also explained in

Compare how 2 books explain kernel density estimate →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.