Projekt

Obecné

Profil

Stáhnout (1.85 KB) Statistiky
| Větev: | Tag: | Revize:
1 abfd9c7c Roman Kalivoda
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using Microsoft.ML;
7 9fc5fa93 Roman Kalivoda
using Microsoft.ML.Data;
8
using ServerApp.Parser.OutputInfo;
9 abfd9c7c Roman Kalivoda
10
namespace ServerApp.Predictor
11
{
12
    class NaiveBayesClassifier : IPredictor
13
    {
14
        private MLContext mlContext;
15
16 9fc5fa93 Roman Kalivoda
        private ITransformer model;
17
18 abfd9c7c Roman Kalivoda
        public NaiveBayesClassifier()
19
        {
20
            mlContext = new MLContext();
21
22
        }
23
24 9fc5fa93 Roman Kalivoda
        public IEnumerable<ModelInput> ExtractModelInput(List<WeatherInfo> weatherInfos, List<ActivityInfo> activityInfos)
25
        {
26
            return weatherInfos.Select(e => new ModelInput(){
27 66c3e0df Roman Kalivoda
                Temp = (float)e.temp,
28
                Label = e.temp > 15.0 ? "Full" : "Empty",
29 9fc5fa93 Roman Kalivoda
            }).ToList();
30
        }
31
32
        public void Fit(IEnumerable<ModelInput> trainingData)
33 abfd9c7c Roman Kalivoda
        {
34 9fc5fa93 Roman Kalivoda
            IDataView trainingDataView = mlContext.Data.LoadFromEnumerable(trainingData);
35 66c3e0df Roman Kalivoda
            var dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey(nameof(ModelInput.Label))
36
                .Append(mlContext.Transforms.Concatenate("Features", new[] { "temp" })
37
                .Append(mlContext.Transforms.NormalizeMinMax("Features", "Features")));
38 9fc5fa93 Roman Kalivoda
            var trainer = mlContext.MulticlassClassification.Trainers.NaiveBayes();
39 66c3e0df Roman Kalivoda
            var traininingPipeline = dataProcessPipeline.Append(trainer)
40
                .Append(mlContext.Transforms.Conversion.MapKeyToValue("prediction", "PredictedLabel"));
41 9fc5fa93 Roman Kalivoda
42
            this.model = traininingPipeline.Fit(trainingDataView);
43
44 abfd9c7c Roman Kalivoda
        }
45
46 66c3e0df Roman Kalivoda
        public String[] Predict(IEnumerable<ModelInput> input)
47 abfd9c7c Roman Kalivoda
        {
48 9fc5fa93 Roman Kalivoda
            var data = mlContext.Data.LoadFromEnumerable(input);
49 66c3e0df Roman Kalivoda
            IDataView result = model.Transform(data);
50
            String[] prediction = result.GetColumn<String>("prediction").ToArray();
51
52
            return prediction;
53 abfd9c7c Roman Kalivoda
        }
54
    }
55
}