Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH Add multi-threshold classification to FixedThresholdClassifier #31544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pedroL0pes
Copy link
Contributor

This commit adds support for multi-threshold classification to FixedThresholdClassifier, enabling discretization of continuous decision scores into multiple classes.
The classifier now accepts a list of thresholds and optional labels for each bin.
Includes:

  • Unit tests covering the new functionality
  • Documentation and usage example

Reference Issues/PRs

#30452
Multiple thresholds in FixedThresholdClassifier

What does this implement/fix? Explain your changes.

This PR extends FixedThresholdClassifier to support discretization of continuous decision scores into multiple classes, by allowing the threshold parameter to be a list of values. This enables users to bin decision function outputs (such as probabilities or scores) into more than two risk classes or categories, a common requirement in real-world applications such as credit scoring, risk modeling, and regulatory compliance.

Key additions:

  • Multi-threshold support: The classifier now accepts a list of threshold values to bin decision scores.
  • Custom labels: Users can optionally provide a list of labels for the output classes corresponding to each bin.
  • Backward compatibility: The original binary thresholding behavior is preserved when a single float is passed.
  • Improved _threshold_scores_to_class_labels function to convert scores to class labels efficiently.
  • Validation: Checks for consistency between number of thresholds and number of labels.
  • Error handling: Raises informative errors for invalid parameter combinations.

Copy link

github-actions bot commented Jun 13, 2025

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: a29bef0. Link to the linter CI: here

@adrinjalali
Copy link
Member

Tests failing. Once CI is green feel free to ping me back.

@pedroL0pes pedroL0pes force-pushed the multi_thresh_feature branch 2 times, most recently from 72c5623 to 05bc405 Compare June 18, 2025 18:01
@nunofbiomartins
Copy link

Hi @adrinjalali .
We only have one CI test failing - we tried everything but can't understand what's wrong/missing.
Could you help us figuring this out, please?
Thank you.

"sklearn/model_selection/_classification_threshold.py:docstring of sklearn.model_selection._classification_threshold.FixedThresholdClassifier:105: WARNING: Block quote ends without a blank line; unexpected unindent. [docutils]"

@adrinjalali
Copy link
Member

This should fix the issue:

diff --git a/sklearn/model_selection/_classification_threshold.py b/sklearn/model_selection/_classification_threshold.py
index ab8ab266f4..37c1380cd8 100644
--- a/sklearn/model_selection/_classification_threshold.py
+++ b/sklearn/model_selection/_classification_threshold.py
@@ -293,7 +293,7 @@ class FixedThresholdClassifier(BaseThresholdClassifier):
     >>> from sklearn.metrics import confusion_matrix
     >>> from sklearn.model_selection import FixedThresholdClassifier, train_test_split
 
-    # Binary classification with custom threshold
+    >>> # Binary classification with custom threshold
     >>> X, y = make_classification(
     ...     n_samples=1_000, weights=[0.9, 0.1], class_sep=0.8, random_state=42
     ... )
@@ -311,13 +311,13 @@ class FixedThresholdClassifier(BaseThresholdClassifier):
     [[184  40]
      [  6  20]]
 
-    # Multi-threshold classification using discretized scores
+    >>> # Multi-threshold classification using discretized scores
+    >>> import numpy as np
     >>> base_clf = LogisticRegression(random_state=0)
     >>> clf_multi = FixedThresholdClassifier(
     ...     base_clf, threshold=[0.3, 0.6], labels=["Low", "Medium", "High"]
     ... ).fit(X_train, y_train)
     >>> y_pred = clf_multi.predict(X_test)
-    >>> import numpy as np
     >>> labels, counts = np.unique(y_pred, return_counts=True)
     >>> result = [(str(label), int(count)) for label, count in zip(labels, counts)]
     >>> print(result)
@@ -327,7 +327,6 @@ class FixedThresholdClassifier(BaseThresholdClassifier):
     High: 14
     Low: 218
     Medium: 18
-    <BLANKLINE>
     """
 
     _parameter_constraints: dict = {

@adrinjalali adrinjalali changed the title Add multi-threshold classification to FixedThresholdClassifier #30452 ENH Add multi-threshold classification to FixedThresholdClassifier Jun 19, 2025
@pedroL0pes pedroL0pes force-pushed the multi_thresh_feature branch from 05bc405 to f2ef469 Compare June 19, 2025 15:50
@nunofbiomartins
Copy link

Hello @adrinjalali .
All tests are now passing. Thank you for your help.
Is the changelog required or can it be bypassed? If it is required, what do we have to do?

@adrinjalali
Copy link
Member

Yes, this requires an enhancement or a feature changelog entry.

cc @glemaitre for a review.

…-learn#30452

This commit adds support for multi-threshold classification to
FixedThresholdClassifier, enabling discretization of continuous
decision scores into multiple classes.
The classifier now accepts a list of thresholds and optional labels
for each bin.
Includes:
- Unit tests covering the new functionality
- Documentation and usage example

Co-authored-by: Pedro Lopes <[email protected]>
@pedroL0pes
Copy link
Contributor Author

pedroL0pes commented Jun 20, 2025

Hi @glemaitre ,
All tests are passing, and the PR is ready for review. We've also added the required changelog entry. Let me know if anything else is needed or if you'd like changes to the implementation or docs.
Thanks in advance for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants