ValueError 'balanced_accuracy' is not a valid scoring value in scikit-learn
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If scikit-learn raises ValueError: 'balanced_accuracy' is not a valid scoring value, the problem is usually version compatibility rather than the model code itself. The metric name exists in newer versions of scikit-learn, but older releases do not recognize it as a built-in scorer string.
Core Sections
Why the error happens
Model-selection helpers such as cross_val_score, GridSearchCV, and RandomizedSearchCV accept scoring names as strings. Those names are resolved against a registry inside the installed scikit-learn version. If your environment predates support for balanced_accuracy, the lookup fails and you get the error.
The code is fine on a compatible version. It fails only when the installed scorer registry does not include that name.
Check which scorers your environment supports
Before changing code, inspect the environment. This avoids guessing whether the issue is a typo, a version mismatch, or a packaging problem.
On older versions where get_scorer_names is not available, inspect sklearn.metrics.SCORERS instead.
If the result is False, the environment does not know that scoring string yet.
Upgrade if you control the environment
The cleanest fix is to upgrade scikit-learn to a version that supports the scorer. That keeps your training code simple and aligned with current documentation.
After upgrading, confirm the active interpreter is using the package you just installed. In notebook environments and mixed virtual environments, that assumption is often wrong.
Checking the import path prevents a common situation where pip upgrades one environment while the notebook kernel still points to another.
Use a custom scorer when upgrade is not possible
If you are pinned to an older release, create an explicit scorer from balanced_accuracy_score. This avoids dependency on the built-in scorer registry.
This works because cross_val_score accepts a scorer callable, not only a string.
Why balanced accuracy matters for imbalanced classes
Balanced accuracy is the average recall across classes. It prevents majority classes from dominating the evaluation. That makes it useful when normal accuracy would look good even though the minority class is never predicted correctly.
Standard accuracy reports strong performance because class 0 dominates the sample. Balanced accuracy exposes the failure on class 1 by giving each class equal weight in the final metric.
Use the metric consistently in search and reporting
If you select hyperparameters with balanced accuracy, report balanced accuracy in evaluation summaries as well. Optimizing with one metric and communicating another often leads to confusion during model review.
A good workflow is:
- choose balanced accuracy because class imbalance matters
- use it in cross-validation or grid search
- report it alongside confusion matrices and per-class recall
- compare it to plain accuracy so stakeholders can see why the chosen metric is stricter
That keeps metric choice tied to the actual business risk of missing minority cases.
Common Pitfalls
- Assuming the error means the metric formula is wrong when the real issue is simply an older scikit-learn version.
- Upgrading
scikit-learnin one environment while the active notebook kernel or application interpreter still imports another installation. - Passing a metric function directly where a scorer string or scorer callable is expected without checking the relevant API signature.
- Choosing plain accuracy for a highly imbalanced dataset and then being surprised when the model misses minority cases.
- Using a custom scorer in model selection but reporting only default accuracy later, which makes tuning decisions hard to justify.
Summary
- The error usually means your installed scikit-learn version does not recognize
balanced_accuracyas a built-in scoring string. - Check the active version and available scorers before changing code.
- Upgrade the package when possible, or use
make_scorer(balanced_accuracy_score)when you cannot. - Balanced accuracy is useful when class imbalance would make standard accuracy misleading.
- Keep the same metric through tuning, validation, and reporting so results stay coherent.

