Non-parametric density estimation
Estimate and visualize an unknown distribution from observed samples without choosing a named parametric family.
SciPy · Function
scipy.stats.gaussian_kde constructs a non-parametric probability-density estimate from observations using Gaussian kernels. It supports univariate and multivariate data, automatic bandwidth selection, optional weights, and callable evaluation.
The class builds a non-parametric probability-density estimate by placing Gaussian kernels over the supplied observations. It supports one- and multi-dimensional data, chooses a bandwidth automatically by default, and can be evaluated by calling the resulting object with query points.
scipy.stats.gaussian_kde(dataset, bw_method=None, weights=None)| Name | Type | Description |
|---|---|---|
dataset | array_like | Datapoints to estimate from. Use a one-dimensional array for univariate data or a two-dimensional array shaped as dimensions by data points for multivariate data. |
bw_method | str, scalar or callable, optional | Bandwidth-factor selection method. Accepted forms include 'scott', 'silverman', a scalar constant, or a callable; the default is automatic selection. |
weights | array_like, optional | Weights assigned to datapoints; must have the same shape as dataset. |
A gaussian_kde object representing the fitted Gaussian kernel-density estimate; the page documents this class through its callable evaluation and related methods.
import numpy as np
from scipy.stats import gaussian_kde
samples = np.array([0.0, 0.5, 1.0, 1.5, 2.0])
kde = gaussian_kde(samples)
points = np.array([0.25, 1.25])
densities = kde(points)
print(densities)Construct a one-dimensional Gaussian kernel density estimate and evaluate its estimated PDF at two points.
Estimate and visualize an unknown distribution from observed samples without choosing a named parametric family.
Evaluate likelihood-density values at scalar or multidimensional query locations.
Generate synthetic observations resembling the fitted density.
Compute probability mass or overlap-related integrals using the documented integration methods.
It estimates a probability density non-parametrically by using Gaussian kernels centered on the observations. The class supports both univariate and multivariate datasets and determines a bandwidth automatically unless configured otherwise.
Follow-up: How would you control smoothing? Pass 'scott', 'silverman', a scalar, or a callable through bw_method; smaller bandwidth generally produces a less smooth estimate.
For one-dimensional data, dataset is a one-dimensional array. For multivariate data, it is a two-dimensional array whose dimensions are arranged as number of dimensions by number of observations.
Follow-up: What must be true of weights? They must have the same shape as dataset.
The constructed object can be called with evaluation points to obtain estimated density values; evaluate is also available for this operation. The object additionally provides integration, resampling, and covariance-related methods.
Follow-up: Name one integration capability: integrate_box_1d integrates a one-dimensional estimate between two bounds, while integrate_box handles a rectangular multivariate region.
scipy.stats.gaussian_kde.evaluatescipy.stats.gaussian_kde.resamplescipy.stats.gaussian_kde.integrate_boxscipy.stats.gaussian_kde.integrate_box_1dscipy.stats.gaussian_kde.integrate_gaussianscipy.stats.gaussian_kde.integrate_kdescipy.stats.gaussian_kde.covariance_factorscipy.stats.gaussian_kde.scotts_factor