PSplines#
- class FDApy.preprocessing.PSplines(n_segments=10, degree=3, order_penalty=2, order_derivative=0)[source]#
P-Splines smoothing.
The class fits a P-splines model to the given data using a B-splines basis and an optional weights matrix.
- Parameters:
n_segments (int | ndarray[Any, dtype[int64]]) – The number of evenly spaced segments.
degree (int | ndarray[Any, dtype[int64]]) – The number of the degree of the basis.
order_penalty (int) – The number of the order of the difference penalty.
order_derivative (int) – The order of the derivative to compute.
- Attributes:
y_hat (npt.NDArray[np.float64]) – The fitted response variable values.
beta_hat (npt.NDArray[np.float64]) – The estimated coefficients for the basis functions.
diagnostics (dict) – A dictionary containing diagnostic information about the fit.
Notes
This code is adapted from [2]. See [1] for more details.
References
Methods
fit(y, x[, sample_weights, penalty])Fit a P-splines model to the given data.
predict([x])Predict the response variable values for the given predictor variable values.
- fit(y, x, sample_weights=None, penalty=None, **kwargs)[source]#
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:
y (ndarray[Any, dtype[float64]]) – An nD array of shape (n1, n2, …, nk) containing the response variable values.
x (List[ndarray[Any, dtype[float64]]] | ndarray[Any, dtype[float64]]) – A 1D or a list of 1D arrays of shape (n1,), (n2,), …, (nk,) containing the predictor variable values.
sample_weights (ndarray[Any, dtype[float64]] | None) – An N-dimensional array of shape (n1, n2, …, nk) containing the weights for each observation. If not provided, all observations are assumed to have equal weight.
penalty (tuple[float, ...] | None) – A tuple of penalty parameters for each dimension.
- Return type:
self
Examples
>>> x = np.array([1, 2, 3, 4, 5]) >>> y = np.array([1, 2, 3, 4, 5]) >>> ps = P_splines(n_segments=3, degree=2) >>> ps.fit(y, x) >>> ps.y_hat array([1., 2., 3., 4., 5.])
- predict(x=None, **kwargs)[source]#
Predict the response variable values for the given predictor 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 (ndarray[Any, dtype[float64]] | None) – A 1D or a list of one-dimensional arrays of shape (n1,), (n2,), …, (nk,) containing the predictor variable values. If not provided, the method returns the fitted values.
- Returns:
An nD array of shape (n1, n2, …, nk) containing the predicted response variable values.
- Return type:
npt.NDArray[np.float64]
Examples
>>> x = np.array([1, 2, 3, 4, 5]) >>> y = np.array([1, 2, 3, 4, 5]) >>> ps = P_splines(n_segments=3, degree=2) >>> ps.fit(y, x) >>> ps.predict(x) array([1., 2., 3., 4., 5.])