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

Skip to content

FIX: Handle single class edge case in balanced_accuracy_score(adjusted=True) - #34937

Closed
deepekaguru wants to merge 1 commit into
scikit-learn:mainfrom
deepekaguru:main
Closed

FIX: Handle single class edge case in balanced_accuracy_score(adjusted=True)#34937
deepekaguru wants to merge 1 commit into
scikit-learn:mainfrom
deepekaguru:main

Conversation

@deepekaguru

Copy link
Copy Markdown

Issue

Fixes #34889 - balanced_accuracy_score(adjusted=True) returns -inf when only one class survives NaN filtering.

Root Cause

The adjustment formula: score = (score - chance) / (1 - chance)
When only 1 class survives:

  • chance = 1/1 = 1.0
  • denominator = 1 - 1.0 = 0 → division by zero → -inf

Solution

Added validation check to raise informative ValueError when only one class is present with adjusted=True.

Closes #34889

@github-actions

Copy link
Copy Markdown

Thank you for your interest in contributing to scikit-learn.

The linked issue is still under discussion, and the maintainers have not yet reached consensus on how it should be resolved. Before opening a pull request, please review the "Issues tagged 'Needs Triage'" section of the Contributing Guide:
https://scikit-learn.org/stable/developers/contributing.html#issues-tagged-needs-triage

For now, we are closing this pull request.

  • If you believe your proposed change addresses the linked issue, please explain your proposal in that issue, and allow time for discussion with the maintainers so that consensus can be reached before implementation begins.

  • If you believe this is a mistake, please leave a comment on the linked issue as well.

@github-actions github-actions Bot closed this Sep 11, 2026
@github-actions github-actions Bot added linked issue not ready PR opened before its linked issue reached consensus CI:Linter failure The linter CI is failing on this PR labels Sep 11, 2026
@github-actions

Copy link
Copy Markdown

❌ Linting issues

This PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling pre-commit hooks. Instructions to enable them can be found here.

You can see the details of the linting issues under the lint job here


ruff check

ruff detected issues. Please run ruff check --fix --output-format=full locally, fix the remaining issues, and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.12.2.

Details

sklearn/metrics/_classification.py:2884:1: SyntaxError: Expected class, function definition or async function definition after decorator
     |
2882 |     prefer_skip_nested_validation=True,
2883 | )
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     | ^
2885 |     """Compute the balanced accuracy.
     |

sklearn/metrics/_classification.py:2884:1: SyntaxError: Only single target (not tuple) can be annotated
     |
2882 |     prefer_skip_nested_validation=True,
2883 | )
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     | ^
2885 |     """Compute the balanced accuracy.
     |

sklearn/metrics/_classification.py:2884:19: SyntaxError: Expected an expression
     |
2882 |     prefer_skip_nested_validation=True,
2883 | )
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     |                   ^
2885 |     """Compute the balanced accuracy.
     |

sklearn/metrics/_classification.py:2884:34: SyntaxError: Expected ',', found '='
     |
2882 |     prefer_skip_nested_validation=True,
2883 | )
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     |                                  ^
2885 |     """Compute the balanced accuracy.
     |

sklearn/metrics/_classification.py:2884:49: SyntaxError: Expected ',', found '='
     |
2882 |     prefer_skip_nested_validation=True,
2883 | )
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     |                                                 ^
2885 |     """Compute the balanced accuracy.
     |

sklearn/metrics/_classification.py:2884:57: SyntaxError: Expected an expression
     |
2882 |     prefer_skip_nested_validation=True,
2883 | )
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     |                                                         ^
2885 |     """Compute the balanced accuracy.
2886 |
2887 |     The balanced accuracy in binary and multiclass classification problems to
     |

sklearn/metrics/_classification.py:2885:1: SyntaxError: Unexpected indentation
     |
2883 | )
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
2885 |     """Compute the balanced accuracy.
     | ^
2886 |
2887 |     The balanced accuracy in binary and multiclass classification problems to
     |

sklearn/metrics/_classification.py:2972:5: SyntaxError: Expected an indented block after `if` statement
     |
2970 |     score = xp.mean(per_class)
2971 |     if adjusted:
2972 |     n_classes = per_class.shape[0]
     |     ^
2973 |     if n_classes == 1:
2974 |         raise ValueError(
     |

sklearn/metrics/_classification.py:2985:1: SyntaxError: Expected a statement
     |
2985 | @validate_params(
     | ^
2986 |     {
2987 |         "y_true": ["array-like", "sparse matrix"],
     |

Found 9 errors.

ruff format

ruff detected issues. Please run ruff format locally and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.12.2.

Details

error: Failed to parse sklearn/metrics/_classification.py:2884:1: Expected class, function definition or async function definition after decorator
960 files already formatted

pyrefly

pyrefly detected issues. Please fix them locally and push the changes. Here you can see the detected issues. Note that the installed pyrefly version is pyrefly=1.2.0.

Details

 INFO Checking project configured at `/home/runner/work/scikit-learn/scikit-learn/pyproject.toml`
ERROR Parse error: Expected class, function definition or async function definition after decorator [parse-error]
    --> sklearn/metrics/_classification.py:2884:1
     |
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     | ^
     |
ERROR Parse error: Only single target (not tuple) can be annotated [parse-error]
    --> sklearn/metrics/_classification.py:2884:1
     |
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
ERROR Parse error: Expected an expression [parse-error]
    --> sklearn/metrics/_classification.py:2884:19
     |
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     |                   ^
     |
ERROR Parse error: Expected `,`, found `=` [parse-error]
    --> sklearn/metrics/_classification.py:2884:34
     |
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     |                                  ^
     |
ERROR Parse error: Expected `,`, found `=` [parse-error]
    --> sklearn/metrics/_classification.py:2884:49
     |
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     |                                                 ^
     |
ERROR Parse error: Expected an expression [parse-error]
    --> sklearn/metrics/_classification.py:2884:57
     |
2884 | (y_true, y_pred, *, sample_weight=None, adjusted=False):
     |                                                         ^
2885 |     """Compute the balanced accuracy.
     |
ERROR Parse error: Unexpected indentation [parse-error]
    --> sklearn/metrics/_classification.py:2885:1
     |
2885 |     """Compute the balanced accuracy.
     | ^^^^
     |
ERROR Parse error: Expected an indented block after `if` statement [parse-error]
    --> sklearn/metrics/_classification.py:2972:5
     |
2972 |     n_classes = per_class.shape[0]
     |     ^^^^^^^^^
     |
ERROR Invalid `return` outside of a function [invalid-syntax]
    --> sklearn/metrics/_classification.py:2982:5
     |
2982 |     return float(score)
     |     ^^^^^^^^^^^^^^^^^^^
     |
ERROR Parse error: Expected a statement [parse-error]
    --> sklearn/metrics/_classification.py:2985:1
     |
2985 | @validate_params(
     | ^
     |
 INFO 10 errors (2 suppressed, 33 warnings not shown)

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

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

Labels

CI:Linter failure The linter CI is failing on this PR linked issue not ready PR opened before its linked issue reached consensus module:metrics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

balanced_accuracy_score(adjusted=True) returns -inf when a single class survives the NaN filter

1 participant