Repository navigation
Description
Warning
This is not a good first (or second) issue to contribute. If you are interested in contributing to scikit-learn please have a look at our contributing doc and in particular the section Issues for new contributors.
Noticed while working on #32755
Classification metrics (CLASSIFICATION_METRICS from test_common.py, they are all defined in sklearn/metrics/_classification.py, but not all metrics in _classification.py fall in this list) vary in how they handle mixed string and numeric inputs (e.g., y_true is string and y_pred is numeric).
precision_recall_fscore_supportand friends (precision_score,recall_score,f1_score,fbeta_scoreand relatedjaccard_score) - raisesValueError:- via
_check_set_wise_labels, which callsunique_labels, which does not allow "mix of string and integer labels“ - note that these metrics do take a
pos_labelparameter, so theoretically wheny_trueis string, we could usey_true == pos_labelto convert to [1,0]. This would not make sense ify_predwas string.
- via
confusion_matrix(and metrics that use it -balanced_accuracy_score,cohen_kappa_score) - raisesValueErrorONLY whenlabels=None:labels=None: callsunique_labelslike abovelabelsis set - AFAICT no error but the confusion matrix would only contain 0. e.g., ify_trueis string andlabelsis set to all possible unique values iny_true, we would do the following conversion:
scikit-learn/sklearn/metrics/_classification.py
Lines 584 to 587 in 66200f1
| if need_index_conversion: | |
| label_to_ind = {label: index for index, label in enumerate(labels)} | |
| y_pred = np.array([label_to_ind.get(label, n_labels + 1) for label in y_pred]) | |
| y_true = np.array([label_to_ind.get(label, n_labels + 1) for label in y_true]) |
Since none of the string labels are present in numeric y_pred, y_pred would be converted to array that consists only of n_labels + 1 values
multilabel_confusion_matrix- always raises raisesValueError(again viaunique_labels):- unlike
confusion_matrix, even whenlabelsis given, we callunique_labels(y_true, y_pred)to get all labels present, and if any are not provided inlabelsthey are added.
- unlike
The following all DO NOT error:
accuracy_score- no error but result would always be 0 (asscore = y_true == y_predwould always be 0). Note not relevant for multilabel cases as input needs to be label indicator matrix.hamming_loss- no error but result would always be 1. Again not relevant for multilabel.zero_one_loss- no error but result would always be 1 (whennormalize=False). Again not relevant for multilabel.matthews_corrcoef- no error but result would always be 0. Note that we transformy_trueandy_predusingLabelEncoderfit ony_trueandy_predconcat (meaningy_trueandy_predwill be numbers, but not contain any numbers in common), before we pass toconfusion_matrix.
For these classification metrics which require y_pred to be thresholded predictions, I don't think it makes sense to have mixed string and numeri (e.g., how would you match ['apple', 'orange', 'apple] to [2,3,2] ?). Indeed, for the metrics that do not error, the result is the 'worst' value.
For metrics that accept pos_label the way forward is less clear. Note that for several continuous classification metrics we do use something like y_true == pos_label, thus allowing y_true to be string.
Note that I will open a separate issue around this for continuous classification metrics (i.e. those in _ranking.py).
cc @ogrisel
Activity
ogrisel commentedon Jan 12, 2026
I don't think we need thresholded/hard classification metrics to support mixed string/numeric inputs. The classification labels and the hard class predictions should always use the same type of values (either string or integers). All the metrics derived from confusion matrix fall into that case.
But we do need mixed inputs for unthresholded/soft classification metrics such as log loss, Brier score, ROC AUC, Average Precision... Basically any metric that accepts a mix of true string/integer label along with continuous predicteds scores.
lucyleeow commentedon Jan 12, 2026
I've realised there was a bad typo above, which I just fixed. The last 4 metrics do not error.
So for the 4 metrics that do not currently error, we should amend them to make them error? (and also amend
multilabel_confusion_matrixsuch that it always errors)Edit: Just checked the continuous classification metrics and they all have consistent behaviour, I had thought they'd be a mixed bag like the classification metrics
ogrisel commentedon Jan 12, 2026
I think the thresholded metrics should raise an error if passed inconsistent types of inputs.
[-]Mixed string/numeric input for classification metrics[/-][+]Mixed string/numeric when input is list for classification metrics[/+]lucyleeow commentedon Jan 15, 2026
I've just realised the lack of error for inconsistent types is only for lists (and non-object type numpy arrays), we actually handle array inputs fine (#18192):
scikit-learn/sklearn/metrics/_classification.py
Lines 154 to 165 in 6c084a8
(
_union1dwill not raise an error for mixed str and number types when input is list)I will work on fixing this for lists and updating the test.