OptCV#

class OptCV(estimator, optimizer, *, scoring: Callable | str | None = None, refit: bool = True, cv=None)[source]#

Tuning an sklearn estimator via any optimizer in the hyperactive toolbox.

OptCV uses any available tuning engine from hyperactive to tune an sklearn estimator via cross-validation.

It passes cross-validation results as scores to the tuning engine, which identifies the best hyperparameters.

Any available tuning engine from hyperactive can be used, for example:

  • grid search - from hyperactive.opt import GridSearchSk as GridSearch, this results in the same algorithm as GridSearchCV

  • hill climbing - from hyperactive.opt import HillClimbing

  • optuna parzen-tree search - from hyperactive.opt.optuna import TPEOptimizer

Configuration of the tuning engine is as per the respective documentation.

Formally, OptCV does the following:

In fit:

  • wraps the estimator, scoring, and other parameters into a SklearnCvExperiment instance, which is passed to the optimizer optimizer as the experiment argument.

  • Optimal parameters are then obtained from optimizer.solve, and set as best_params_ and best_estimator_ attributes.

  • If refit=True, best_estimator_ is fitted to the entire X and y.

In predict and predict-like methods, calls the respective method of the best_estimator_ if refit=True.

Parameters:
estimatorsklearn BaseEstimator

The estimator to be tuned.

optimizerhyperactive BaseOptimizer

The optimizer to be used for hyperparameter search.

scoringcallable or str, default = accuracy_score or mean_squared_error

sklearn scoring function or metric to evaluate the model’s performance. Default is determined by the type of estimator: accuracy_score for classifiers, and mean_squared_error for regressors, as per sklearn convention through the default score method of the estimator.

refit: bool, optional, default = True

Whether to refit the best estimator with the entire dataset. If True, the best estimator is refit with the entire dataset after the optimization process. If False, does not refit, and predict is not available.

cvint or cross-validation generator, default = KFold(n_splits=3, shuffle=True)

The number of folds or cross-validation strategy to be used. If int, the cross-validation used is KFold(n_splits=cv, shuffle=True).

Attributes:
classes_

Classes function.

fit_successful

Fit Successful function.

Methods

decision_function(X)

Decision Function function.

fit(X, y, **fit_params)

Fit the model.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

inverse_transform([X, Xt])

Inverse Transform function.

predict(X)

Predict function.

predict_log_proba(X)

Predict Log Proba function.

predict_proba(X)

Predict Proba function.

score(X[, y])

Return the score on the given data, if the estimator has been refit.

score_samples(X)

Score Samples function.

set_params(**params)

Set the parameters of this estimator.

transform(X)

Transform function.

verify_fit()

Mark fit successful and preserve signature.

fit(X, y, **fit_params)[source]#

Fit the model.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

Training data.

yarray-like of shape (n_samples,) or (n_samples, n_targets)

Target values. Will be cast to X’s dtype if necessary.

Returns:
selfobject

Fitted Estimator.

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

Return the score on the given data, if the estimator has been refit.

This uses the score defined by scoring where provided, and the best_estimator_.score method otherwise.

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

Input data, where n_samples is the number of samples and n_features is the number of features.

yarray-like of shape (n_samples, n_output) or (n_samples,), default=None

Target relative to X for classification or regression; None for unsupervised learning.

**paramsdict

Parameters to be passed to the underlying scorer(s).

Returns:
scorefloat

The score defined by scoring if provided, and the best_estimator_.score method otherwise.

property fit_successful[source]#

Fit Successful function.

property classes_[source]#

Classes function.

decision_function(X)[source]#

Decision Function function.

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

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

Returns:
paramsdict

Parameter names mapped to their values.

inverse_transform(X=None, Xt=None)[source]#

Inverse Transform function.

predict(X)[source]#

Predict function.

predict_log_proba(X)[source]#

Predict Log Proba function.

predict_proba(X)[source]#

Predict Proba function.

score_samples(X)[source]#

Score Samples function.

set_params(**params)[source]#

Set the parameters of this estimator.

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

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

transform(X)[source]#

Transform function.

verify_fit()[source]#

Mark fit successful and preserve signature.