ML.Net 0.7 - Get Scores and Labels for MulticlassClassification
I'm using ML.NET 0.7 and have a MulticlassClassification model with the following result class:
public class TestClassOut
{
public string Id { get; set; }
public float Score { get; set; }
public string PredictedLabel { get; set; }
}
I'd like to know the scores and the corresponding labels on the Scores property. Feels like I should be able to make the property a Tuple<string,float> or similar to get the label that the score represents.
I understand that there was a method on V0.5:
model.TryGetScoreLabelNames(out scoreLabels);
But can't seem to find the equivalent in V0.7.
Can this be done? if so how?
c# ml.net
add a comment |
I'm using ML.NET 0.7 and have a MulticlassClassification model with the following result class:
public class TestClassOut
{
public string Id { get; set; }
public float Score { get; set; }
public string PredictedLabel { get; set; }
}
I'd like to know the scores and the corresponding labels on the Scores property. Feels like I should be able to make the property a Tuple<string,float> or similar to get the label that the score represents.
I understand that there was a method on V0.5:
model.TryGetScoreLabelNames(out scoreLabels);
But can't seem to find the equivalent in V0.7.
Can this be done? if so how?
c# ml.net
add a comment |
I'm using ML.NET 0.7 and have a MulticlassClassification model with the following result class:
public class TestClassOut
{
public string Id { get; set; }
public float Score { get; set; }
public string PredictedLabel { get; set; }
}
I'd like to know the scores and the corresponding labels on the Scores property. Feels like I should be able to make the property a Tuple<string,float> or similar to get the label that the score represents.
I understand that there was a method on V0.5:
model.TryGetScoreLabelNames(out scoreLabels);
But can't seem to find the equivalent in V0.7.
Can this be done? if so how?
c# ml.net
I'm using ML.NET 0.7 and have a MulticlassClassification model with the following result class:
public class TestClassOut
{
public string Id { get; set; }
public float Score { get; set; }
public string PredictedLabel { get; set; }
}
I'd like to know the scores and the corresponding labels on the Scores property. Feels like I should be able to make the property a Tuple<string,float> or similar to get the label that the score represents.
I understand that there was a method on V0.5:
model.TryGetScoreLabelNames(out scoreLabels);
But can't seem to find the equivalent in V0.7.
Can this be done? if so how?
c# ml.net
c# ml.net
edited Nov 16 '18 at 9:02
martijnn2008
2,43832334
2,43832334
asked Nov 12 '18 at 16:26
jondow
162111
162111
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is probably not the answer you're looking for, but I ended up copying the code from TryGetScoreLabelNames (it's in the Legacy namespace as of 0.7) and tweaking it to use the schema from my input data. The dataView below is an IDataView I created from my prediction input data so I could get the schema off of it.
public bool TryGetScoreLabelNames(out string names, string scoreColumnName = DefaultColumnNames.Score)
{
names = (string)null;
Schema outputSchema = model.GetOutputSchema(dataView.Schema);
int col = -1;
if (!outputSchema.TryGetColumnIndex(scoreColumnName, out col))
return false;
int valueCount = outputSchema.GetColumnType(col).ValueCount;
if (!outputSchema.HasSlotNames(col, valueCount))
return false;
VBuffer<ReadOnlyMemory<char>> vbuffer = new VBuffer<ReadOnlyMemory<char>>();
outputSchema.GetMetadata<VBuffer<ReadOnlyMemory<char>>>("SlotNames", col, ref vbuffer);
if (vbuffer.Length != valueCount)
return false;
names = new string[valueCount];
int num = 0;
foreach (ReadOnlyMemory<char> denseValue in vbuffer.DenseValues())
names[num++] = denseValue.ToString();
return true;
}
I also asked this question in gitter for ml.net (https://gitter.im/dotnet/mlnet) and got this response from Zruty0
my best suggestion is to convert labels to 0..(N-1) beforehand, then
train, and then inspect the resulting 'Score' column. It'll be a
vector of size N, with per-class scores. PredictedLabel is actually
just argmax(Score), and you can get the 2nd and other candidates by
sorting Score
If you have a static set of classes this might be a better option, but my situation has an ever-growing set of classes.
NoteValueCountwill be gone in 0.8 so you'll have to cast((VectorType)col).Sizeinstead.
– ClojureMostly
Nov 13 '18 at 6:41
That does the trick, thanks for the advice.
– jondow
Nov 13 '18 at 9:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266283%2fml-net-0-7-get-scores-and-labels-for-multiclassclassification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is probably not the answer you're looking for, but I ended up copying the code from TryGetScoreLabelNames (it's in the Legacy namespace as of 0.7) and tweaking it to use the schema from my input data. The dataView below is an IDataView I created from my prediction input data so I could get the schema off of it.
public bool TryGetScoreLabelNames(out string names, string scoreColumnName = DefaultColumnNames.Score)
{
names = (string)null;
Schema outputSchema = model.GetOutputSchema(dataView.Schema);
int col = -1;
if (!outputSchema.TryGetColumnIndex(scoreColumnName, out col))
return false;
int valueCount = outputSchema.GetColumnType(col).ValueCount;
if (!outputSchema.HasSlotNames(col, valueCount))
return false;
VBuffer<ReadOnlyMemory<char>> vbuffer = new VBuffer<ReadOnlyMemory<char>>();
outputSchema.GetMetadata<VBuffer<ReadOnlyMemory<char>>>("SlotNames", col, ref vbuffer);
if (vbuffer.Length != valueCount)
return false;
names = new string[valueCount];
int num = 0;
foreach (ReadOnlyMemory<char> denseValue in vbuffer.DenseValues())
names[num++] = denseValue.ToString();
return true;
}
I also asked this question in gitter for ml.net (https://gitter.im/dotnet/mlnet) and got this response from Zruty0
my best suggestion is to convert labels to 0..(N-1) beforehand, then
train, and then inspect the resulting 'Score' column. It'll be a
vector of size N, with per-class scores. PredictedLabel is actually
just argmax(Score), and you can get the 2nd and other candidates by
sorting Score
If you have a static set of classes this might be a better option, but my situation has an ever-growing set of classes.
NoteValueCountwill be gone in 0.8 so you'll have to cast((VectorType)col).Sizeinstead.
– ClojureMostly
Nov 13 '18 at 6:41
That does the trick, thanks for the advice.
– jondow
Nov 13 '18 at 9:37
add a comment |
This is probably not the answer you're looking for, but I ended up copying the code from TryGetScoreLabelNames (it's in the Legacy namespace as of 0.7) and tweaking it to use the schema from my input data. The dataView below is an IDataView I created from my prediction input data so I could get the schema off of it.
public bool TryGetScoreLabelNames(out string names, string scoreColumnName = DefaultColumnNames.Score)
{
names = (string)null;
Schema outputSchema = model.GetOutputSchema(dataView.Schema);
int col = -1;
if (!outputSchema.TryGetColumnIndex(scoreColumnName, out col))
return false;
int valueCount = outputSchema.GetColumnType(col).ValueCount;
if (!outputSchema.HasSlotNames(col, valueCount))
return false;
VBuffer<ReadOnlyMemory<char>> vbuffer = new VBuffer<ReadOnlyMemory<char>>();
outputSchema.GetMetadata<VBuffer<ReadOnlyMemory<char>>>("SlotNames", col, ref vbuffer);
if (vbuffer.Length != valueCount)
return false;
names = new string[valueCount];
int num = 0;
foreach (ReadOnlyMemory<char> denseValue in vbuffer.DenseValues())
names[num++] = denseValue.ToString();
return true;
}
I also asked this question in gitter for ml.net (https://gitter.im/dotnet/mlnet) and got this response from Zruty0
my best suggestion is to convert labels to 0..(N-1) beforehand, then
train, and then inspect the resulting 'Score' column. It'll be a
vector of size N, with per-class scores. PredictedLabel is actually
just argmax(Score), and you can get the 2nd and other candidates by
sorting Score
If you have a static set of classes this might be a better option, but my situation has an ever-growing set of classes.
NoteValueCountwill be gone in 0.8 so you'll have to cast((VectorType)col).Sizeinstead.
– ClojureMostly
Nov 13 '18 at 6:41
That does the trick, thanks for the advice.
– jondow
Nov 13 '18 at 9:37
add a comment |
This is probably not the answer you're looking for, but I ended up copying the code from TryGetScoreLabelNames (it's in the Legacy namespace as of 0.7) and tweaking it to use the schema from my input data. The dataView below is an IDataView I created from my prediction input data so I could get the schema off of it.
public bool TryGetScoreLabelNames(out string names, string scoreColumnName = DefaultColumnNames.Score)
{
names = (string)null;
Schema outputSchema = model.GetOutputSchema(dataView.Schema);
int col = -1;
if (!outputSchema.TryGetColumnIndex(scoreColumnName, out col))
return false;
int valueCount = outputSchema.GetColumnType(col).ValueCount;
if (!outputSchema.HasSlotNames(col, valueCount))
return false;
VBuffer<ReadOnlyMemory<char>> vbuffer = new VBuffer<ReadOnlyMemory<char>>();
outputSchema.GetMetadata<VBuffer<ReadOnlyMemory<char>>>("SlotNames", col, ref vbuffer);
if (vbuffer.Length != valueCount)
return false;
names = new string[valueCount];
int num = 0;
foreach (ReadOnlyMemory<char> denseValue in vbuffer.DenseValues())
names[num++] = denseValue.ToString();
return true;
}
I also asked this question in gitter for ml.net (https://gitter.im/dotnet/mlnet) and got this response from Zruty0
my best suggestion is to convert labels to 0..(N-1) beforehand, then
train, and then inspect the resulting 'Score' column. It'll be a
vector of size N, with per-class scores. PredictedLabel is actually
just argmax(Score), and you can get the 2nd and other candidates by
sorting Score
If you have a static set of classes this might be a better option, but my situation has an ever-growing set of classes.
This is probably not the answer you're looking for, but I ended up copying the code from TryGetScoreLabelNames (it's in the Legacy namespace as of 0.7) and tweaking it to use the schema from my input data. The dataView below is an IDataView I created from my prediction input data so I could get the schema off of it.
public bool TryGetScoreLabelNames(out string names, string scoreColumnName = DefaultColumnNames.Score)
{
names = (string)null;
Schema outputSchema = model.GetOutputSchema(dataView.Schema);
int col = -1;
if (!outputSchema.TryGetColumnIndex(scoreColumnName, out col))
return false;
int valueCount = outputSchema.GetColumnType(col).ValueCount;
if (!outputSchema.HasSlotNames(col, valueCount))
return false;
VBuffer<ReadOnlyMemory<char>> vbuffer = new VBuffer<ReadOnlyMemory<char>>();
outputSchema.GetMetadata<VBuffer<ReadOnlyMemory<char>>>("SlotNames", col, ref vbuffer);
if (vbuffer.Length != valueCount)
return false;
names = new string[valueCount];
int num = 0;
foreach (ReadOnlyMemory<char> denseValue in vbuffer.DenseValues())
names[num++] = denseValue.ToString();
return true;
}
I also asked this question in gitter for ml.net (https://gitter.im/dotnet/mlnet) and got this response from Zruty0
my best suggestion is to convert labels to 0..(N-1) beforehand, then
train, and then inspect the resulting 'Score' column. It'll be a
vector of size N, with per-class scores. PredictedLabel is actually
just argmax(Score), and you can get the 2nd and other candidates by
sorting Score
If you have a static set of classes this might be a better option, but my situation has an ever-growing set of classes.
answered Nov 12 '18 at 21:57
takvor
162
162
NoteValueCountwill be gone in 0.8 so you'll have to cast((VectorType)col).Sizeinstead.
– ClojureMostly
Nov 13 '18 at 6:41
That does the trick, thanks for the advice.
– jondow
Nov 13 '18 at 9:37
add a comment |
NoteValueCountwill be gone in 0.8 so you'll have to cast((VectorType)col).Sizeinstead.
– ClojureMostly
Nov 13 '18 at 6:41
That does the trick, thanks for the advice.
– jondow
Nov 13 '18 at 9:37
Note
ValueCount will be gone in 0.8 so you'll have to cast ((VectorType)col).Size instead.– ClojureMostly
Nov 13 '18 at 6:41
Note
ValueCount will be gone in 0.8 so you'll have to cast ((VectorType)col).Size instead.– ClojureMostly
Nov 13 '18 at 6:41
That does the trick, thanks for the advice.
– jondow
Nov 13 '18 at 9:37
That does the trick, thanks for the advice.
– jondow
Nov 13 '18 at 9:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266283%2fml-net-0-7-get-scores-and-labels-for-multiclassclassification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown