ML.NET Model Builder generated code to return multiple prediction results

664 Views Asked by At

I selected Text Classification in Model Builder and was able to evaluate the model with sample data. I get multiple results rows with percent accuracy in Visual Studio plugin like below:

Results

Result1 60%


Result2 5%

Result3 <1%

Result4 <1%

Result5 <1%

However when using the generated model code I only get one prediction result using

var predictionResult = ConsumeModel.Predict(input);

Question: How do I use the generated model to return multiple prediction results with percentage accuracy for one model input? Similar to how model builder displays in the Evaluate step in Visual Studio plugin.

1

There are 1 best solutions below

0
On

The easiest way is to base it on what Score returns and match it with the Label name in the Dataset folder. Then use comparison to rearrange the order of results received.

For example in Dataset Animals below:

(Image) Convert scores to Label name

PredictedLabel: Cat
Score: [0.003, 0.981, 0.01, 0.0025, 0.003, 0.0015]
------------Similar
Bird: 0.003
Cat: 0.981
Dog: 0.01
Mouse: 0.0025
Snake: 0.003
Tiger: 0.0015

You can put it in a 2-dimensional array and sort it. Finally, you get the result as below.

Cat: 0.981
Dog: 0.01
Bird: 0.003
Snake: 0.003
Mouse: 0.0025
Tiger: 0.0015

You can refer to the following 2-dimensional array sort code, it will sort Score and keep your LabelName.

int j = 1;

        Array.Sort(array, delegate (object[] x, object[] y)
        {
            return (y[j] as IComparable).CompareTo(x[j]);
        });

Now, you can use the loop to retrieve the results and present them the way you want.