Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.gaussian_kde

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.

How it works

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.

Syntax

scipy.stats.gaussian_kde(dataset, bw_method=None, weights=None)

Parameters

NameTypeDescription
datasetarray_likeDatapoints 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_methodstr, scalar or callable, optionalBandwidth-factor selection method. Accepted forms include 'scott', 'silverman', a scalar constant, or a callable; the default is automatic selection.
weightsarray_like, optionalWeights assigned to datapoints; must have the same shape as dataset.

Returns

A gaussian_kde object representing the fitted Gaussian kernel-density estimate; the page documents this class through its callable evaluation and related methods.

Key points

  • gaussian_kde represents a kernel-density estimate formed from Gaussian kernels.
  • It estimates a random variable's PDF without assuming a fixed parametric distribution.
  • The same class supports one-dimensional and multidimensional datasets.
  • Bandwidth selection is automatic by default, which controls kernel smoothing.
  • bw_method accepts Scott's rule, Silverman's rule, a numeric factor, or a callable.
  • Optional observation weights can be supplied when constructing the estimate.
  • Use evaluation methods to calculate the estimated PDF at requested points.

Common mistakes

  • Multimodal data may be oversmoothed, so the default estimate can hide separate modes.
  • Use a one-dimensional array for univariate observations and a dimensions-by-observations array for multivariate data.
  • When supplying weights, their shape must match the dataset.

Example: Evaluate a Gaussian KDE

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.

When to use it

Non-parametric density estimation

Estimate and visualize an unknown distribution from observed samples without choosing a named parametric family.

Density evaluation

Evaluate likelihood-density values at scalar or multidimensional query locations.

Resampling

Generate synthetic observations resembling the fitted density.

Density integration

Compute probability mass or overlap-related integrals using the documented integration methods.

Interview questions

QWhat does gaussian_kde represent?

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.

QWhat input shape does the constructor require?

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.

QHow is the estimated density evaluated?

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.evaluate
  • scipy.stats.gaussian_kde.resample
  • scipy.stats.gaussian_kde.integrate_box
  • scipy.stats.gaussian_kde.integrate_box_1d
  • scipy.stats.gaussian_kde.integrate_gaussian
  • scipy.stats.gaussian_kde.integrate_kde
  • scipy.stats.gaussian_kde.covariance_factor
  • scipy.stats.gaussian_kde.scotts_factor

Source: Gaussian_kde. Checked 2026-10-15.