Skip to content

Mixed string/numeric when input is list for classification metrics #33045

Closed

Description

@lucyleeow

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_support and friends (precision_score, recall_score, f1_score, fbeta_score and related jaccard_score) - raises ValueError:
    • via _check_set_wise_labels, which calls unique_labels, which does not allow "mix of string and integer labels“
    • note that these metrics do take a pos_label parameter, so theoretically when y_true is string, we could use y_true == pos_label to convert to [1,0]. This would not make sense if y_pred was string.
  • confusion_matrix (and metrics that use it - balanced_accuracy_score, cohen_kappa_score) - raises ValueError ONLY when labels=None:
    • labels=None: calls unique_labels like above
    • labels is set - AFAICT no error but the confusion matrix would only contain 0. e.g., if y_true is string and labels is set to all possible unique values in y_true, we would do the following conversion:

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 raises ValueError (again via unique_labels):
    • unlike confusion_matrix, even when labels is given, we call unique_labels(y_true, y_pred) to get all labels present, and if any are not provided in labels they are added.

The following all DO NOT error:

  • accuracy_score - no error but result would always be 0 (as score = y_true == y_pred would 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 (when normalize=False). Again not relevant for multilabel.
  • matthews_corrcoef - no error but result would always be 0. Note that we transform y_true and y_pred using LabelEncoder fit on y_true and y_pred concat (meaning y_true and y_pred will be numbers, but not contain any numbers in common), before we pass to confusion_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

  1. added
    Needs Triage
    on Jan 11, 2026
  2. ogrisel commented on Jan 12, 2026

    @ogrisel
    Member

    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.

  3. lucyleeow commented on Jan 12, 2026

    @lucyleeow
    MemberAuthor

    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_matrix such 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

  4. ogrisel commented on Jan 12, 2026

    @ogrisel
    Member

    I think the thresholded metrics should raise an error if passed inconsistent types of inputs.

  5. changed the title [-]Mixed string/numeric input for classification metrics[/-] [+]Mixed string/numeric when input is list for classification metrics[/+] on Jan 15, 2026
  6. lucyleeow commented on Jan 15, 2026

    @lucyleeow
    MemberAuthor

    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):

    unique_values = _union1d(y_true, y_pred, xp)
    except TypeError as e:
    # We expect y_true and y_pred to be of the same data type.
    # If `y_true` was provided to the classifier as strings,
    # `y_pred` given by the classifier will also be encoded with
    # strings. So we raise a meaningful error
    raise TypeError(
    "Labels in y_true and y_pred should be of the same type. "
    f"Got y_true={xp.unique(y_true)} and "
    f"y_pred={xp.unique(y_pred)}. Make sure that the "
    "predictions provided by the classifier coincides with "
    "the true labels."

    (_union1d will 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.

  7. removed
    Needs Triage
    on Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions