pflm.smooth.Polyfit2DModel#

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

Bases: BaseEstimator, RegressorMixin

Local polynomial (2D) regression with kernel smoothing and interpolation.

Parameters:
kernel_typeKernelType, default=KernelType.GAUSSIAN

Kernel used for smoothing.

degreeint, default=1

Polynomial degree (>= 1).

deriv1int, default=0

Derivative order along the first dimension (>= 0).

deriv2int, default=0

Derivative order along the second dimension (>= 0).

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, 2)

Training inputs.

y_np.ndarray of shape (n_samples,)

Training targets.

sample_weight_np.ndarray of shape (n_samples,)

Sample weights.

reg_grid1_np.ndarray of shape (m1,)

Interpolation grid along the first dimension.

reg_grid2_np.ndarray of shape (m2,)

Interpolation grid along the second dimension.

reg_fitted_values_np.ndarray of shape (m1, m2)

Fitted values evaluated on the interpolation grid mesh.

obs_grid1_np.ndarray of shape (n_obs_grid1,)

Unique observed grid from X[:, 0].

obs_grid2_np.ndarray of shape (n_obs_grid2,)

Unique observed grid from X[:, 1].

bandwidth1_float

Selected/used bandwidth for the first dimension.

bandwidth2_float

Selected/used bandwidth for the second dimension.

bandwidth_selection_results_dict

Selection details including candidates, method, and chosen pair.

See also

Polyfit1DModel

Local polynomial regression in 1D.

Examples

>>> import numpy as np
>>> from pflm.smooth import Polyfit2DModel
>>> rng = np.random.default_rng(42)
>>> n = 200
>>> X = rng.uniform(0, 1, (n, 2))
>>> y = np.sin(X[:, 0] * np.pi) * np.cos(X[:, 1] * np.pi) + rng.normal(0, 0.1, n)
>>> model = Polyfit2DModel().fit(X, y)
>>> model.bandwidth1_ > 0 and model.bandwidth2_ > 0
True
>>> y_pred = model.predict(np.array([0.5, 0.8]), np.array([0.5, 0.8]))
>>> y_pred.shape
(2,)
fit(X: ndarray | list[list[float]], y: ndarray | list[float], sample_weight: ndarray | list[float] | None = None, bandwidth1: float | None = None, bandwidth2: float | None = None, reg_grid1: ndarray | list[float] | None = None, reg_grid2: ndarray | list[float] | None = None, num_bw_candidates: int = 21, bandwidth_selection_method: Literal['cv', 'gcv'] = 'gcv', num_points_reg_grid: int = 100, same_bandwidth_for_2dim: bool = False, custom_bw_candidates: ndarray | None = None, cv_folds: int = 5)[source][source]#

Fit the 2D local polynomial model with kernel smoothing.

Parameters:
Xarray-like of shape (n_samples, 2)

Training inputs.

yarray-like of shape (n_samples,)

Training targets.

sample_weightarray-like of shape (n_samples,), optional

Sample weights.

bandwidth1float, optional

Bandwidth for the first axis.

bandwidth2float, optional

Bandwidth for the second axis.

reg_grid1array-like of shape (m1,), optional

Interpolation grid on axis-1. If None, a uniform grid is created.

reg_grid2array-like of shape (m2,), optional

Interpolation grid on axis-2. 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 each axis of the internal interpolation grid (used if reg_grid1/reg_grid2 are None).

same_bandwidth_for_2dimbool, default=False
custom_bw_candidatesnp.ndarray, optional
cv_foldsint, default=5
Returns:
Polyfit2DModel

Fitted estimator (self).

Raises:
ValueError

If inputs are invalid or selection arguments are inconsistent.

fitted_values() ndarray[source][source]#

Return fitted values on the model’s 2D interpolation grid.

Returns:
np.ndarray of shape (len(reg_grid1_) * len(reg_grid2_),)

Fitted values evaluated on the 2D interpolation mesh.

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

Return interpolation grids and fitted values.

Returns:
reg_grid1np.ndarray of shape (m1,)

Copy of reg_grid1_.

reg_grid2np.ndarray of shape (m2,)

Copy of reg_grid2_.

reg_fitted_valuesnp.ndarray of shape (m1, m2)

Copy of reg_fitted_values_.

predict(X1: ndarray, X2: ndarray, use_model_interp: bool = True) ndarray[source][source]#

Predict responses on a 2D grid.

Parameters:
X1np.ndarray of shape (n_x1,)

Query points along axis-1.

X2np.ndarray of shape (n_x2,)

Query points along axis-2.

use_model_interpbool, default=True

If True, interpolate from the fitted grid; if False, evaluate directly.

Returns:
np.ndarray of shape (n_x2, n_x1)

Predicted values (note the row-major order).

Raises:
sklearn.exceptions.NotFittedError

If the model is not fitted.

set_fit_request(*, bandwidth1: bool | None | str = '$UNCHANGED$', bandwidth2: 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_grid1: bool | None | str = '$UNCHANGED$', reg_grid2: bool | None | str = '$UNCHANGED$', same_bandwidth_for_2dim: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') Polyfit2DModel#

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:
bandwidth1str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for bandwidth1 parameter in fit.

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

Metadata routing for bandwidth2 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_grid1str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for reg_grid1 parameter in fit.

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

Metadata routing for reg_grid2 parameter in fit.

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

Metadata routing for same_bandwidth_for_2dim 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(*, X1: bool | None | str = '$UNCHANGED$', X2: bool | None | str = '$UNCHANGED$', use_model_interp: bool | None | str = '$UNCHANGED$') Polyfit2DModel#

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:
X1str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for X1 parameter in predict.

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

Metadata routing for X2 parameter in predict.

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$') Polyfit2DModel#

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.