This is a pre-release version (2.0.0.dev0+git20260907.41eeb59).Go to latest (1.18.0)
{ } Raw JSON

bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / interpolate / _ndgriddata / NearestNDInterpolator

class

scipy.interpolate._ndgriddata:NearestNDInterpolator

source: build-install/usr/lib/python3.14/site-packages/scipy/interpolate/_ndgriddata.py:20

Signature

class NearestNDInterpolator(x, y, rescale = False, tree_options = None)

Members

Summary

Nearest-neighbor interpolator in N > 1 dimensions.

Parameters

x : (npoints, ndims) 2-D ndarray of floats

Data point coordinates.

y : (npoints, ...) N-D ndarray of float or complex

Data values. The length of y along the first axis must be equal to the length of x.

rescale : bool, optional

Rescale points to unit cube before performing interpolation. This is useful if some of the input dimensions have incommensurable units and differ by many orders of magnitude.

tree_options : dict, optional

Options passed to the underlying KDTree.

Methods

__call__

Notes

Uses scipy.spatial.KDTree

Examples

We can interpolate values on a 2D plane:
from scipy.interpolate import NearestNDInterpolator
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng()
x = rng.random(10) - 0.5
y = rng.random(10) - 0.5
z = np.hypot(x, y)
X = np.linspace(min(x), max(x))
Y = np.linspace(min(y), max(y))
X, Y = np.meshgrid(X, Y)  # 2D grid for interpolation
interp = NearestNDInterpolator(list(zip(x, y)), z)
Z = interp(X, Y)
✓
plt.pcolormesh(X, Y, Z, shading='auto')
plt.plot(x, y, "ok", label="input point")
plt.legend()
plt.colorbar()
plt.axis("equal")
✗
(np.float64(-0.4370756110313493), np.float64(0.38013595940277656), np.float64(-0.1808950586627801), np.float64(0.5047502333805315))
plt.show()
✓
fig-53927f525a4f3d6e.png

See also

CloughTocher2DInterpolator

Piecewise cubic, C1 smooth, curvature-minimizing interpolator in 2D.

LinearNDInterpolator

Piecewise linear interpolator in N dimensions.

RegularGridInterpolator

Interpolator on a regular or rectilinear grid in arbitrary dimensions (interpn wraps this class).

griddata

Interpolate unstructured D-D data.

interpn

Interpolation on a regular grid or rectilinear grid.

Aliases

  • scipy.interpolate.NearestNDInterpolator