SplineTransformer
- class sklearn.preprocessing.SplineTransformer(n_knots=5, degree=3, *, knots='uniform', extrapolation='constant', include_bias=True, order, ,
Generate univariate B-spline bases for features.
Generate a new feature matrix consisting of
n_splines=n_knots + degree - 1 (n_knots - 1 for
extrapolation="periodic") spline basis functions
(B-splines) of polynomial order=`degree` for each feature.
In order to learn more about the SplineTransformer class go to: Time-related feature engineering
Read more in the User Guide.
Number of knots of the splines if knots equals one of
{‘uniform’, ‘quantile’}. Must be larger or equal 2. Ignored if knots
is array-like.
The polynomial degree of the spline basis. Must be a non-negative integer.
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 array-like 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.
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. option
extrapolate=True in scipy.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 “day-of-year” input feature. In this case it is
recommended to manually set the knot values to control the period.
If False, then the last spline element inside the data range of a feature is dropped. As B-splines 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 of output array in the dense case. 'F' order is faster to compute, but
may slow down subsequent estimators.
Specifies the way missing values are handled.
‘error’ : Raise an error if np.nan values are present during fit.
‘zeros’ : Encode splines of missing values with values 0.
Note that handle_missing='zeros' differs from first imputing missing values
with zeros and then creating the spline basis. The latter creates spline basis
functions which have non-zero values at the missing values
whereas this option simply sets all spline basis function values to zero at the
missing values.
Will return sparse CSR matrix if set True else will return an array.
List of BSplines objects, one for each feature.
The total number of input features.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
High degrees and a high number of knots can cause overfitting.
See examples/linear_model/plot_polynomial_interpolation.py.
>>> import numpy as np
>>> from sklearn.preprocessing import SplineTransformer
>>> X = np.arange(6).reshape(6, 1)
>>> spline = SplineTransformer(degree=
Compute knot positions of splines.
The data.
Ignored.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params
and returns a transformed version of X.
Get output feature names for transformation.
Input features.
If input_features is None, then feature_names_in_ is
used as feature names in. If is not defined,
then the following input feature names are generated:
.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Configure whether metadata should be requested to be passed to the method.
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
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.
Estimator parameters.
Transform each feature data to B-splines.
The data to transform.
The matrix of features, where n_splines is the number of bases elements of the B-splines, n_knots + degree - 1.
Visualizing the probabilistic predictions of a VotingClassifier
The total number of output features, which is computed as
n_features * n_splines, where n_splines is
the number of bases elements of the B-splines,
n_knots + degree - 1 for non-periodic splines and
n_knots - 1 for periodic ones.
If include_bias=False, then it is only
n_features * (n_splines - 1).
Individual weights for each sample. Used to calculate quantiles if
knots="quantile". For knots="uniform", zero weighted
observations are ignored for finding the min and max of X.
Fitted transformer.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Pass only if the estimator accepts additional params in its fit method.
Transformed array.
["x0", "x1", ..., "x(n_features_in_ - 1)"]If input_features is an array-like, then input_features must
match feature_names_in_ if feature_names_in_ is defined.
Transformed feature names.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to fit.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for sample_weight parameter in fit.
The updated object.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.