pflm.smooth.Polyfit1DModel#

class Polyfit1DModel(kernel_type: KernelType = KernelType.GAUSSIAN, degree: int = 1, deriv: int = 0, interp_kind: Literal['linear', 'spline'] = 'linear', random_seed: int | None = None)[source][source]#

Bases: BaseEstimator, RegressorMixin

1D polynomial fitting with kernel smoothing and fast interpolation.

This estimator performs local polynomial regression using a selectable kernel. It can choose bandwidth by CV/GCV and predicts efficiently via interpolation.

Parameters:
kernel_typeKernelType, default=KernelType.GAUSSIAN

Kernel used for smoothing.

degreeint, default=1

Polynomial degree (>= 1).

derivint, default=0

Derivative order (0 <= deriv <= degree).

interp_kind{“linear”, “spline”}, default=”linear”

Interpolation method used for fast prediction.

random_seedint, optional

Random seed for reproducibility (e.g., CV shuffles).

Attributes:
X_np.ndarray of shape (n_samples,)

Training inputs (sorted copy stored as sorted_X_).

y_np.ndarray of shape (n_samples,)

Training targets (sorted copy stored as sorted_y_).

sample_weight_np.ndarray of shape (n_samples,)

Sample weights (sorted copy stored as sorted_sample_weight_).

n_features_in_int

Number of features during fit (always 1 for 1D).

reg_grid_np.ndarray of shape (m,)

Interpolation grid used for fast predictions.

reg_fitted_values_np.ndarray of shape (m,)

Fitted values evaluated on reg_grid_.

obs_grid_np.ndarray of shape (n_obs_grid,)

Unique observed grid from X_.

bandwidth_float

Selected/used bandwidth.

bandwidth_selection_results_dict

Selection details: candidates, method, scores, and chosen bandwidth.

See also

Polyfit2DModel

Local polynomial regression in 2D.

Examples

>>> import numpy as np
>>> from pflm.smooth import Polyfit1DModel
>>> rng = np.random.default_rng(42)
>>> X = np.linspace(0, 2 * np.pi, 100)
>>> y = np.sin(X) + rng.normal(0, 0.1, 100)
>>> model = Polyfit1DModel().fit(X, y)
>>> model.bandwidth_ > 0
True
>>> y_pred = model.predict(np.array([1.0, 2.0, 3.0]))
>>> y_pred.shape
(3,)
fit(X: ndarray | list[float], y: ndarray | list[float], sample_weight: ndarray | list[float] | None = None, bandwidth: float | None = None, reg_grid: ndarray | list[float] | None = None, num_bw_candidates: int = 21, bandwidth_selection_method: Literal['cv', 'gcv'] = 'gcv', num_points_reg_grid: int = 100, custom_bw_candidates: ndarray | None = None, cv_folds: int = 5) Polyfit1DModel[source][source]#

Fit the 1D local polynomial model with kernel smoothing.

Parameters:
Xarray-like of shape (n_samples,) or (n_samples, 1)

Training inputs.

yarray-like of shape (n_samples,)

Training targets.

sample_weightarray-like of shape (n_samples,), optional

Sample weights.

bandwidthfloat, optional

If not provided, selected by bandwidth_selection_method.

reg_gridarray-like of shape (m,), optional

Interpolation grid. If None, a uniform grid is created.

num_bw_candidatesint, default=21
bandwidth_selection_method{“cv”, “gcv”}, default=”gcv”
num_points_reg_gridint, default=100

Number of points for the internal interpolation grid (used if reg_grid is None).

custom_bw_candidatesnp.ndarray, optional
cv_foldsint, default=5
Returns:
Polyfit1DModel

Fitted estimator (self).

Raises:
ValueError

If inputs are invalid or selection arguments are inconsistent.

fitted_values() ndarray[source][source]#

Return fitted values on the interpolation grid.

Returns:
np.ndarray of shape (len(reg_grid_),)

Fitted values evaluated on reg_grid_.

get_fitted_grids() tuple[ndarray, ndarray][source][source]#

Return interpolation grid and fitted values.

Returns:
reg_gridnp.ndarray of shape (m,)

Copy of reg_grid_.

reg_fitted_valuesnp.ndarray of shape (m,)

Copy of reg_fitted_values_.

Raises:
sklearn.exceptions.NotFittedError

If the model is not fitted.

predict(X: ndarray | list[float], use_model_interp: bool = True) ndarray[source][source]#

Predict responses at new inputs.

Parameters:
Xarray-like of shape (m,) or (m, 1)

Query points.

use_model_interpbool, default=True

If True, interpolate from reg_grid_; if False, evaluate directly.

Returns:
np.ndarray of shape (m,)

Predicted values.

Raises:
sklearn.exceptions.NotFittedError

If the model is not fitted.

set_fit_request(*, bandwidth: bool | None | str = '$UNCHANGED$', bandwidth_selection_method: bool | None | str = '$UNCHANGED$', custom_bw_candidates: bool | None | str = '$UNCHANGED$', cv_folds: bool | None | str = '$UNCHANGED$', num_bw_candidates: bool | None | str = '$UNCHANGED$', num_points_reg_grid: bool | None | str = '$UNCHANGED$', reg_grid: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') Polyfit1DModel#

Configure whether metadata should be requested to be passed to the fit method.

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.

Added in version 1.3.

Parameters:
bandwidthstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for bandwidth parameter in fit.

bandwidth_selection_methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for bandwidth_selection_method parameter in fit.

custom_bw_candidatesstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for custom_bw_candidates parameter in fit.

cv_foldsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for cv_folds parameter in fit.

num_bw_candidatesstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for num_bw_candidates parameter in fit.

num_points_reg_gridstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for num_points_reg_grid parameter in fit.

reg_gridstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for reg_grid parameter in fit.

sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns:
selfobject

The updated object.

set_predict_request(*, use_model_interp: bool | None | str = '$UNCHANGED$') Polyfit1DModel#

Configure whether metadata should be requested to be passed to the predict method.

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 predict 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 predict.

  • 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.

Parameters:
use_model_interpstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for use_model_interp parameter in predict.

Returns:
selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') Polyfit1DModel#

Configure whether metadata should be requested to be passed to the score method.

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 score 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 score.

  • 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.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.