Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 4977ce53

Přidáno uživatelem Roman Kalivoda před téměř 4 roky(ů)

Re #8832 Refactoring

Refactored existing classes to comply with the coding conventions

Zobrazit rozdíly:

.gitignore
1
################################################################################
2
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
3
################################################################################
4

  
5
/Server/ServerApp/data/auto
Server/ServerApp/Predictor/IPredictor.cs
1
using System;
1
//
2
// Author: Roman Kalivoda
3
//
4

  
5
using System;
2 6
using System.Collections.Generic;
3 7
using System.Linq;
4 8
using System.Text;
......
6 10
using Microsoft.ML;
7 11
using Microsoft.ML.Data;
8 12

  
13

  
9 14
namespace ServerApp.Predictor
10 15
{
16
    /// <summary>
17
    /// A predictor interface.
18
    /// </summary>
11 19
    interface IPredictor
12 20
    {
13

  
21
        /// <summary>
22
        /// Trains the predictor with the given training data input.
23
        /// </summary>
24
        /// <param name="trainInput">A collection of <c>ModelInput</c> instances. The objects contain both feature vector inputs and corresponding labels.</param>
14 25
        void Fit(IEnumerable<ModelInput> trainInput);
15 26

  
16
        String[] Predict(IEnumerable<ModelInput> input);
27
        /// <summary>
28
        /// Predicts classes to the given feature vectors.
29
        /// </summary>
30
        /// <param name="input">A collection of model feature vectors in <c>ModelInput</c> instances.</param>
31
        /// <returns>A collection of <c>PredictionResult</c> instances.</returns>
32
        IDataView Predict(IEnumerable<ModelInput> input);
17 33
    }
18 34
}
Server/ServerApp/Predictor/ModelInput.cs
1
using Microsoft.ML.Data;
1
//
2
// Author: Roman Kalivoda
3
//
4

  
5
using Microsoft.ML.Data;
2 6

  
3 7
namespace ServerApp.Predictor
4 8
{
9

  
10
    /// <summary>
11
    /// A container of input features and label for the ML.NET classification model.
12
    /// </summary>
5 13
    public class ModelInput
6 14
    {
15
        /// <summary>
16
        /// A label of this training input.
17
        /// </summary>
7 18
        [ColumnName("Label"), LoadColumn(0)]
8 19
        public string Label { get; set; }
9 20

  
10

  
21
        /// <summary>
22
        /// Temperature at the site.
23
        /// </summary>
11 24
        [ColumnName("temp"), LoadColumn(1)]
12 25
        public float Temp { get; set; }
13 26

  
......
17 30
        //[ColumnName("time"), LoadColumn(3)]
18 31
        //public string Col3 { get; set; }
19 32
    }
20
}
33
}
Server/ServerApp/Predictor/ModelOutput.cs
1
using System;
1
//
2
// Author: Roman Kalivoda
3
//
4

  
5
using System;
2 6
using Microsoft.ML.Data;
3 7

  
4 8
namespace ServerApp.Predictor
5 9
{
10
    /// <summary>
11
    /// An output class of the ML.NET classifier model.
12
    /// </summary>
6 13
    public class ModelOutput
7 14
    {
15
        /// <summary>
16
        /// A predicted class.
17
        /// </summary>
8 18
        [ColumnName("prediction")]
9 19
        public String Prediction { get; set; }
20

  
21
        /// <summary>
22
        /// The score of prediction probability into individual classes.
23
        /// </summary>
10 24
        public float[] Score { get; set; }
11 25

  
12 26
    }
Server/ServerApp/Predictor/NaiveBayesClassifier.cs
1
using System;
1
//
2
// Author: Roman Kalivoda
3
//
4

  
5
using System;
2 6
using System.Collections.Generic;
3 7
using System.Linq;
4 8
using System.Text;
......
9 13

  
10 14
namespace ServerApp.Predictor
11 15
{
16
    /// <summary>
17
    /// Implementation of the naive Bayes classifier in ML.NET.
18
    /// </summary>
12 19
    class NaiveBayesClassifier : IPredictor
13 20
    {
21
        /// <summary>
22
        /// Context of the ML.NET framework.
23
        /// </summary>
14 24
        private MLContext mlContext;
15 25

  
26
        /// <summary>
27
        /// Model instance
28
        /// </summary>
16 29
        private ITransformer model;
17 30

  
31
        /// <summary>
32
        /// Instantiates new <c>MLContext</c>.
33
        /// </summary>
18 34
        public NaiveBayesClassifier()
19 35
        {
20 36
            mlContext = new MLContext();
21 37

  
22 38
        }
23 39

  
40
        /// <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>
24 46
        public IEnumerable<ModelInput> ExtractModelInput(List<WeatherInfo> weatherInfos, List<ActivityInfo> activityInfos)
25 47
        {
26 48
            return weatherInfos.Select(e => new ModelInput(){
......
29 51
            }).ToList();
30 52
        }
31 53

  
32
        public void Fit(IEnumerable<ModelInput> trainingData)
54
        public void Fit(IEnumerable<ModelInput> trainInput)
33 55
        {
34
            IDataView trainingDataView = mlContext.Data.LoadFromEnumerable(trainingData);
56
            IDataView trainingDataView = mlContext.Data.LoadFromEnumerable(trainInput);
35 57
            var dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey(nameof(ModelInput.Label))
36 58
                .Append(mlContext.Transforms.Concatenate("Features", new[] { "temp" })
37 59
                .Append(mlContext.Transforms.NormalizeMinMax("Features", "Features")));
......
43 65

  
44 66
        }
45 67

  
46
        public String[] Predict(IEnumerable<ModelInput> input)
68
        public IDataView Predict(IEnumerable<ModelInput> input)
47 69
        {
48 70
            var data = mlContext.Data.LoadFromEnumerable(input);
49 71
            IDataView result = model.Transform(data);
50
            String[] prediction = result.GetColumn<String>("prediction").ToArray();
51

  
52
            return prediction;
72
            return result;
53 73
        }
54 74
    }
55 75
}

Také k dispozici: Unified diff