>>> from sklearn.datasets import load_iris
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.model_selection import RandomizedSearchCV
>>> from scipy.stats import uniform
>>> iris = load_iris()
>>> logistic = LogisticRegression(solver='saga', tol=1e-2, max_iter=200,
... random_state=0)
>>> distributions = dict(C=uniform(loc=0, scale=4),
... penalty=['l2', 'l1'])
>>> clf = RandomizedSearchCV(logistic, distributions, random_state=0)
>>> search = clf.fit(iris.data, iris.target)
>>> search.best_params_
{'C': 2..., 'penalty': 'l1'}
Methods
decision_function
(X)
Call decision_function on the estimator with the best found parameters.
fit
(X[, y, groups])
Run fit with all sets of parameters.
get_metadata_routing
()
Get metadata routing of this object.
get_params
([deep])
Get parameters for this estimator.
inverse_transform
(Xt)
Call inverse_transform on the estimator with the best found params.
predict
(X)
Call predict on the estimator with the best found parameters.
predict_log_proba
(X)
Call predict_log_proba on the estimator with the best found parameters.
predict_proba
(X)
Call predict_proba on the estimator with the best found parameters.
score
(X[, y])
Return the score on the given data, if the estimator has been refit.
score_samples
(X)
Call score_samples on the estimator with the best found parameters.
set_fit_request
(*[, groups])
Request metadata passed to the fit
method.
set_params
(**params)
Set the parameters of this estimator.
transform
(X)
Call transform on the estimator with the best found parameters.
decision_function(X)[source]
Call decision_function on the estimator with the best found parameters.
Only available if refit=True
and the underlying estimator supports
decision_function
.
Parameters:
Xindexable, length n_samplesMust fulfill the input assumptions of the
underlying estimator.
Returns:
y_scorendarray of shape (n_samples,) or (n_samples, n_classes) or (n_samples, n_classes * (n_classes-1) / 2)Result of the decision function for X
based on the estimator with
the best found parameters.
fit(X, y=None, *, groups=None, **fit_params)[source]
Run fit with all sets of parameters.
Parameters:
Xarray-like of shape (n_samples, n_features)Training vector, 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=NoneTarget relative to X for classification or regression;
None for unsupervised learning.
groupsarray-like of shape (n_samples,), default=NoneGroup labels for the samples used while splitting the dataset into
train/test set. Only used in conjunction with a “Group” cv
instance (e.g., GroupKFold
).
**fit_paramsdict of str -> objectParameters passed to the fit
method of the estimator.
If a fit parameter is an array-like whose length is equal to
num_samples
then it will be split across CV groups along with X
and y
. For example, the sample_weight parameter is split
because len(sample_weights) = len(X)
.
Returns:
selfobjectInstance of fitted estimator.
get_metadata_routing()[source]
Get metadata routing of this object.
Please check User Guide on how the routing
mechanism works.
Returns:
routingMetadataRequestA MetadataRequest
encapsulating
routing information.
Parameters:
deepbool, default=TrueIf True, will return the parameters for this estimator and
contained subobjects that are estimators.
Returns:
paramsdictParameter names mapped to their values.
inverse_transform(Xt)[source]
Call inverse_transform on the estimator with the best found params.
Only available if the underlying estimator implements
inverse_transform
and refit=True
.
Parameters:
Xtindexable, length n_samplesMust fulfill the input assumptions of the
underlying estimator.
Returns:
X{ndarray, sparse matrix} of shape (n_samples, n_features)Result of the inverse_transform
function for Xt
based on the
estimator with the best found parameters.
predict(X)[source]
Call predict on the estimator with the best found parameters.
Only available if refit=True
and the underlying estimator supports
predict
.
Parameters:
Xindexable, length n_samplesMust fulfill the input assumptions of the
underlying estimator.
Returns:
y_predndarray of shape (n_samples,)The predicted labels or values for X
based on the estimator with
the best found parameters.
predict_log_proba(X)[source]
Call predict_log_proba on the estimator with the best found parameters.
Only available if refit=True
and the underlying estimator supports
predict_log_proba
.
Parameters:
Xindexable, length n_samplesMust fulfill the input assumptions of the
underlying estimator.
Returns:
y_predndarray of shape (n_samples,) or (n_samples, n_classes)Predicted class log-probabilities for X
based on the estimator
with the best found parameters. The order of the classes
corresponds to that in the fitted attribute classes_.
predict_proba(X)[source]
Call predict_proba on the estimator with the best found parameters.
Only available if refit=True
and the underlying estimator supports
predict_proba
.
Parameters:
Xindexable, length n_samplesMust fulfill the input assumptions of the
underlying estimator.
Returns:
y_predndarray of shape (n_samples,) or (n_samples, n_classes)Predicted class probabilities for X
based on the estimator with
the best found parameters. The order of the classes corresponds
to that in the fitted attribute classes_.
score(X, y=None)[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=NoneTarget relative to X for classification or regression;
None for unsupervised learning.
Returns:
scorefloatThe score defined by scoring
if provided, and the
best_estimator_.score
method otherwise.
score_samples(X)[source]
Call score_samples on the estimator with the best found parameters.
Only available if refit=True
and the underlying estimator supports
score_samples
.
New in version 0.24.
Parameters:
XiterableData to predict on. Must fulfill input requirements
of the underlying estimator.
Returns:
y_scorendarray of shape (n_samples,)The best_estimator_.score_samples
method.
set_fit_request(*, groups: Union[bool, None, str] = '$UNCHANGED$') → RandomizedSearchCV[source]
Request metadata passed to the fit
method.
Note that this method is only relevant if
enable_metadata_routing=True
(see sklearn.set_config
).
Please see 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.
New in version 1.3.
This method is only relevant if this estimator is used as a
sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.
Parameters:
groupsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGEDMetadata routing for groups
parameter in fit
.
Returns:
selfobjectThe updated object.
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:
**paramsdictEstimator parameters.
Returns:
selfestimator instanceEstimator instance.
transform(X)[source]
Call transform on the estimator with the best found parameters.
Only available if the underlying estimator supports transform
and
refit=True
.
Parameters:
Xindexable, length n_samplesMust fulfill the input assumptions of the
underlying estimator.
Returns:
Xt{ndarray, sparse matrix} of shape (n_samples, n_features)X
transformed in the new space based on the estimator with
the best found parameters.
Examples using sklearn.model_selection.RandomizedSearchCV
Release Highlights for scikit-learn 0.24
Release Highlights for scikit-learn 0.24
Faces recognition example using eigenfaces and SVMs
Faces recognition example using eigenfaces and SVMs
Comparison of kernel ridge and Gaussian process regression
Comparison of kernel ridge and Gaussian process regression
Comparing randomized search and grid search for hyperparameter estimation
Comparing randomized search and grid search for hyperparameter estimation
Sample pipeline for text feature extraction and evaluation
Sample pipeline for text feature extraction and evaluation
Column Transformer with Mixed Types
Column Transformer with Mixed Types