bundles / scipy 1.17.1 / scipy / interpolate / _bsplines / make_interp_spline
function
scipy.interpolate._bsplines:make_interp_spline
Signature
def make_interp_spline ( x , y , k = 3 , t = None , bc_type = None , axis = 0 , check_finite = True ) Summary
Create an interpolating B-spline with specified degree and boundary conditions.
Parameters
x: array_like, shape (n,)Abscissas.
y: array_like, shape (n, ...)Ordinates.
k: int, optionalB-spline degree. Default is cubic,
k = 3.t: array_like, shape (nt + k + 1,), optional.Knots. The number of knots needs to agree with the number of data points and the number of derivatives at the edges. Specifically,
nt - nmust equallen(deriv_l) + len(deriv_r).bc_type: 2-tuple or NoneBoundary conditions. Default is None, which means choosing the boundary conditions automatically. Otherwise, it must be a length-two tuple where the first element (
deriv_l) sets the boundary conditions atx[0]and the second element (deriv_r) sets the boundary conditions atx[-1]. Each of these must be an iterable of pairs(order, value)which gives the values of derivatives of specified orders at the given edge of the interpolation interval. Alternatively, the following string aliases are recognized:"clamped": The first derivatives at the ends are zero. This is equivalent tobc_type=([(1, 0.0)], [(1, 0.0)])."natural": The second derivatives at ends are zero. This is equivalent tobc_type=([(2, 0.0)], [(2, 0.0)])."not-a-knot"(default): The first and second segments are the same polynomial. This is equivalent to havingbc_type=None."periodic": The values and the firstk-1derivatives at the ends are equivalent.
axis: int, optionalInterpolation axis. Default is 0.
check_finite: bool, optionalWhether to check that the input arrays contain only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs. Default is True.
Returns
b: `BSpline` objectA BSpline object of the degree
kand with knotst.
Notes
Array API Standard Support
make_interp_spline has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
Use cubic interpolation on Chebyshev nodes:import numpy as np import matplotlib.pyplot as plt def cheb_nodes(N): jj = 2.*np.arange(N) + 1 x = np.cos(np.pi * jj / 2 / N)[::-1] return x✓
x = cheb_nodes(20) y = np.sqrt(1 - x**2)✓
from scipy.interpolate import BSpline, make_interp_spline b = make_interp_spline(x, y) np.allclose(b(x), y)✓
b.k
✓l, r = [(2, 0.0)], [(2, 0.0)] b_n = make_interp_spline(x, y, bc_type=(l, r)) # or, bc_type="natural" np.allclose(b_n(x), y) x0, x1 = x[0], x[-1] np.allclose([b_n(x0, 2), b_n(x1, 2)], [0, 0])✓
phi = np.linspace(0, 2.*np.pi, 40) r = 0.3 + np.cos(phi) x, y = r*np.cos(phi), r*np.sin(phi) # convert to Cartesian coordinates✓
spl = make_interp_spline(phi, np.c_[x, y])
✓phi_new = np.linspace(0, 2.*np.pi, 100) x_new, y_new = spl(phi_new).T✓
plt.plot(x, y, 'o') plt.plot(x_new, y_new, '-')✗
plt.show()
✓
x = np.linspace(0, 2*np.pi, 10) y = np.array([np.sin(x), np.cos(x)])✓
ax = plt.axes(projection='3d') xx = np.linspace(0, 2*np.pi, 100) bspl = make_interp_spline(x, y, k=5, bc_type='periodic', axis=1)✓
ax.plot3D(xx, *bspl(xx)) ax.scatter3D(x, *y, color='red')✗
plt.show()
✓
See also
- BSpline
base class representing the B-spline objects
- CubicSpline
a cubic spline in the polynomial basis
- UnivariateSpline
a wrapper over FITPACK spline fitting routines
- make_lsq_spline
a similar factory function for spline fitting
- splrep
a wrapper over FITPACK spline fitting routines
Aliases
-
scipy.interpolate.make_interp_spline