Binary classifier that manually sets the decision threshold.
This classifier allows to change the default decision threshold 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.
Here, the threshold is not optimized and is set to a constant value.
Read more in the User Guide.
Added in version 1.5.
The binary classifier, fitted or not, for which we want to optimize the decision threshold used during predict.
The decision threshold to use when converting posterior probability estimates (i.e. output of predict_proba) or decision scores (i.e. output of decision_function) into a class label. When "auto", the threshold is set to 0.5 if predict_proba is used as response_method, otherwise it is set to 0 (i.e. the default threshold for decision_function).
The label of the positive class. Used to process the output of the response_method method. When pos_label=None, if y_true is in {-1, 1} or {0, 1}, pos_label is set to 1, otherwise an error will be raised.
Methods by the classifier estimator corresponding to the decision function for which we want to find a threshold. It can be:
"auto", it will try to invoke "predict_proba" or "decision_function" in that order."predict_proba" or "decision_function". If the method is not implemented by the classifier, it will raise an error.The fitted classifier used when predicting.
classes_ndarray of shape (n_classes,)
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.
See also
sklearn.model_selection.TunedThresholdClassifierCVClassifier that post-tunes the decision threshold based on some metrics and using cross-validation.
sklearn.calibration.CalibratedClassifierCVEstimator that calibrates probabilities.
>>> from sklearn.datasets import make_classification >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.metrics import confusion_matrix >>> from sklearn.model_selection import FixedThresholdClassifier, train_test_split >>> X, y = make_classification( ... n_samples=1_000, weights=[0.9, 0.1], class_sep=0.8, random_state=42 ... ) >>> X_train, X_test, y_train, y_test = train_test_split( ... X, y, stratify=y, random_state=42 ... ) >>> classifier = LogisticRegression(random_state=0).fit(X_train, y_train) >>> print(confusion_matrix(y_test, classifier.predict(X_test))) [[217 7] [ 19 7]] >>> classifier_other_threshold = FixedThresholdClassifier( ... classifier, threshold=0.1, response_method="predict_proba" ... ).fit(X_train, y_train) >>> print(confusion_matrix(y_test, classifier_other_threshold.predict(X_test))) [[184 40] [ 6 20]]
Classes labels.
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.
The decision function computed the fitted estimator.
Fit the classifier.
Training data.
Target values.
Parameters to pass to the fit method of the underlying classifier.
Returns an instance of self.
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.
The logarithm class probabilities of the input samples.
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.
The class probabilities of the input samples.
Return the mean accuracy on the given test 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 X.
Sample weights.
Mean accuracy of self.predict(X) w.r.t. y.
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.
Estimator instance.
Request metadata passed to the score 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 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.
Note
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.
Metadata routing for sample_weight parameter in score.
The updated object.
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.model_selection.FixedThresholdClassifier.html