1
|
//
|
2
|
// Author: Roman Kalivoda
|
3
|
//
|
4
|
|
5
|
using System;
|
6
|
using System.Collections.Generic;
|
7
|
using System.Linq;
|
8
|
using Microsoft.ML;
|
9
|
|
10
|
namespace ServerApp.Predictor
|
11
|
{
|
12
|
/// <summary>
|
13
|
/// Implementation of the naive Bayes classifier in ML.NET.
|
14
|
/// </summary>
|
15
|
class NaiveBayesClassifier : IPredictor
|
16
|
{
|
17
|
/// <summary>
|
18
|
/// Context of the ML.NET framework.
|
19
|
/// </summary>
|
20
|
private MLContext _mlContext;
|
21
|
|
22
|
/// <summary>
|
23
|
/// Model instance
|
24
|
/// </summary>
|
25
|
private ITransformer _trainedModel;
|
26
|
|
27
|
private PredictionEngine<ModelInput, ModelOutput> _predictionEngine;
|
28
|
|
29
|
IDataView _trainingDataView;
|
30
|
|
31
|
/// <summary>
|
32
|
/// Instantiates new <c>MLContext</c>.
|
33
|
/// </summary>
|
34
|
public NaiveBayesClassifier()
|
35
|
{
|
36
|
_mlContext = new MLContext();
|
37
|
}
|
38
|
|
39
|
public void Fit(IEnumerable<ModelInput> trainInput)
|
40
|
{
|
41
|
this._trainingDataView = _mlContext.Data.LoadFromEnumerable(trainInput);
|
42
|
var pipeline = _mlContext.Transforms.Conversion.MapValueToKey(nameof(ModelInput.Label))
|
43
|
.Append(_mlContext.Transforms.Concatenate("Features", new[] { "Temp" }))
|
44
|
.Append(_mlContext.Transforms.NormalizeMinMax("Features", "Features"))
|
45
|
.AppendCacheCheckpoint(_mlContext)
|
46
|
.Append(_mlContext.MulticlassClassification.Trainers.NaiveBayes())
|
47
|
.Append(_mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel")); ;
|
48
|
|
49
|
this._trainedModel = pipeline.Fit(this._trainingDataView);
|
50
|
this._predictionEngine = _mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(this._trainedModel);
|
51
|
|
52
|
}
|
53
|
|
54
|
public string Predict(ModelInput input)
|
55
|
{
|
56
|
return this._predictionEngine.Predict(input).Prediction;
|
57
|
}
|
58
|
|
59
|
public void Evaluate(IEnumerable<ModelInput> modelInputs)
|
60
|
{
|
61
|
var testDataView = this._mlContext.Data.LoadFromEnumerable(modelInputs);
|
62
|
var testMetrics = _mlContext.MulticlassClassification.Evaluate(_trainedModel.Transform(testDataView));
|
63
|
|
64
|
Console.WriteLine($"*************************************************************************************************************");
|
65
|
Console.WriteLine($"* Metrics for Multi-class Classification model - Test Data ");
|
66
|
Console.WriteLine($"*------------------------------------------------------------------------------------------------------------");
|
67
|
Console.WriteLine($"* MicroAccuracy: {testMetrics.MicroAccuracy:0.###}");
|
68
|
Console.WriteLine($"* MacroAccuracy: {testMetrics.MacroAccuracy:0.###}");
|
69
|
Console.WriteLine($"* LogLoss: {testMetrics.LogLoss:#.###}");
|
70
|
Console.WriteLine($"* LogLossReduction: {testMetrics.LogLossReduction:#.###}");
|
71
|
Console.WriteLine($"*************************************************************************************************************");
|
72
|
}
|
73
|
}
|
74
|
}
|