To use Python’s scipy.stats.gaussian_kde, pass it observed sample values, then evaluate the fitted density at the points you want to inspect. For one variable, provide a one-dimensional array; for multiple variables, arrange the data as dimensions by observations. The main modeling choice is bandwidth: SciPy defaults to Scott’s rule, but that is a starting point rather than a universally best setting.
Fit a KDE and evaluate it on a grid
A kernel density estimate (KDE) uses smooth kernels centered on observed samples to estimate a probability density. This minimal example fits a univariate estimate and evaluates it across a grid:
As an Amazon Associate I earn from qualifying purchases.
import numpy as np
from scipy.stats import gaussian_kde
samples = np.array([1.2, 1.5, 1.7, 2.0, 2.4, 2.8])
kde = gaussian_kde(samples) # Scott's rule is the default
grid = np.linspace(samples.min() - 1, samples.max() + 1, 200)
density = kde(grid)
density contains estimated density values corresponding to the locations in grid. You can also call kde.evaluate(grid); calling the fitted object is a shorthand for evaluating it.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Arrange the data correctly
One variable
For univariate observations, pass a one-dimensional array, with one sample per entry, as in the example above.
#1 Best Overall
Multiple variables
For multivariate data, SciPy expects an array shaped (number of dimensions, number of samples). Each row is a variable, and each column is one observation. For two variables measured over N observations, the shape is (2, N), not (N, 2). See the SciPy gaussian_kde API reference for the documented input shape and parameters.
Choose and compare the bandwidth
The bandwidth controls how much the estimate smooths the data. Too much smoothing can hide meaningful modes or local structure; too little can leave a noisy curve. SciPy cautions that its estimator works best for unimodal distributions and that multimodal distributions tend to be oversmoothed. Compare plausible choices against the same data and grid, considering how many features remain visible and how smooth or noisy the result looks.
Rank #2
Built-in rules and custom factors
With bw_method=None, SciPy uses Scott’s rule. The documented choices also include 'scott', 'silverman', a scalar factor, or a callable. Scott’s factor is n**(-1. / (d + 4)), where n is the sample count and d is the number of dimensions. SciPy’s multivariate Silverman factor is (n * (d + 2) / 4.)**(-1. / (d + 4)).
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchA scalar passed as bw_method is a factor, not a bandwidth in the measurement units of your data. SciPy forms the kernel covariance by multiplying the data covariance by factor**2. Consequently, a scalar changes the smoothing relative to the data covariance rather than specifying a fixed width such as “0.5 units.” The formulas and parameter behavior are described in the API reference.
Compare Scott, Silverman, and a scalar on the same grid
kde = gaussian_kde(samples) # Scott's rule
scott_density = kde(grid)
kde.set_bandwidth(bw_method="silverman")
silverman_density = kde(grid)
kde.set_bandwidth(bw_method=0.5) # scalar factor, not data units
custom_density = kde(grid)
Here, 0.5 is an illustrative scalar factor, not a recommended value for every dataset. For a fair visual comparison, plot each returned array against the same grid. SciPy’s set_bandwidth reference documents changing the bandwidth and demonstrates comparing built-in rules with a scalar. The best choice depends on the data and the goal; SciPy notes cross-validation and plug-in approaches as other possible selection methods, without identifying one method as universally best.
Use weights when observations should not count equally
If some observations should contribute more than others, provide sample weights matching the dataset shape. Without weights, samples are equally weighted. SciPy’s documented Scott and Silverman factors use the effective sample count, neff, for unequal weights rather than the raw count n. Check the API reference for the current signature and weight details.
Use the fitted KDE for other tasks
After fitting, choose a method that matches the calculation you need:
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitcheskde(points)orkde.evaluate(points)returns estimated density values at points.kde.logpdf(points)returns log-density values.kde.resample(...)draws samples from the estimated density.kde.integrate_box_1d(low, high)integrates a univariate estimate over an interval.kde.integrate_box(low_bounds, high_bounds)integrates over a rectangular region.kde.integrate_gaussian(mean, cov)integrates the KDE against a multivariate Gaussian; the mean and covariance dimensions must match the KDE.kde.integrate_kde(other)integrates the product of two KDEs. SciPy documents aValueErrorif the estimates have different dimensionality.
For details on the product integral’s behavior, see the SciPy integrate_kde reference.
Best Value
Check the SciPy version behind the API details
The cited class, bandwidth, and product-integral references are for SciPy 1.16.0, 1.18.0, and 1.17.0 respectively. These documentation versions do not establish which version is installed in a particular environment. If an argument or method behaves differently than expected, check your installed SciPy version and consult its matching documentation.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




