P-Splines#
P-Splines#
- class pyspline.psplines.PSplines(penalty: Tuple[float] = (1.0,), *, n_segments: Tuple[int] = (10,), degree: Tuple[int] = (3,), order_penalty: int = 2)#
Bases:
BaseEstimator,RegressorMixinP-Splines Smoothing.
- Parameters:
- penalty: Tuple[float], default=(1.0,)
A tuple of penalty parameters for each dimension.
- n_segments: Tuple[int], default=(10,)
The number of evenly spaced segments.
- degree: Tuple[int], default=(3,)
The number of the degree of the basis.
- order_penalty: int, default=2
The number of the order of the difference penalty.
Notes
This code is adapted from _[2]. See [1] for more details.
References
[1]Eilers, P., Marx, B.D., (2021) Practical Smoothing: The Joys of P-splines. Cambridge University Press, Cambridge.
[2]Eilers, P., Marx, B., Li, B., Gampe, J., Rodriguez-Alvarez, M.X., (2023) JOPS: Practical Smoothing with P-Splines.
Examples
>>> from skltemplate import TemplateEstimator >>> import numpy as np >>> X = np.arange(100).reshape(100, 1) >>> y = np.zeros((100, )) >>> estimator = TemplateEstimator() >>> estimator.fit(X, y) TemplateEstimator()Methods
derivative(X[, order_derivative])Estimate the derivative of the data.
errors(X)Estimate the standard errors of the fitted values.
fit(X, y[, sample_weights, domains])Fit a P-splines model to the given data.
get_metadata_routing()Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
predict(X)Predict the response variable values.
score(X, y[, sample_weight])Return the coefficient of determination of the prediction.
set_fit_request(*[, domains, sample_weights])Request metadata passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
set_score_request(*[, sample_weight])Request metadata passed to the
scoremethod.- fit(X: ndarray[Any, dtype[float64]], y: ndarray[Any, dtype[float64]], sample_weights: ndarray[Any, dtype[float64]] | None = None, domains: list[tuple[float64]] | tuple[float64] | None = None) PSplines#
Fit a P-splines model to the given data.
The method fits a P-splines model to the given data using a B-splines basis and an optional weights matrix.
- Parameters:
- X: npt.NDArray[np.float_], shape=(n_obs, n_dimension)
An array containing the predictor variable values.
- y: npt.NDArray[np.float_], shape=(n_obs,)
An array containing the response variable values.
- sample_weights: npt.NDArray[np.float64] | None, default=None
An array of shape (n_obs,) containing the weights for each observation. If not provided, all observations are assumed to have equal weight.
- domains: list[tuple[np.float_]] | tuple[np.float_] | None, default=None
The domains of the B-splines basis.
- Returns:
- self: PSplines
Returns self.
- predict(X: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[float64]]#
Predict the response variable values.
The method predicts the response variable values for the given predictor variable values using the fitted P-splines model. If X is not provided, the method returns the fitted values.
- Parameters:
- X: npt.NDArray[np.float_]
An array containing the predictor variable values.
- Returns:
- npt.NDArray[np.float_]
An array containing the estimated response variable values.
- errors(X: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[float64]]#
Estimate the standard errors of the fitted values.
- Parameters:
- X: npt.NDArray[np.float_]
An array containing the predictor variable values.
- Returns:
- npt.NDArray[np.float_]
An array containing standard errors of the fitted values.
- derivative(X: ndarray[Any, dtype[float64]], order_derivative: int = 1)#
Estimate the derivative of the data.
- Parameters:
- X: npt.NDArray[np.float_]
An array containing the predictor variable values.
- order_derivative: int, default=1
Order of the derivative to compute.
- set_fit_request(*, domains: bool | None | str = '$UNCHANGED$', sample_weights: bool | None | str = '$UNCHANGED$') PSplines#
Request metadata passed to the
fitmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
- domainsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
domainsparameter infit.- sample_weightsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightsparameter infit.
- Returns:
- selfobject
The updated object.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') PSplines#
Request metadata passed to the
scoremethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.
- Returns:
- selfobject
The updated object.