Skip to content

DOC Supervised clustering metrics specify array input dtype #32930

Description

@lucyleeow

Several of our supervised clustering metrics specify/suggest (??) that the array input should be of type int or integral, e.g.:

pair_confusion_matrix:

labels_true : array-like of shape (n_samples,), dtype=integral
Ground truth class labels to be used as a reference.
labels_pred : array-like of shape (n_samples,), dtype=integral
Cluster labels to evaluate.

rand_score:

labels_true : array-like of shape (n_samples,), dtype=integral
Ground truth class labels to be used as a reference.
labels_pred : array-like of shape (n_samples,), dtype=integral
Cluster labels to evaluate.

mutual_info_score:

labels_true : array-like of shape (n_samples,), dtype=integral
A clustering of the data into disjoint subsets, called :math:`U` in
the above formula.
labels_pred : array-like of shape (n_samples,), dtype=integral
A clustering of the data into disjoint subsets, called :math:`V` in
the above formula.

fowlkes_mallows_score:

labels_true : array-like of shape (n_samples,), dtype=int
A clustering of the data into disjoint subsets.
labels_pred : array-like of shape (n_samples,), dtype=int
A clustering of the data into disjoint subsets.

Looking at the code/testing a few - we don't seem to enforce this. I also have not seen specification of dtype of array input much elsewhere in the codebase. Is this dytpe specification needed in the docs?

Edit: I mean is it worth clarifying it is a suggestion and not enforced?

Activity

  1. changed the title [-]DOC Supervised clustering metrics input dytpe specification[/-] [+]DOC Supervised clustering metrics specify array input dtype[/+] on Dec 23, 2025
  2. added
    Needs Triage
    on Dec 23, 2025
  3. added and removed
    Needs Triage
    on Dec 23, 2025
  4. rustamali9183 commented on Dec 23, 2025

    @rustamali9183
  5. Othman-vram commented on Dec 25, 2025

    @Othman-vram
  6. Othman-vram commented on Dec 25, 2025

    @Othman-vram
  7. akhilkumar-dot commented on Dec 25, 2025

    @akhilkumar-dot
  8. StefanieSenger commented on Dec 29, 2025

    @StefanieSenger
    Member

    Some of the clustering metrics raise a warning if the input type is continuous:

    if "continuous" in (type_pred, type_label):
    msg = (
    "Clustering metrics expects discrete values but received"
    f" {type_label} values for label, and {type_pred} values "
    "for target"
    )
    warnings.warn(msg, UserWarning)

    It was added in #17911. (I am wondering why we would only warn and not error here though.)

    Since we expect integers, I think it is consistent to mention this in the docs, but then the code needs to comply to this.

    Looking at the code/testing a few - we don't seem to enforce this.

    Is it, that there were only warnings, or did those you had tested not even warn? I would expect an error here and I wonder if we should error instead of warn.

    It would be an interesting next step to check all the clustering metrics from this module and see if all that should raise a warning actually use check_clusterings() internally. It is probably an easy issue to work on, but not an issue for a first time contributor. Do we know someone with a bit of experience in the repo who'd be happy to take care of this?

  9. lucyleeow commented on Dec 30, 2025

    @lucyleeow
    MemberAuthor

    Some of the clustering metrics raise a warning if the input type is continuous:

    Good point I didn't look into that part. However, discrete vs continuous does not seem to depend on dtype. e.g.:

    In [7]: from sklearn.utils.multiclass import type_of_target
    
    In [8]: type_of_target(np.array([1.0, 1.0, 0.0, 0.0]))
    Out[8]: 'binary'

    (I checked that dtype of np.array([1.0, 1.0, 0.0, 0.0]) is float64)

    And I just checked with the docstring example, changing the input to float instead of int, and no warning or error for e.g., rand_score:

    In [1]: from sklearn.metrics.cluster import rand_score
    In [4]: import numpy as np
    In [5]: rand_score([0, 0, 1, 1.0], np.array([1.0, 1.0, 0, 0]))
    Out[5]: 1.0
  10. Ravichandranayakar commented on Dec 30, 2025

    @Ravichandranayakar
    Contributor

    hi i investigated this issue locally by testing int , str and float inputs
    i also tested "discrete floats" (like 0.0 and 1.0) to see if the warning is based on type or value

    here are the results for all 4 metrics

    1. int and str inputs -> works warnings=0
    2. discrete floats (0.0, 1.0) -> works warnings=0
    3. continuous floats (0.1, 0.2) -> works but triggers the expected "discrete values" warning

    this proves that the functions actually check if the values are discrete not strictly if the dtype is int
    so the documentation requirement for dtype=integral is definitely too strict

    i would be happy to update the docstrings to clarify that discrete labels (including strings) are valid inputs

    import warnings
    from sklearn.metrics import pair_confusion_matrix, rand_score, mutual_info_score, fowlkes_mallows_score
    
    metrics = [
        ("pair_confusion_matrix", pair_confusion_matrix),
        ("rand_score", rand_score),
        ("mutual_info_score", mutual_info_score),
        ("fowlkes_mallows_score", fowlkes_mallows_score),
    ]
    
    labels = {
        "int": ([1,2,1,2],[1,2,1,2]),
        "str": (['a','b','a','b'],['a','b','a','b']),
        "float_cont": ([0.1,0.2,0.1,0.2],[0.1,0.2,0.1,0.2]),
        "float_disc": ([0.0,0.0,1.0,1.0],[1.0,1.0,0.0,0.0]),
    }
    
    for name, metric in metrics:
        print(f"\n--- {name} ---")
        for dtype, (y1, y2) in labels.items():
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter("always")
                res = metric(y1, y2)
                print(f"{dtype} -> result={res}, warnings={len(w)}")
  11. StefanieSenger commented on Dec 31, 2025

    @StefanieSenger
    Member

    However, discrete vs continuous does not seem to depend on dtype

    Yes, that's true. Providing discrete floats works and doesn't trigger the warning. And the params that are documented to hold the int dtype are actually all starting on "label...", which suggests discreteness.

    Now thinking about this, I would go with the suggestion of removing the dtype specification from the docs, without adding any other information constraints on the labels.

    This now seems to me as a separate issue, but since we talked about it in here:
    Concerning the warning, I was thinking that it was chosen to warn and not to error to keep the code executable, since type_of_target is not precise.

    Do you think that using type_of_target is the right choice to be used in check_clusterings? Actually, we only need to check if the inputs are convertible into ints. check_array can do that, I think. In this case, we could even error instead of to warn, because check_array can give us a precise answer.

  12. Ravichandranayakar commented on Jan 1, 2026

    @Ravichandranayakar
    Contributor

    Thank you @StefanieSenger.

    I agree that the warning/error logic in check_clusterings is a separate architectural discussion.

    Since there is consensus on the documentation part removing the dtype specification , I am happy to proceed with only that adjustment to solve this particular issue.

    @lucyleeow My previous PR was only for the docs. Do you want me to reopen that one, or create a new PR.

  13. manshirahangdale commented on Jan 14, 2026

    @manshirahangdale
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