Skip to content

Make class labels on the ConfusionMatrix publicly readable for custom charting support #7176

Closed

Description

@QuantumHope

Is your feature request related to a problem? Please describe.
I'm trying to build a library of data science charts for visualizing the results of ML.NET model training. One of those is a graphical confusion matrix pictured below (no offense to the wonderful built-in formatted table option present in ML.NET already).
image

Unfortunately, there's no public way of getting the names of classes for the matrix as ConfusionMatrix.PredictedClassesIndicators is internal. This means that class labels in multi-class classification charts must:

  1. Use an arbitrary label like "Class 1"
  2. Require the caller to specify an array of class names
  3. Call the public ConfusionMatrix.GetFormattedConfusionMatrix and parse the output to get the class names from the generated output.

Describe the solution you'd like
Either make PredictedClassesIndicators publicly gettable or provide a GetClassLabel(int classIndex): string method for accessing this information.

Describe alternatives you've considered
Right now I am requiring callers to provide an array of class names for multi-class classification. However, if this feature goes some time before it is implemented, I may decide to write code to parse the results of GetFormattedConfusionMatrix which does include those internal labels.

Additional context
My goal is for ML.NET to be as viable as SciKit-Learn in as many contexts as possible. Right now, I'm using ML.NET for my master's program and I'd much rather turn in a report with a graphical confusion matrix than a formatted text confusion matrix. Also, I'm running these charts in a Polyglot Notebook.

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    area-CoreCore ML.NET APIs and shared functionality
    and removed on Sep 8, 2026
  2. rosebyte commented on Oct 22, 2026

    @rosebyte
    Member

    Triage: The PredictedClassesIndicators is intentionally not public. For multiclass classification, you can retrieve the class labels through the public schema APIs, without parsing the formatted confusion table. The evaluator itself uses the Score column's SlotNames annotations, so reading those preserves the matrix's class ordering:

    var scoredData = model.Transform(testData);
    var metrics = mlContext.MulticlassClassification.Evaluate(scoredData);
    var scoreColumn = scoredData.Schema["Score"];
    
    string[] labels;
    if (scoreColumn.HasSlotNames())
    {
        VBuffer<ReadOnlyMemory<char>> names = default;
        scoreColumn.GetSlotNames(ref names);
        labels = names.DenseValues().Select(name => name.ToString()).ToArray();
    }
    else
    {
        labels = Enumerable.Range(0, metrics.ConfusionMatrix.NumberOfClasses)
            .Select(i => i.ToString()).ToArray();
    }

    Here, labels[i] identifies both row i and column i of metrics.ConfusionMatrix.Counts. Rows represent actual classes and columns represent predicted classes. If slot names are unavailable, the fallback produces numeric indicators, matching the evaluator's behaviour; it cannot recover names that are missing from the metadata. If you use a custom score-column name, use that name both when evaluating and when reading the schema.

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

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-CoreCore ML.NET APIs and shared functionalityenhancementNew feature or requestgreendevelopment

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions