API Reference¶
Summary¶
Smooths the univariate/multivariate/gridded data or computes the corresponding splines |
|
The result for auto smoothing for csaps function |
|
The interface class for smooting splines |
|
Cubic smoothing spline |
|
N-D grid cubic smoothing spline |
|
The interface class for spline representation in PP-form |
|
The base class for univariate/multivariate spline in piecewise polynomial form |
|
N-D grid spline representation in PP-form |
Main API¶
- csaps.csaps(xdata: Union[ndarray, Sequence[Number]], ydata: Union[ndarray, Sequence], *, weights: Optional[Union[ndarray, Sequence[Number]]] = None, smooth: Optional[float] = None, axis: Optional[int] = None, normalizedsmooth: bool = False) ISmoothingSpline ¶
- csaps.csaps(xdata: Union[ndarray, Sequence[Number]], ydata: Union[ndarray, Sequence], xidata: Union[ndarray, Sequence[Number]], *, weights: Optional[Union[ndarray, Sequence[Number]]] = None, axis: Optional[int] = None, normalizedsmooth: bool = False) AutoSmoothingResult
- csaps.csaps(xdata: Union[ndarray, Sequence[Number]], ydata: Union[ndarray, Sequence], xidata: Union[ndarray, Sequence[Number]], *, smooth: float, weights: Optional[Union[ndarray, Sequence[Number]]] = None, axis: Optional[int] = None, normalizedsmooth: bool = False) Union[ndarray, Sequence]
- csaps.csaps(xdata: Sequence[Union[ndarray, Sequence[Number]]], ydata: Union[ndarray, Sequence], *, weights: Optional[Sequence[Union[ndarray, Sequence[Number]]]] = None, smooth: Optional[Sequence[float]] = None, axis: Optional[int] = None, normalizedsmooth: bool = False) ISmoothingSpline
- csaps.csaps(xdata: Sequence[Union[ndarray, Sequence[Number]]], ydata: Union[ndarray, Sequence], xidata: Sequence[Union[ndarray, Sequence[Number]]], *, weights: Optional[Sequence[Union[ndarray, Sequence[Number]]]] = None, axis: Optional[int] = None, normalizedsmooth: bool = False) AutoSmoothingResult
- csaps.csaps(xdata: Sequence[Union[ndarray, Sequence[Number]]], ydata: Union[ndarray, Sequence], xidata: Sequence[Union[ndarray, Sequence[Number]]], *, smooth: Sequence[float], weights: Optional[Sequence[Union[ndarray, Sequence[Number]]]] = None, axis: Optional[int] = None, normalizedsmooth: bool = False) Union[ndarray, Sequence]
Smooths the univariate/multivariate/gridded data or computes the corresponding splines
This function might be used as the main API for smoothing any data.
- Parameters:
- xdatanp.ndarray, array-like
- The data sites
x1 < x2 < ... < xN
: 1-D data vector/sequence (array-like) for univariate/multivariate
ydata
caseThe sequence of 1-D data vectors for nd-gridded
ydata
case
- The data sites
- ydatanp.ndarray, array-like
- The data values:
1-D data vector/sequence (array-like) for univariate data case
N-D array/array-like for multivariate data case
N-D array for nd-gridded data case
- xidata[Optional] np.ndarray, array-like, Sequence[array-like]
- The data sites for output smoothed data:
1-D data vector/sequence (array-like) for univariate/multivariate
ydata
caseThe sequence of 1-D data vectors for nd-gridded
ydata
case
If this argument was not set, the function will return computed spline for given data in
ISmoothingSpline
object.- weights[Optional] np.ndarray, array-like, Sequence[array-like]
- The weights data vectors:
1-D data vector/sequence (array-like) for univariate/multivariate
ydata
caseThe sequence of 1-D data vectors for nd-gridded
ydata
case
- smooth[Optional] float, Sequence[float]
- The smoothing factor value(s):
float value in the range
[0, 1]
for univariate/multivariateydata
casethe sequence of float in the range
[0, 1]
or None for nd-griddedydata
case
If this argument was not set or None or sequence with None-items, the function will return named tuple
AutoSmoothingResult
with computed smoothed data values and smoothing factor value(s).- axis[Optional] int
The
ydata
axis. Axis along whichydata
is assumed to be varying. If this argument was not set the last axis will be used (axis == -1
).Note
Currently, axis will be ignored for nd-gridded
ydata
case.- normalizedsmooth[Optional] bool
If True, the smooth parameter is normalized such that results are invariant to xdata range and less sensitive to nonuniformity of weights and xdata clumping
New in version 1.1.0.
- Returns:
- yidatanp.ndarray
Smoothed data values if
xidata
andsmooth
were set.- autosmoothing_resultAutoSmoothingResult
- The named tuple object with two fileds:
‘values’ – smoothed data values
‘smooth’ – computed smoothing factor
This result will be returned if
xidata
was set andsmooth
was not set.- ssp_objISmoothingSpline
- Smoothing spline object if
xidata
was not set: CubicSmoothingSpline
instance for univariate/multivariate dataNdGridCubicSmoothingSpline
instance for nd-gridded data
- Smoothing spline object if
Examples
Univariate data smoothing
import numpy as np from csaps import csaps x = np.linspace(-5., 5., 25) y = np.exp(-(x/2.5)**2) + (np.random.rand(25) - 0.2) * 0.3 xi = np.linspace(-5., 5., 150) # Smooth data with smoothing factor 0.85 yi = csaps(x, y, xi, smooth=0.85) # Smooth data and compute smoothing factor automatically yi, smooth = csaps(x, y, xi) # Do not evaluate the spline, only compute it sp = csaps(x, y, smooth=0.98)
Object-Oriented API¶
- class csaps.CubicSmoothingSpline(xdata: Union[ndarray, Sequence[Number]], ydata: Union[ndarray, Sequence], weights: Optional[Union[ndarray, Sequence[Number]]] = None, smooth: Optional[float] = None, axis: int = -1, normalizedsmooth: bool = False)¶
Bases:
ISmoothingSpline
[SplinePPForm
,float
,Union
[ndarray
,Sequence
[Number
]],int
,Union
[bool
,str
]]Cubic smoothing spline
The cubic spline implementation for univariate/multivariate data.
- Parameters:
- xdatanp.ndarray, sequence, vector-like
X input 1-D data vector (data sites:
x1 < x2 < ... < xN
)- ydatanp.ndarray, vector-like, sequence[vector-like]
Y input 1-D data vector or ND-array with shape[axis] equal of xdata size)
- weights[Optional] np.ndarray, list
Weights 1-D vector with size equal of
xdata
size- smooth[Optional] float
- Smoothing parameter in range [0, 1] where:
0: The smoothing spline is the least-squares straight line fit
1: The cubic spline interpolant with natural condition
- axis[Optional] int
Axis along which
ydata
is assumed to be varying. Meaning that for x[i] the corresponding values are np.take(ydata, i, axis=axis). By default is -1 (the last axis).- normalizedsmooth[Optional] bool
If True, the smooth parameter is normalized such that results are invariant to xdata range and less sensitive to nonuniformity of weights and xdata clumping
New in version 1.1.0.
- __call__(x: Union[ndarray, Sequence[Number]], nu: Optional[int] = None, extrapolate: Optional[Union[bool, str]] = None) ndarray ¶
Evaluate the spline for given data
- Parameters:
- x1-d array-like
Points to evaluate the spline at.
- nu[Optional] int
Order of derivative to evaluate. Must be non-negative.
- extrapolate[Optional] bool or ‘periodic’
If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If ‘periodic’, periodic extrapolation is used. Default is True.
Notes
Derivatives are evaluated piecewise for each polynomial segment, even if the polynomial is not differentiable at the breakpoints. The polynomial intervals are considered half-open,
[a, b)
, except for the last interval which is closed[a, b]
.
- property smooth: float¶
Returns the smoothing factor
- Returns:
- smoothfloat
Smoothing factor in the range [0, 1]
- property spline: SplinePPForm¶
Returns the spline description in SplinePPForm instance
- Returns:
- splineSplinePPForm
The spline representation in
SplinePPForm
instance
- class csaps.NdGridCubicSmoothingSpline(xdata: Sequence[Union[ndarray, Sequence[Number]]], ydata: ndarray, weights: Optional[Union[ndarray, Sequence[Number], Sequence[Union[ndarray, Sequence[Number]]]]] = None, smooth: Optional[Union[float, Sequence[Optional[float]]]] = None, normalizedsmooth: bool = False)¶
Bases:
ISmoothingSpline
[NdGridSplinePPForm
,Tuple
[float
, …],Sequence
[Union
[ndarray
,Sequence
[Number
]]],Tuple
[int
, …],bool
]N-D grid cubic smoothing spline
Class implements N-D grid data smoothing (piecewise tensor product polynomial).
- Parameters:
- xdatalist, tuple, Sequence[vector-like]
X data site vectors for each dimensions. These vectors determine ND-grid. For example:
# 2D grid x = [ np.linspace(0, 5, 21), np.linspace(0, 6, 25), ]
- ydatanp.ndarray
Y data ND-array with shape equal
xdata
vector sizes- weights[Optional] list, tuple, Sequence[vector-like]
Weights data vector(s) for all dimensions or each dimension with size(s) equal to
xdata
sizes- smooth[Optional] float, Sequence[float]
- The smoothing parameter (or a sequence of parameters for each dimension) in range
[0, 1]
where: 0: The smoothing spline is the least-squares straight line fit
1: The cubic spline interpolant with natural condition
- The smoothing parameter (or a sequence of parameters for each dimension) in range
- normalizedsmooth[Optional] bool
If True, the smooth parameter is normalized such that results are invariant to xdata range and less sensitive to nonuniformity of weights and xdata clumping
New in version 1.1.0.
- __call__(x: Union[Sequence[Union[ndarray, Sequence[Number]]], Sequence[Number]], nu: Optional[Tuple[int, ...]] = None, extrapolate: Optional[bool] = None) ndarray ¶
Evaluate the spline for given data
- Parameters:
- xtuple of 1-d array-like
The tuple of point values for each dimension to evaluate the spline at.
- nu[Optional] tuple of int
Orders of derivatives to evaluate. Each must be non-negative.
- extrapolate[Optional] bool
Whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs.
- Returns:
- yarray-like
Interpolated values. Shape is determined by replacing the interpolation axis in the original array with the shape of x.
- property smooth: Tuple[float, ...]¶
Returns a tuple of smoothing parameters for each axis
- Returns:
- smoothTuple[float, …]
The smoothing parameter in the range
[0, 1]
for each axis
- property spline: NdGridSplinePPForm¶
Returns the spline description in ‘NdGridSplinePPForm’ instance
- Returns:
- splineNdGridSplinePPForm
The spline description in
NdGridSplinePPForm
instance
- class csaps.SplinePPForm(c, x, extrapolate=None, axis=0)¶
Bases:
ISplinePPForm
[ndarray
,int
],PPoly
The base class for univariate/multivariate spline in piecewise polynomial form
Piecewise polynomial in terms of coefficients and breakpoints.
Notes
Inherited from
scipy.interpolate.PPoly
- property breaks: ndarray¶
Returns the breaks for the spline
- Returns:
- breaksUnion[np.ndarray, ty.Tuple[np.ndarray, …]]
Breaks data
- property coeffs: ndarray¶
Returns the spline coefficients
- Returns:
- coeffsnp.ndarray
Coefficients n-d array
- property ndim: int¶
Returns the number of spline dimensions
The number of dimensions is product of shape without
shape[self.axis]
.
- property order: int¶
Returns the spline order
- Returns:
- orderty.Union[int, ty.Tuple[int, …]]
The spline order
- property pieces: int¶
Returns the spline pieces data
- Returns:
- piecesty.Union[int, ty.Tuple[int, …]]
The spline pieces data
- property shape: Tuple[int]¶
Returns the source data shape
- class csaps.NdGridSplinePPForm(c, x, extrapolate=None)¶
Bases:
ISplinePPForm
[Tuple
[ndarray
, …],Tuple
[int
, …]],NdPPoly
N-D grid spline representation in PP-form
N-D grid spline is represented in piecewise tensor product polynomial form.
Notes
Inherited from
scipy.interpolate.NdPPoly
- property breaks: Tuple[ndarray, ...]¶
Returns the breaks for the spline
- Returns:
- breaksUnion[np.ndarray, ty.Tuple[np.ndarray, …]]
Breaks data
- property coeffs: ndarray¶
Returns the spline coefficients
- Returns:
- coeffsnp.ndarray
Coefficients n-d array
- property ndim: int¶
Returns the spline dimension count
- Returns:
- ndimint
The spline dimension count
- property order: Tuple[int, ...]¶
Returns the spline order
- Returns:
- orderty.Union[int, ty.Tuple[int, …]]
The spline order
- property pieces: Tuple[int, ...]¶
Returns the spline pieces data
- Returns:
- piecesty.Union[int, ty.Tuple[int, …]]
The spline pieces data
- property shape: Tuple[int, ...]¶
Returns the source data shape
- Returns:
- shapetuple of int
The source data shape
Interfaces¶
- class csaps.ISmoothingSpline(*args, **kwds)¶
Bases:
ABC
,Generic
[TSpline
,TSmooth
,TXi
,TNu
,TExtrapolate
]The interface class for smooting splines
- abstract __call__(xi: TXi, nu: Optional[TNu] = None, extrapolate: Optional[TExtrapolate] = None) ndarray ¶
Evaluates spline on the data sites
- abstract property smooth: TSmooth¶
Returns smoothing factor(s)
- abstract property spline: TSpline¶
Returns spline representation in PP-form
- class csaps.ISplinePPForm(*args, **kwds)¶
Bases:
ABC
,Generic
[TData
,TProps
]The interface class for spline representation in PP-form
- abstract property breaks: TData¶
Returns the breaks for the spline
- Returns:
- breaksUnion[np.ndarray, ty.Tuple[np.ndarray, …]]
Breaks data
- abstract property coeffs: ndarray¶
Returns the spline coefficients
- Returns:
- coeffsnp.ndarray
Coefficients n-d array
- abstract property ndim: int¶
Returns the spline dimension count
- Returns:
- ndimint
The spline dimension count
- abstract property order: TProps¶
Returns the spline order
- Returns:
- orderty.Union[int, ty.Tuple[int, …]]
The spline order
- abstract property pieces: TProps¶
Returns the spline pieces data
- Returns:
- piecesty.Union[int, ty.Tuple[int, …]]
The spline pieces data
- abstract property shape: Tuple[int]¶
Returns the source data shape
- Returns:
- shapetuple of int
The source data shape