1 |
4977ce53
|
Roman Kalivoda
|
//
|
2 |
|
|
// Author: Roman Kalivoda
|
3 |
|
|
//
|
4 |
|
|
|
5 |
|
|
using System;
|
6 |
abfd9c7c
|
Roman Kalivoda
|
using System.Collections.Generic;
|
7 |
|
|
using System.Linq;
|
8 |
|
|
using System.Text;
|
9 |
|
|
using System.Threading.Tasks;
|
10 |
|
|
using Microsoft.ML;
|
11 |
9fc5fa93
|
Roman Kalivoda
|
using Microsoft.ML.Data;
|
12 |
|
|
using ServerApp.Parser.OutputInfo;
|
13 |
abfd9c7c
|
Roman Kalivoda
|
|
14 |
|
|
namespace ServerApp.Predictor
|
15 |
|
|
{
|
16 |
4977ce53
|
Roman Kalivoda
|
/// <summary>
|
17 |
|
|
/// Implementation of the naive Bayes classifier in ML.NET.
|
18 |
|
|
/// </summary>
|
19 |
abfd9c7c
|
Roman Kalivoda
|
class NaiveBayesClassifier : IPredictor
|
20 |
|
|
{
|
21 |
4977ce53
|
Roman Kalivoda
|
/// <summary>
|
22 |
|
|
/// Context of the ML.NET framework.
|
23 |
|
|
/// </summary>
|
24 |
abfd9c7c
|
Roman Kalivoda
|
private MLContext mlContext;
|
25 |
|
|
|
26 |
4977ce53
|
Roman Kalivoda
|
/// <summary>
|
27 |
|
|
/// Model instance
|
28 |
|
|
/// </summary>
|
29 |
9fc5fa93
|
Roman Kalivoda
|
private ITransformer model;
|
30 |
|
|
|
31 |
4977ce53
|
Roman Kalivoda
|
/// <summary>
|
32 |
|
|
/// Instantiates new <c>MLContext</c>.
|
33 |
|
|
/// </summary>
|
34 |
abfd9c7c
|
Roman Kalivoda
|
public NaiveBayesClassifier()
|
35 |
|
|
{
|
36 |
|
|
mlContext = new MLContext();
|
37 |
|
|
|
38 |
|
|
}
|
39 |
|
|
|
40 |
4977ce53
|
Roman Kalivoda
|
/// <summary>
|
41 |
|
|
/// Extracts list of feature vectors from parsed info objects.
|
42 |
|
|
/// </summary>
|
43 |
|
|
/// <param name="weatherInfos">List of weather info objects.</param>
|
44 |
|
|
/// <param name="activityInfos">List of info objects about activities at the site.</param>
|
45 |
|
|
/// <returns>A list of feature vectors for model training.</returns>
|
46 |
9fc5fa93
|
Roman Kalivoda
|
public IEnumerable<ModelInput> ExtractModelInput(List<WeatherInfo> weatherInfos, List<ActivityInfo> activityInfos)
|
47 |
|
|
{
|
48 |
|
|
return weatherInfos.Select(e => new ModelInput(){
|
49 |
66c3e0df
|
Roman Kalivoda
|
Temp = (float)e.temp,
|
50 |
|
|
Label = e.temp > 15.0 ? "Full" : "Empty",
|
51 |
9fc5fa93
|
Roman Kalivoda
|
}).ToList();
|
52 |
|
|
}
|
53 |
|
|
|
54 |
4977ce53
|
Roman Kalivoda
|
public void Fit(IEnumerable<ModelInput> trainInput)
|
55 |
abfd9c7c
|
Roman Kalivoda
|
{
|
56 |
4977ce53
|
Roman Kalivoda
|
IDataView trainingDataView = mlContext.Data.LoadFromEnumerable(trainInput);
|
57 |
66c3e0df
|
Roman Kalivoda
|
var dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey(nameof(ModelInput.Label))
|
58 |
|
|
.Append(mlContext.Transforms.Concatenate("Features", new[] { "temp" })
|
59 |
|
|
.Append(mlContext.Transforms.NormalizeMinMax("Features", "Features")));
|
60 |
9fc5fa93
|
Roman Kalivoda
|
var trainer = mlContext.MulticlassClassification.Trainers.NaiveBayes();
|
61 |
66c3e0df
|
Roman Kalivoda
|
var traininingPipeline = dataProcessPipeline.Append(trainer)
|
62 |
|
|
.Append(mlContext.Transforms.Conversion.MapKeyToValue("prediction", "PredictedLabel"));
|
63 |
9fc5fa93
|
Roman Kalivoda
|
|
64 |
|
|
this.model = traininingPipeline.Fit(trainingDataView);
|
65 |
|
|
|
66 |
abfd9c7c
|
Roman Kalivoda
|
}
|
67 |
|
|
|
68 |
4977ce53
|
Roman Kalivoda
|
public IDataView Predict(IEnumerable<ModelInput> input)
|
69 |
abfd9c7c
|
Roman Kalivoda
|
{
|
70 |
9fc5fa93
|
Roman Kalivoda
|
var data = mlContext.Data.LoadFromEnumerable(input);
|
71 |
66c3e0df
|
Roman Kalivoda
|
IDataView result = model.Transform(data);
|
72 |
4977ce53
|
Roman Kalivoda
|
return result;
|
73 |
abfd9c7c
|
Roman Kalivoda
|
}
|
74 |
|
|
}
|
75 |
|
|
}
|