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.Conversion.ConvertType(nameof(ModelInput.Hour)))
|
44
|
.Append(_mlContext.Transforms.Concatenate("Features",
|
45
|
new[] { nameof(ModelInput.Temp), nameof(ModelInput.Rain), nameof(ModelInput.Wind), nameof(ModelInput.Hour) }))
|
46
|
.Append(_mlContext.Transforms.NormalizeMeanVariance("Features", useCdf:false))
|
47
|
.AppendCacheCheckpoint(_mlContext)
|
48
|
.Append(_mlContext.MulticlassClassification.Trainers.NaiveBayes())
|
49
|
.Append(_mlContext.Transforms.Conversion.MapKeyToValue(nameof(ModelOutput.PredictedLabel)));
|
50
|
|
51
|
var cvResults = _mlContext.MulticlassClassification.CrossValidate(this._trainingDataView, pipeline);
|
52
|
foreach (var result in cvResults)
|
53
|
{
|
54
|
var testMetrics = result.Metrics;
|
55
|
Console.WriteLine($"*************************************************************************************************************");
|
56
|
Console.WriteLine($"* Metrics for Multi-class Classification model - Model #{result.Fold} ");
|
57
|
Console.WriteLine($"*------------------------------------------------------------------------------------------------------------");
|
58
|
Console.WriteLine($"* MicroAccuracy: {testMetrics.MicroAccuracy:0.###}");
|
59
|
Console.WriteLine($"* MacroAccuracy: {testMetrics.MacroAccuracy:0.###}");
|
60
|
Console.WriteLine($"* LogLoss: {testMetrics.LogLoss:#.###}");
|
61
|
Console.WriteLine($"* LogLossReduction: {testMetrics.LogLossReduction:#.###}");
|
62
|
Console.WriteLine($"* Confusion Matrix: {testMetrics.ConfusionMatrix.GetFormattedConfusionTable()}");
|
63
|
Console.WriteLine($"*************************************************************************************************************");
|
64
|
}
|
65
|
this._trainedModel = cvResults.OrderByDescending(fold => fold.Metrics.MicroAccuracy).Select(fold => fold.Model).First();
|
66
|
Console.WriteLine($"Selected the model #{cvResults.OrderByDescending(fold => fold.Metrics.MicroAccuracy).Select(fold => fold.Fold).First()} as the best.");
|
67
|
this._predictionEngine = _mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(this._trainedModel);
|
68
|
|
69
|
}
|
70
|
|
71
|
public string Predict(ModelInput input)
|
72
|
{
|
73
|
return this._predictionEngine.Predict(input).PredictedLabel;
|
74
|
}
|
75
|
|
76
|
public void Evaluate(IEnumerable<ModelInput> modelInputs)
|
77
|
{
|
78
|
var testDataView = this._mlContext.Data.LoadFromEnumerable(modelInputs);
|
79
|
var data = _trainedModel.Transform(testDataView);
|
80
|
var testMetrics = _mlContext.MulticlassClassification.Evaluate(data);
|
81
|
|
82
|
Console.WriteLine($"*************************************************************************************************************");
|
83
|
Console.WriteLine($"* Metrics for Multi-class Classification model - Test Data ");
|
84
|
Console.WriteLine($"*------------------------------------------------------------------------------------------------------------");
|
85
|
Console.WriteLine($"* MicroAccuracy: {testMetrics.MicroAccuracy:0.###}");
|
86
|
Console.WriteLine($"* MacroAccuracy: {testMetrics.MacroAccuracy:0.###}");
|
87
|
Console.WriteLine($"* LogLoss: {testMetrics.LogLoss:#.###}");
|
88
|
Console.WriteLine($"* LogLossReduction: {testMetrics.LogLossReduction:#.###}");
|
89
|
Console.WriteLine($"* Confusion Matrix: {testMetrics.ConfusionMatrix.GetFormattedConfusionTable()}");
|
90
|
Console.WriteLine($"*************************************************************************************************************");
|
91
|
}
|
92
|
}
|
93
|
}
|