-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Scores to Label mapping #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
094df6a
3ea2793
f067e0f
2e11917
431117d
ef46c6c
6eae0b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,13 @@ public interface ITransformModel | |
/// </summary> | ||
ISchema InputSchema { get; } | ||
|
||
/// <summary> | ||
/// The resulting schema once applied to this model. The <see cref="InputSchema"/> might have | ||
/// columns that are not needed by this transform and these columns will be seen in the | ||
/// <see cref="OutputSchema"/> produced by this transform. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also not correct. E.g., choose columns transform. #Closed |
||
/// </summary> | ||
ISchema OutputSchema { get; } | ||
|
||
/// <summary> | ||
/// Apply the transform(s) in the model to the given input data. | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,10 +39,14 @@ public sealed class TransformModel : ITransformModel | |
/// if transform model A needs column X and model B needs Y, that is NOT produced by A, | ||
/// then trimming A's input schema would cause composition to fail. | ||
/// </summary> | ||
public ISchema InputSchema | ||
{ | ||
get { return _schemaRoot; } | ||
} | ||
public ISchema InputSchema => _schemaRoot; | ||
|
||
/// <summary> | ||
/// The resulting schema once applied to this model. The <see cref="InputSchema"/> might have | ||
/// columns that are not needed by this transform and these columns will be seen in the | ||
/// <see cref="OutputSchema"/> produced by this transform. | ||
/// </summary> | ||
public ISchema OutputSchema => _chain.Schema; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember we now have C# 7.x's niceties available to us. #Closed |
||
/// <summary> | ||
/// Create a TransformModel containing the transforms from "result" back to "input". | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
using Microsoft.ML.Runtime.Api; | ||
using Microsoft.ML.Runtime.Data; | ||
using Microsoft.ML.Runtime.EntryPoints; | ||
using Microsoft.ML.Runtime.Internal.Utilities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
@@ -29,6 +30,39 @@ internal Runtime.EntryPoints.TransformModel PredictorModel | |
get { return _predictorModel; } | ||
} | ||
|
||
/// <summary> | ||
/// Returns labels that correspond to indices of the score array in the case of | ||
/// multi-class classification problem. | ||
/// </summary> | ||
/// <param name="mapping">Label to score mapping</param> | ||
/// <param name="scoreColumnName">Name of the score column</param> | ||
/// <returns></returns> | ||
public bool TryGetScoreLabelMapping(out string[] mapping, string scoreColumnName = DefaultColumnNames.Score) | ||
{ | ||
mapping = null; | ||
ISchema schema = _predictorModel.OutputSchema; | ||
int colIndex = -1; | ||
if (!schema.TryGetColumnIndex(scoreColumnName, out colIndex)) | ||
return false; | ||
|
||
int expectedLabelCount = schema.GetColumnType(colIndex).AsVector.ValueCount; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (!schema.HasSlotNames(colIndex, expectedLabelCount)) | ||
return false; | ||
|
||
VBuffer<DvText> labels = default; | ||
schema.GetMetadata(MetadataUtils.Kinds.SlotNames, colIndex, ref labels); | ||
VBufferUtils.Densify(ref labels); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not |
||
|
||
if (labels.Length != expectedLabelCount) | ||
return false; | ||
|
||
mapping = new string[labels.Length]; | ||
for (int index = 0; index < labels.Count; index++) | ||
mapping[index] = labels.Values[index].ToString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rephrase as enumeration over |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: extra lines #Resolved |
||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Read model from file asynchronously. | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,14 @@ public void TrainAndPredictIrisModelWithStringLabelTest() | |
pipeline.Add(new StochasticDualCoordinateAscentClassifier()); | ||
|
||
PredictionModel<IrisDataWithStringLabel, IrisPrediction> model = pipeline.Train<IrisDataWithStringLabel, IrisPrediction>(); | ||
string[] scoreLabels; | ||
model.TryGetScoreLabelMapping(out scoreLabels); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why I can't just define
and fill it automatically if it's possible? #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In reply to: 190754719 [](ancestors = 190754719) |
||
|
||
Assert.NotNull(scoreLabels); | ||
Assert.Equal(3, scoreLabels.Length); | ||
Assert.Equal("Iris-setosa", scoreLabels[0]); | ||
Assert.Equal("Iris-versicolor", scoreLabels[1]); | ||
Assert.Equal("Iris-virginica", scoreLabels[2]); | ||
|
||
IrisPrediction prediction = model.Predict(new IrisDataWithStringLabel() | ||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is not correct. Indeed the point of writing this documentation is to clarify to users that this will not necessarily be the schema when applied. :/ #Closed