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

SciPy · Function

ndimage.gaussian_filter

Applies a multidimensional Gaussian filter to an array. It can smooth data, compute Gaussian derivatives, and control boundary handling and kernel size.

How it works

gaussian_filter smooths an array by applying Gaussian convolution independently along each selected axis. The sigma value controls the standard deviation of the kernel; order selects Gaussian derivatives, while mode, cval, truncate, and optional radius control boundaries and kernel extent.

Syntax

scipy.ndimage.gaussian_filter(input, sigma, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0, *, radius=None, axes=None)

Parameters

NameTypeDescription
inputarray_likeInput array.
sigmascalar or sequence of scalarsStandard deviation of the Gaussian kernel; scalar or one value per axis.
orderint or sequence of ints, optionalFilter order per axis; zero means Gaussian convolution and positive values mean Gaussian derivatives.
outputarray or dtype, optionalDestination array or dtype for the result.
modestr or sequence of str, optionalHow array borders are handled.
cvalscalar, optionalValue used beyond the borders when mode is constant.
truncatefloat, optionalNumber of standard deviations at which to truncate the filter.
radiusint or sequence of ints, optionalExplicit Gaussian-kernel radius; overrides truncate.
axestuple of int, optionalAxes over which to apply the filter.

Returns

ndarray: filtered array with the same shape as input.

Key points

  • The function smooths multidimensional array data with a Gaussian filter.
  • The default order, zero, performs ordinary Gaussian convolution.
  • Positive order values compute derivatives of the Gaussian instead of only smoothing.
  • Without output, the result is allocated using the input array's dtype.
  • The result preserves the input shape.
  • radius may be scalar or per-axis, allowing explicit kernel extents.

Common mistakes

  • Intermediate calculations use the output dtype, so integer or low-precision outputs can lose accuracy.
  • When radius is supplied, it determines kernel size and takes precedence over truncate.
  • A sequence sigma must match the axes being filtered; a scalar applies the same standard deviation to every axis.

Example: Blur a 2D array

import numpy as np
from scipy.ndimage import gaussian_filter

image = np.zeros((7, 7), dtype=float)
image[3, 3] = 1.0
blurred = gaussian_filter(image, sigma=1.0)
print(np.round(blurred, 3))

Creates a 2D impulse and smooths it with an isotropic Gaussian filter.

When to use it

Smoothing

Reduce high-frequency noise in images or multidimensional measurements.

Gaussian derivatives

Identify directional changes or edges using positive derivative orders.

Axis and kernel control

Restrict processing to selected dimensions or explicitly control the kernel footprint.

Interview questions

QHow does gaussian_filter implement multidimensional filtering?

It applies a multidimensional Gaussian filter by performing one-dimensional convolutions along the axes. With order=0 it performs Gaussian convolution; positive orders apply derivatives of the Gaussian.

Follow-up: How would you limit filtering to selected dimensions? Use axes to specify the dimensions, and provide compatible sigma, order, mode, or radius sequences for those axes.

QWhat does sigma control?

sigma is the Gaussian kernel standard deviation. A scalar uses the same value on every filtered axis, while a sequence supplies one value per axis.

Follow-up: What changes when radius is set? The kernel width becomes 2*radius+1 per axis, and truncate is ignored.

QWhat are the output and dtype behaviors?

output can be an array receiving the result or a dtype specifying the result type. If omitted, SciPy creates an array with the input dtype, and the returned array has the same shape as the input.

Follow-up: Why can this affect numeric quality? Intermediate arrays use the output dtype, so a narrow or integer dtype can make results imprecise.

  • scipy.ndimage.gaussian_filter1d
  • scipy.ndimage.gaussian_gradient_magnitude
  • scipy.ndimage.gaussian_laplace
  • scipy.ndimage.convolve

Source: gaussian_filter — SciPy v1.18.0 Manual. Checked 2026-10-15.