sklearn.preprocessing
.SplineTransformer¶

class
sklearn.preprocessing.
SplineTransformer
(n_knots=5, degree=3, *, knots='uniform', extrapolation='constant', include_bias=True, order='C')[source]¶ Generate univariate Bspline bases for features.
Generate a new feature matrix consisting of
n_splines=n_knots + degree  1
(n_knots  1
forextrapolation="periodic"
) spline basis functions (Bsplines) of polynomial order=`degree` for each feature.Read more in the User Guide.
New in version 1.0.
 Parameters
 n_knotsint, default=5
Number of knots of the splines if
knots
equals one of {‘uniform’, ‘quantile’}. Must be larger or equal 2. degreeint, default=3
The polynomial degree of the spline basis. Must be a nonnegative integer.
 knots{‘uniform’, ‘quantile’} or arraylike of shape (n_knots, n_features), default=’uniform’
Set knot positions such that first knot <= features <= last knot.
If ‘uniform’,
n_knots
number of knots are distributed uniformly from min to max values of the features.If ‘quantile’, they are distributed uniformly along the quantiles of the features.
If an arraylike is given, it directly specifies the sorted knot positions including the boundary knots. Note that, internally,
degree
number of knots are added before the first knot, the same after the last knot.
 extrapolation{‘error’, ‘constant’, ‘linear’, ‘continue’, ‘periodic’}, default=’constant’
If ‘error’, values outside the min and max values of the training features raises a
ValueError
. If ‘constant’, the value of the splines at minimum and maximum value of the features is used as constant extrapolation. If ‘linear’, a linear extrapolation is used. If ‘continue’, the splines are extrapolated as is, i.e. optionextrapolate=True
inscipy.interpolate.BSpline
. If ‘periodic’, periodic splines with a periodicity equal to the distance between the first and last knot are used. Periodic splines enforce equal function values and derivatives at the first and last knot. For example, this makes it possible to avoid introducing an arbitrary jump between Dec 31st and Jan 1st in spline features derived from a naturally periodic “dayofyear” input feature. In this case it is recommended to manually set the knot values to control the period. include_biasbool, default=True
If True (default), then the last spline element inside the data range of a feature is dropped. As Bsplines sum to one over the spline basis functions for each data point, they implicitly include a bias term, i.e. a column of ones. It acts as an intercept term in a linear models.
 order{‘C’, ‘F’}, default=’C’
Order of output array. ‘F’ order is faster to compute, but may slow down subsequent estimators.
 Attributes
 bsplines_list of shape (n_features,)
List of BSplines objects, one for each feature.
 n_features_in_int
The total number of input features.
 n_features_out_int
The total number of output features, which is computed as
n_features * n_splines
, wheren_splines
is the number of bases elements of the Bsplines,n_knots + degree  1
for nonperiodic splines andn_knots  1
for periodic ones. Ifinclude_bias=False
, then it is onlyn_features * (n_splines  1)
.
See also
KBinsDiscretizer
Transformer that bins continuous data into intervals.
PolynomialFeatures
Transformer that generates polynomial and interaction features.
Notes
High degrees and a high number of knots can cause overfitting.
See examples/linear_model/plot_polynomial_interpolation.py.
Examples
>>> import numpy as np >>> from sklearn.preprocessing import SplineTransformer >>> X = np.arange(6).reshape(6, 1) >>> spline = SplineTransformer(degree=2, n_knots=3) >>> spline.fit_transform(X) array([[0.5 , 0.5 , 0. , 0. ], [0.18, 0.74, 0.08, 0. ], [0.02, 0.66, 0.32, 0. ], [0. , 0.32, 0.66, 0.02], [0. , 0.08, 0.74, 0.18], [0. , 0. , 0.5 , 0.5 ]])
Methods
fit
(X[, y])Compute knot positions of splines.
fit_transform
(X[, y])Fit to data, then transform it.
get_feature_names
([input_features])Return feature names for output features.
get_params
([deep])Get parameters for this estimator.
set_params
(**params)Set the parameters of this estimator.
transform
(X)Transform each feature data to Bsplines.

fit
(X, y=None)[source]¶ Compute knot positions of splines.
 Parameters
 Xarraylike of shape (n_samples, n_features)
The data.
 yNone
Ignored.
 Returns
 selfobject
Fitted transformer.

fit_transform
(X, y=None, **fit_params)[source]¶ Fit to data, then transform it.
Fits transformer to
X
andy
with optional parametersfit_params
and returns a transformed version ofX
. Parameters
 Xarraylike of shape (n_samples, n_features)
Input samples.
 yarraylike of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
 **fit_paramsdict
Additional fit parameters.
 Returns
 X_newndarray array of shape (n_samples, n_features_new)
Transformed array.

get_feature_names
(input_features=None)[source]¶ Return feature names for output features.
 Parameters
 input_featureslist of str of shape (n_features,), default=None
String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.
 Returns
 output_feature_nameslist of str of shape (n_output_features,)

get_params
(deep=True)[source]¶ Get parameters for this estimator.
 Parameters
 deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
 Returns
 paramsdict
Parameter names mapped to their values.

set_params
(**params)[source]¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object. Parameters
 **paramsdict
Estimator parameters.
 Returns
 selfestimator instance
Estimator instance.

transform
(X)[source]¶ Transform each feature data to Bsplines.
 Parameters
 Xarraylike of shape (n_samples, n_features)
The data to transform.
 Returns
 XBSndarray of shape (n_samples, n_features * n_splines)
The matrix of features, where n_splines is the number of bases elements of the Bsplines, n_knots + degree  1.