1 |
4977ce53
|
Roman Kalivoda
|
//
|
2 |
|
|
// Author: Roman Kalivoda
|
3 |
|
|
//
|
4 |
|
|
|
5 |
d358b79e
|
Roman Kalivoda
|
using System;
|
6 |
abfd9c7c
|
Roman Kalivoda
|
using System.Collections.Generic;
|
7 |
|
|
using System.Linq;
|
8 |
|
|
using Microsoft.ML;
|
9 |
|
|
|
10 |
|
|
namespace ServerApp.Predictor
|
11 |
|
|
{
|
12 |
4977ce53
|
Roman Kalivoda
|
/// <summary>
|
13 |
|
|
/// Implementation of the naive Bayes classifier in ML.NET.
|
14 |
|
|
/// </summary>
|
15 |
abfd9c7c
|
Roman Kalivoda
|
class NaiveBayesClassifier : IPredictor
|
16 |
|
|
{
|
17 |
4977ce53
|
Roman Kalivoda
|
/// <summary>
|
18 |
|
|
/// Context of the ML.NET framework.
|
19 |
|
|
/// </summary>
|
20 |
d358b79e
|
Roman Kalivoda
|
private MLContext _mlContext;
|
21 |
abfd9c7c
|
Roman Kalivoda
|
|
22 |
4977ce53
|
Roman Kalivoda
|
/// <summary>
|
23 |
|
|
/// Model instance
|
24 |
|
|
/// </summary>
|
25 |
d358b79e
|
Roman Kalivoda
|
private ITransformer _trainedModel;
|
26 |
|
|
|
27 |
|
|
private PredictionEngine<ModelInput, ModelOutput> _predictionEngine;
|
28 |
|
|
|
29 |
|
|
IDataView _trainingDataView;
|
30 |
9fc5fa93
|
Roman Kalivoda
|
|
31 |
4977ce53
|
Roman Kalivoda
|
/// <summary>
|
32 |
|
|
/// Instantiates new <c>MLContext</c>.
|
33 |
|
|
/// </summary>
|
34 |
abfd9c7c
|
Roman Kalivoda
|
public NaiveBayesClassifier()
|
35 |
|
|
{
|
36 |
d358b79e
|
Roman Kalivoda
|
_mlContext = new MLContext();
|
37 |
9fc5fa93
|
Roman Kalivoda
|
}
|
38 |
|
|
|
39 |
4977ce53
|
Roman Kalivoda
|
public void Fit(IEnumerable<ModelInput> trainInput)
|
40 |
abfd9c7c
|
Roman Kalivoda
|
{
|
41 |
d358b79e
|
Roman Kalivoda
|
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 |
662b2404
|
Roman Kalivoda
|
|
52 |
d358b79e
|
Roman Kalivoda
|
}
|
53 |
9fc5fa93
|
Roman Kalivoda
|
|
54 |
d358b79e
|
Roman Kalivoda
|
public string Predict(ModelInput input)
|
55 |
|
|
{
|
56 |
|
|
return this._predictionEngine.Predict(input).Prediction;
|
57 |
abfd9c7c
|
Roman Kalivoda
|
}
|
58 |
|
|
|
59 |
d358b79e
|
Roman Kalivoda
|
public void Evaluate(IEnumerable<ModelInput> modelInputs)
|
60 |
abfd9c7c
|
Roman Kalivoda
|
{
|
61 |
d358b79e
|
Roman Kalivoda
|
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 |
abfd9c7c
|
Roman Kalivoda
|
}
|
73 |
|
|
}
|
74 |
|
|
}
|