Skip to content
RgramKernelSmoother

KernelSmoother

See component workflows for rationale and worked analysis, the user guide for concepts, and result schemas for diagnostic tables.

rgram.KernelSmoother

[source]
class rgram.KernelSmoother(
    bandwidth='silverman',
    bandwidth_value=None,
    bandwidth_adjust=1.0,
    kernel='epanechnikov',
    n_eval_samples=100,
    *,
    batch_size=128,
    unsupported='nan',
    regression='local_constant',
    singular='constant',
    algorithm='auto',
    extrapolation='allow',
)

One-dimensional local-constant or local-linear kernel regression.

Parameters

bandwidth {'silverman', 'scott', 'manual'}, default='silverman'

Rule of thumb or manual bandwidth selection. Rules are based on x, not regression error; use cross-validation to tune predictive accuracy.

bandwidth_value float or None, default=None

Positive bandwidth in feature units, required for 'manual'.

bandwidth_adjust float, default=1.0

Positive multiplier applied to the selected bandwidth.

kernel str or callable, default='epanechnikov'

One of epanechnikov, gaussian, uniform, triangular, cosine, logistic, biweight, tricube. A callable accepts a Polars expression and returns nonnegative weights.

n_eval_samples int, default=100

Number of points returned by predict_grid().

batch_size int, default=128

Maximum query rows processed together. Blocks are also capped at approximately one million query/training pairs (at least one query).

unsupported {'nan', 'raise'}, default='nan'

Policy when a query has no positive kernel weight. 'nan' preserves the row and warns; 'raise' fails explicitly. Extrapolation has a separate policy.

regression {'local_constant', 'local_linear'}, default='local_constant'

Local constant (Nadaraya-Watson) or weighted local linear fit. Local linear reduces boundary bias but can extrapolate and use negative coefficients.

singular {'constant', 'raise'}, default='constant'

Fall back with a warning to a local constant for singular local-linear fits, or raise. Diagnostics always identify fallback rows.

algorithm {'auto', 'brute', 'neighbors'}, default='auto'

Auto uses support windows only for already-sorted x and compact kernels; otherwise bounded dense blocks. Neighbors requires eligible input and raises otherwise. This estimator never sorts observations or an index.

extrapolation {'allow', 'nan', 'raise'}, default='allow'

Outside-range queries warn and use the stated policy. Query values are never clipped. Allow still requires positive kernel support.

Attributes

X_ ndarray of shape (n_samples,)

Independent snapshot of original training features in row order.

y_ ndarray of shape (n_samples,)

Independent snapshot of original training responses in row order.

sample_weight_ ndarray of shape (n_samples,)

Original supplied weights, or ones if absent. Independent snapshot.

sample_weight_provided_ bool

Whether observation weights were supplied during fit.

feature_names_in_ ndarray of shape (1,)

Training feature name, present only for named input.

data_summary_ dict

Data integrity, original dtype, fitted policy and computation summary.

training_sorted_ bool

Whether training features were already nondecreasing.

algorithm_ str

Computational path selected during fitting. Prediction resolves the current algorithm option again, so changing it can alter the path.

weight_scale_ float

Maximum training observation weight used for computational normalization.

bandwidth_ float

Effective fitted bandwidth, including bandwidth_adjust.

n_samples_in_ int

Number of training rows retained, including zero-weight rows.

n_features_in_ int

Always one. Both 1D inputs and (n_samples, 1) arrays are accepted.

Notes

Training inputs are copied. Null, non-finite and multivariate inputs are rejected, never silently dropped. Kernel inspection columns follow the original training row order. Compact-kernel prediction visits only supported neighbors when training x is already sorted. No sorting is performed. Infinite-support and custom kernels use O(n_train * n_query) time with bounded blocks. get_weights() explicitly materializes a dense matrix and should be used on small query selections.

See Also

Examples

>>> from rgram import KernelSmoother
>>> model = KernelSmoother(kernel='gaussian', bandwidth='manual', bandwidth_value=1.)
>>> _ = model.fit([0., 1., 2.], [3., 3., 3.])
>>> model.predict([0.5, 1.5]).round(2)
array([3., 3.])

Methods

Method Purpose
fit Fit one numeric feature and one numeric response.
fit_predict Fit and evaluate at the original training feature values.
get_metadata_routing Get metadata routing of this object.
get_params Get parameters for this estimator.
get_weights Inspect the dense matrix of local influences in training-row order.
predict Evaluate the local-constant or local-linear response curve.
predict_diagnostics Return local support and numerical information for each query.
predict_grid Evaluate diagnostics on an evenly spaced training-range grid.
predict_interval Compute pointwise paired-bootstrap confidence intervals for the curve.
regression_diagnostics Inspect observed values, fitted values, and residuals in input order.
score Return :ref:coefficient of determination <r2_score> on test data.
set_fit_request Configure whether metadata should be requested to be passed to the fit method.
set_params Set the parameters of this estimator.
set_score_request Configure whether metadata should be requested to be passed to the score method.

fit

[source]
rgram.KernelSmoother.fit(
    X,
    y,
    *,
    data=None,
    sample_weight=None,
)

Fit one numeric feature and one numeric response.

Parameters

X array-like of shape (n_samples,) or (n_samples, 1), or str

Numeric feature, or its column name when data is supplied.

y array-like of shape (n_samples,) or (n_samples, 1), or str

Numeric response, or its column name when data is supplied.

data pandas.DataFrame, polars.DataFrame or polars.LazyFrame, default=None

Source for named columns. Pandas columns are converted internally to Polars. Selected columns are snapshotted once; indices are ignored.

sample_weight array-like of shape (n_samples,), default=None

Finite nonnegative influence weights with positive total mass.

Returns

self estimator

Fitted estimator, with independent training snapshots.

Raises

ValueError

For incompatible shapes, unequal lengths, missing/non-finite values, invalid settings, or unsupported weighting conventions.

TypeError

For nonnumeric/complex input or invalid callback/data types.

Notes

Rows and duplicates are preserved. Unsorted input emits an advisory warning. A failed refit invalidates prediction access. Zero-weight rows still participate in feature-based bin or bandwidth selection. Normal fitting does not run CV; Regressogram(n_bins='cv') explicitly requests it.

Examples

>>> import numpy as np
>>> from rgram import KernelSmoother
>>> X = np.arange(10.0).reshape(-1, 1)
>>> model = KernelSmoother().fit(X, X[:, 0])
>>> model.predict(X).shape
(10,)

fit_predict

[source]
rgram.KernelSmoother.fit_predict(
    X,
    y,
    *,
    data=None,
    sample_weight=None,
)

Fit and evaluate at the original training feature values.

Parameters

X array-like or str

One training feature, or its column name when data is supplied.

y array-like or str

Aligned numeric response, or its column name when data is supplied.

data pandas.DataFrame, polars.DataFrame or polars.LazyFrame, default=None

Source for named columns.

sample_weight array-like of shape (n_samples,), default=None

Observation influence weights; see fit.

Returns

prediction ndarray of shape (n_samples,)

Predictions at original training rows, preserving their order.

Notes

Training predictions describe the fit, not held-out performance. Fit followed by predict evaluates new queries; use predict_interval for bootstrap confidence bounds.

get_metadata_routing

[inherited API]
rgram.KernelSmoother.get_metadata_routing()

Get metadata routing of this object.

Please check metadata routing guide on how the routing mechanism works.

Returns

routing MetadataRequest

A sklearn.utils.metadata_routing.MetadataRequest encapsulating routing information.

get_params

[inherited API]
rgram.KernelSmoother.get_params(deep=True)

Get parameters for this estimator.

Parameters

deep bool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params dict

Parameter names mapped to their values.

get_weights

[source]
rgram.KernelSmoother.get_weights(
    X,
    *,
    kind='prediction',
)

Inspect the dense matrix of local influences in training-row order.

Parameters

X array-like of shape (n_queries,) or (n_queries, 1)

Query feature values.

kind {'prediction', 'kernel'}, default='prediction'

Prediction coefficients or normalized nonnegative kernel/observation weights. Local-linear prediction coefficients can be negative.

Returns

weights ndarray of shape (n_queries, n_samples_in_)

Columns follow original training order. Unsupported rows contain zeros. Supported prediction rows combine y_ into predicted values. For local constants, prediction and kernel weights coincide.

Notes

This intentionally allocates a dense matrix. Use small query selections; ordinary prediction does not require storing this full matrix.

predict

[source]
rgram.KernelSmoother.predict(X)

Evaluate the local-constant or local-linear response curve.

Parameters

X array-like of shape (n_queries,) or (n_queries, 1)

Query feature values. Named inputs must match the fitted feature.

Returns

prediction ndarray of shape (n_queries,)

One value per query in input order. Unsupported estimates are NaN unless the configured support policy raises.

Raises

sklearn.exceptions.NotFittedError

If no successful fit is available.

ValueError

For invalid queries, feature-name mismatch, or strict policies.

See Also

predict_diagnostics

[source]
rgram.KernelSmoother.predict_diagnostics(X)

Return local support and numerical information for each query.

Parameters

X array-like of shape (n_queries,) or (n_queries, 1)

One query feature, with fitted feature name if named.

Returns

diagnostics polars.DataFrame

Columns: x, in_training_range, prediction, supported, n_neighbors, effective_n, local_linear_fallback. One row per query in original order. Unsupported rows have NaN prediction, zero neighbors and effective_n, and false supported, unless a strict policy raises.

Notes

effective_n = 1 / sum(normalized kernel/observation weights ** 2). This measures weight concentration, not confidence or signed-coefficient variance.

predict_grid

[source]
rgram.KernelSmoother.predict_grid()

Evaluate diagnostics on an evenly spaced training-range grid.

Returns

diagnostics polars.DataFrame

The predict_diagnostics schema at n_eval_samples locations spanning the observed feature minimum and maximum, endpoints included.

Notes

This changes evaluation locations, not smoothing. With a constant feature, all grid coordinates are equal. Fit must have succeeded first.

predict_interval

[source]
rgram.KernelSmoother.predict_interval(
    X,
    *,
    confidence_level=0.95,
    n_resamples=200,
    random_state=None,
    method='percentile',
    resampling='iid',
    groups=None,
    block_size=None,
    min_valid_fraction=1.0,
)

Compute pointwise paired-bootstrap confidence intervals for the curve.

Parameters

X array-like of shape (n_queries,) or (n_queries, 1)

Query locations; order and duplicates are preserved.

confidence_level float, default=0.95

Nominal interval level strictly between zero and one.

n_resamples int, default=200

Bootstrap refits, at least two. More draws improve Monte Carlo stability.

random_state int or None, default=None

Seed for resampling, independent of any estimator CV seed.

method {'percentile', 'basic'}, default='percentile'

Bootstrap quantiles, or quantiles reflected around the original estimate.

resampling {'iid', 'groups', 'blocks'}, default='iid'

Resample paired independent rows, whole independent groups, or contiguous moving blocks in original training order. No sorting occurs.

groups array-like of shape (n_samples_in_,) or None, default=None

Nonmissing group labels, required only for group resampling. At least two distinct groups are required.

block_size int or None, default=None

Block length from 2 to n_samples_in_ - 1, required only for blocks.

min_valid_fraction float, default=1.0

Fraction of draws that must support each query, in (0, 1]. At least two valid draws are always required. Lower fractions condition on valid draws.

Returns

interval polars.DataFrame

Columns: x, prediction, lower, upper, n_valid, bootstrap_coverage. Insufficiently supported bounds are NaN. Queries are never dropped.

Raises

sklearn.exceptions.NotFittedError

If no successful fit is available.

ValueError

For invalid options, fewer than two training rows, invalid group/block inputs, or errors from the estimator's strict fit/prediction policies.

Notes

These are curve intervals, not prediction intervals or simultaneous bands. IID draws assume independent random-design rows. Group and block draws require appropriate independent groups or ordered dependence. Smoothing settings are held fixed; bin boundaries are relearned. Selection uncertainty and smoothing bias are not corrected. Memory includes n_resamples * n_queries draw values. A draw with no positive observation weight counts as unsupported. Other fit errors propagate. Explicit neighbor-only algorithms can reject unsorted draws.

regression_diagnostics

[source]
rgram.KernelSmoother.regression_diagnostics(X, y)

Inspect observed values, fitted values, and residuals in input order.

Parameters

X array-like of shape (n_samples,) or (n_samples, 1)

One evaluation feature. Named inputs must match the fitted feature.

y array-like of shape (n_samples,) or (n_samples, 1)

Observed response, aligned with x. May be training or held-out data.

Returns

diagnostics polars.DataFrame

Columns: x, observed, prediction, residual (observed minus prediction), supported (finite prediction). Unsupported rows remain present.

Raises

sklearn.exceptions.NotFittedError

If no successful fit is available.

ValueError

For invalid, misaligned inputs or strict prediction policies.

score

[inherited API]
rgram.KernelSmoother.score(
    X,
    y,
    sample_weight=None,
)

Return coefficient of determination on test data.

The coefficient of determination, \(R^2\), is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)** 2).sum() and \(v\) is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.

Parameters

X array-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

y array-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.

sample_weight array-like of shape (n_samples,), default=None

Sample weights.

Returns

score float

\(R^2\) of self.predict(X) w.r.t. y.

Notes

The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of sklearn.metrics.r2_score. This influences the score method of all the multioutput regressors (except for sklearn.multioutput.MultiOutputRegressor).

set_fit_request

[inherited API]
rgram.KernelSmoother.set_fit_request(
    *,
    data='$UNCHANGED$',
    sample_weight='$UNCHANGED$',
)

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 :term:meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config). Please check the metadata routing 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

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

Metadata routing for data parameter in fit.

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

Metadata routing for sample_weight parameter in fit.

Returns

self object

The updated object.

set_params

[inherited API]
rgram.KernelSmoother.set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as sklearn.pipeline.Pipeline). The latter have parameters of the form <component>__<parameter> so that it's possible to update each component of a nested object.

Parameters

**params dict

Estimator parameters.

Returns

self estimator instance

Estimator instance.

set_score_request

[inherited API]
rgram.KernelSmoother.set_score_request(*, sample_weight='$UNCHANGED$')

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 :term:meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config). Please check the metadata routing 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_weight str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns

self object

The updated object.

Implementation source: smoothing.py.