TunedThresholdClassifierCV
- class sklearn.model_selection.TunedThresholdClassifierCV(estimator, *, scoring='balanced_accuracy', response_method='auto', thresholds=100, cv=None, refit=True, , ,
Classifier that post-tunes the decision threshold using cross-validation.
This estimator post-tunes the decision threshold (cut-off point) that is
used for converting posterior probability estimates (i.e. output of
predict_proba) or decision scores (i.e. output of decision_function)
into a class label. The tuning is done by optimizing a binary metric,
potentially constrained by another metric.
Read more in the User Guide.
The classifier, fitted or not, for which we want to optimize
the decision threshold used during predict.
The objective metric to be optimized. Can be one of:
str: string associated to a scoring function for binary classification, see String name scorers for options.
callable: a scorer callable object (e.g., function) with signature
scorer(estimator, X, y). See Callable scorers for details.
Methods by the classifier estimator corresponding to the
decision function for which we want to find a threshold. It can be:
if "auto", it will try to invoke, for each classifier,
"predict_proba" or "decision_function" in that order.
otherwise, one of "predict_proba" or "decision_function".
If the method is not implemented by the classifier, it will raise an
error.
The number of decision threshold to use when discretizing the output of the
classifier method. Pass an array-like to manually specify the thresholds
to use.
Determines the cross-validation splitting strategy to train classifier. Possible inputs for cv are:
None, to use the default 5-fold stratified K-fold cross validation;
An integer number, to specify the number of folds in a stratified k-fold;
A float number, to specify a single shuffle split. The floating number should be in (0, 1) and represent the size of the validation set;
An object to be used as a cross-validation generator;
An iterable yielding train, test splits;
"prefit", to bypass the cross-validation.
Refer User Guide for the various cross-validation strategies that can be used here.
Whether or not to refit the classifier on the entire training set once
the decision threshold has been found.
Note that forcing refit=False on cross-validation having more
than a single split will raise an error. Similarly, refit=True in
conjunction with cv="prefit" will raise an error.
The number of jobs to run in parallel. When cv represents a
cross-validation strategy, the fitting and scoring on each data split
is done in parallel. None means 1 unless in a
joblib.parallel_backend context. -1 means using all
processors. See Glossary for more details.
Controls the randomness of cross-validation when cv is a float.
See Glossary.
Whether to store all scores and thresholds computed during the cross-validation process.
The fitted classifier used when predicting.
The new decision threshold.
The optimal score of the objective metric, evaluated at best_threshold_.
A dictionary containing the scores and thresholds computed during the
cross-validation process. Only exist if store_cv_results=True. The
keys are "thresholds" and "scores".
>>> from sklearn.datasets import make_classification
>>> from sklearn.ensemble import RandomForestClassifier
>>> from sklearn.metrics import classification_report
>>> from sklearn.model_selection import TunedThresholdClassifierCV, train_test_split
>>> X, y = make_classification(
Decision function for samples in X using the fitted estimator.
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Fit the classifier.
Training data.
Target values.
Parameters to pass to the method of the underlying
classifier.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRouter encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Predict the target of new samples.
The samples, as accepted by estimator.predict.
The predicted class.
Predict logarithm class probabilities for X using the fitted estimator.
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Predict class probabilities for X using the fitted estimator.
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Return accuracy on provided data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Test samples.
True labels for .
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.
Estimator parameters.
Configure whether metadata should be requested to be passed to the method.
Post-tuning the decision threshold for cost-sensitive learning
Post-hoc tuning the cut-off point of decision function
estimatorThis option should only be used when the set used to fit estimator is
different from the one used to tune the cut-off point (by calling
TunedThresholdClassifierCV.fit).
Classes labels.
Number of features seen during fit. Only defined if the underlying estimator exposes such an attribute when fit.
n_features_in_,)Names of features seen during fit. Only defined if the underlying estimator exposes such an attribute when fit.
The decision function computed the fitted estimator.
Returns an instance of self.
The logarithm class probabilities of the input samples.
The class probabilities of the input samples.
XSample weights.
Mean accuracy of self.predict(X) w.r.t. y.
Estimator instance.
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.
Metadata routing for sample_weight parameter in score.
The updated object.