Python 3.13+Numbers & mathfunction
statistics.kde
statistics
Kernel density estimation: returns a density function estimated from data (3.13+).
Syntax
statistics.kde(data, h, kernel='normal')
The bandwidth h decides everything: too small and you get a spike over every observation, too large and the data smears into a single hump — it is usually tuned by eye on a plot. What comes back is a function, not numbers: call it at the grid points you want, and pass cumulative=True if you need the distribution function instead of the density.
Examples
import statistics
print(not hasattr(statistics, 'kde') or callable(statistics.kde)) # → True
f = statistics.kde([1.0, 2.0, 3.0], h=1.0)
print(round(f(2.0), 4)) # → 0.2943
print(statistics.kde([0.0], h=1.0, kernel='uniform')(0.0)) # → 0.5
print(statistics.kde([], h=1.0)) # → StatisticsError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy