Modelling#

Ridge regression with built-in cross-validation for spectral modelling.

class spectoprep.modelling.ridge.OptimizedRidgeCV(alphas=None, cv=5, scoring='neg_mean_squared_error', fit_intercept=True, gcv_mode=None, store_cv_results=False, groups=None)[source]#

Bases: BaseEstimator, RegressorMixin

Ridge regression with alpha selected by cross-validation.

Thin, scikit-learn-compatible wrapper around sklearn.linear_model.RidgeCV that adds optional group-aware cross-validation.

Parameters:
  • alphas (array-like, default=None) – Alpha values to try. When None, np.logspace(-3, 3, 10) is used. Resolved in fit() so the estimator honours the scikit-learn contract that __init__ only stores its arguments unchanged.

  • cv (int, cross-validation generator, iterable or None, default=5) – Cross-validation splitting strategy. When None, RidgeCV uses its efficient leave-one-out generalized cross-validation (GCV), and gcv_mode/store_cv_results become available.

  • scoring (str or callable, default='neg_mean_squared_error') – Scoring used to select alpha.

  • fit_intercept (bool, default=True) – Whether to fit an intercept.

  • gcv_mode ({None, 'auto', 'svd', 'eigen'}, default=None) – GCV strategy. Only used when cv is None.

  • store_cv_results (bool, default=False) – Store per-alpha CV results in cv_results_. Only valid when cv is None (RidgeCV restriction).

  • groups (array-like, default=None) – Group labels. When provided (and cv is not None), grouped K-fold splits are materialised so no group is split across folds.

Notes

normalize was removed from scikit-learn’s RidgeCV in 1.2 and store_cv_values renamed to store_cv_results in 1.5; this wrapper tracks the current API. Standardise inputs with a scaler in a Pipeline instead of relying on normalize.

fit(X, y, sample_weight=None)[source]#

Fit the Ridge model, selecting alpha by cross-validation.

predict(X)[source]#

Predict target values for X.

score(X, y, sample_weight=None)[source]#

Return the \(R^2\) score of the prediction.

get_cv_results()[source]#

Return a summary of the cross-validation results.

set_fit_request(*, 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 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:
  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

  • self (OptimizedRidgeCV)

Returns:

self – The updated object.

Return type:

object

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 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_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

  • self (OptimizedRidgeCV)

Returns:

self – The updated object.

Return type:

object