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,RegressorMixinLocal 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
Polyfit1DModelLocal 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.
- np.ndarray of shape (len(
- 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
fitmethod.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(seesklearn.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 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.
- Parameters:
- bandwidth1str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
bandwidth1parameter infit.- bandwidth2str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
bandwidth2parameter infit.- bandwidth_selection_methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
bandwidth_selection_methodparameter infit.- custom_bw_candidatesstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
custom_bw_candidatesparameter infit.- cv_foldsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
cv_foldsparameter infit.- num_bw_candidatesstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
num_bw_candidatesparameter infit.- num_points_reg_gridstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
num_points_reg_gridparameter infit.- reg_grid1str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
reg_grid1parameter infit.- reg_grid2str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
reg_grid2parameter infit.- same_bandwidth_for_2dimstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
same_bandwidth_for_2dimparameter infit.- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter infit.
- 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
predictmethod.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(seesklearn.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 topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.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
X1parameter inpredict.- X2str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
X2parameter inpredict.- use_model_interpstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
use_model_interpparameter inpredict.
- 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
scoremethod.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(seesklearn.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 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.
- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.
- Returns:
- selfobject
The updated object.