public void TrainAndPredictIrisModelWithStringLabelTest() { string dataPath = GetDataPath("iris.data"); var pipeline = new LearningPipeline(); pipeline.Add(new TextLoader(dataPath).CreateFrom <IrisDataWithStringLabel>(useHeader: false, separator: ',')); pipeline.Add(new Dictionarizer("Label")); // "IrisPlantType" is used as "Label" because of column attribute name on the field. pipeline.Add(new ColumnConcatenator(outputColumn: "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")); pipeline.Add(new StochasticDualCoordinateAscentClassifier()); PredictionModel <IrisDataWithStringLabel, IrisPrediction> model = pipeline.Train <IrisDataWithStringLabel, IrisPrediction>(); string[] scoreLabels; model.TryGetScoreLabelNames(out scoreLabels); 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() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }); Assert.Equal(1, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(0, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisDataWithStringLabel() { SepalLength = 3.1f, SepalWidth = 5.5f, PetalLength = 2.2f, PetalWidth = 6.4f, }); Assert.Equal(0, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(1, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisDataWithStringLabel() { SepalLength = 3.1f, SepalWidth = 2.5f, PetalLength = 1.2f, PetalWidth = 4.4f, }); Assert.Equal(.2, prediction.PredictedLabels[0], 1); Assert.Equal(.8, prediction.PredictedLabels[1], 1); Assert.Equal(0, prediction.PredictedLabels[2], 2); // Note: Testing against the same data set as a simple way to test evaluation. // This isn't appropriate in real-world scenarios. string testDataPath = GetDataPath("iris.data"); var testData = new TextLoader(testDataPath).CreateFrom <IrisDataWithStringLabel>(useHeader: false, separator: ','); var evaluator = new ClassificationEvaluator(); evaluator.OutputTopKAcc = 3; ClassificationMetrics metrics = evaluator.Evaluate(model, testData); Assert.Equal(.98, metrics.AccuracyMacro); Assert.Equal(.98, metrics.AccuracyMicro, 2); Assert.Equal(.06, metrics.LogLoss, 2); Assert.InRange(metrics.LogLossReduction, 94, 96); Assert.Equal(1, metrics.TopKAccuracy); Assert.Equal(3, metrics.PerClassLogLoss.Length); Assert.Equal(0, metrics.PerClassLogLoss[0], 1); Assert.Equal(.1, metrics.PerClassLogLoss[1], 1); Assert.Equal(.1, metrics.PerClassLogLoss[2], 1); ConfusionMatrix matrix = metrics.ConfusionMatrix; Assert.Equal(3, matrix.Order); Assert.Equal(3, matrix.ClassNames.Count); Assert.Equal("Iris-setosa", matrix.ClassNames[0]); Assert.Equal("Iris-versicolor", matrix.ClassNames[1]); Assert.Equal("Iris-virginica", matrix.ClassNames[2]); Assert.Equal(50, matrix[0, 0]); Assert.Equal(50, matrix["Iris-setosa", "Iris-setosa"]); Assert.Equal(0, matrix[0, 1]); Assert.Equal(0, matrix["Iris-setosa", "Iris-versicolor"]); Assert.Equal(0, matrix[0, 2]); Assert.Equal(0, matrix["Iris-setosa", "Iris-virginica"]); Assert.Equal(0, matrix[1, 0]); Assert.Equal(0, matrix["Iris-versicolor", "Iris-setosa"]); Assert.Equal(48, matrix[1, 1]); Assert.Equal(48, matrix["Iris-versicolor", "Iris-versicolor"]); Assert.Equal(2, matrix[1, 2]); Assert.Equal(2, matrix["Iris-versicolor", "Iris-virginica"]); Assert.Equal(0, matrix[2, 0]); Assert.Equal(0, matrix["Iris-virginica", "Iris-setosa"]); Assert.Equal(1, matrix[2, 1]); Assert.Equal(1, matrix["Iris-virginica", "Iris-versicolor"]); Assert.Equal(49, matrix[2, 2]); Assert.Equal(49, matrix["Iris-virginica", "Iris-virginica"]); }