Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 60a60164

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

Re #8955 refactoring

Zobrazit rozdíly:

Server/ServerApp/Connection/XMLProtocolHandler/Request.cs
1 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6 2
using System.Xml.Serialization;
7 3

  
8 4
namespace ServerApp.Connection.XMLProtocolHandler
Server/ServerApp/Predictor/FeatureExtractor.cs
60 60
            {
61 61
                res.Add(new ModelInput
62 62
                {
63
                    Temp = (float)val.temp,
64 63
                    Time = val.startTime,
64
                    Temp = (float)val.temp,
65
                    Hour = val.startTime.Hour,
65 66
                    Wind = (float)val.wind,
66 67
                    Rain = (float)val.rain,
67 68
                });
......
90 91
            if (ratio < 0.1f)
91 92
            {
92 93
                return "10%";
93
            } else if (ratio < 0.2f)
94
            }
95
            else if (ratio < 0.2f)
94 96
            {
95 97
                return "20%";
96
            } else if (ratio < 0.3f)
98
            }
99
            else if (ratio < 0.3f)
97 100
            {
98 101
                return "30%";
99
            } else if (ratio < 0.4f)
102
            }
103
            else if (ratio < 0.4f)
100 104
            {
101 105
                return "40%";
102
            } else if (ratio < 0.5f)
106
            }
107
            else if (ratio < 0.5f)
103 108
            {
104 109
                return "50%";
105
            } else if (ratio < 0.6f)
110
            }
111
            else if (ratio < 0.6f)
106 112
            {
107 113
                return "60%";
108
            } else if(ratio < 0.7f) {
114
            }
115
            else if (ratio < 0.7f)
116
            {
109 117
                return "70%";
110
            } else if (ratio < 0.8f)
118
            }
119
            else if (ratio < 0.8f)
111 120
            {
112 121
                return "80%";
113
            } else if (ratio < 0.9f)
122
            }
123
            else if (ratio < 0.9f)
114 124
            {
115 125
                return "90%";
116
            } else
126
            }
127
            else
117 128
            {
118 129
                return "100%";
119 130
            }
......
121 132

  
122 133
        private double LabelToRatio(string label)
123 134
        {
124
            if (label.Equals("10%")) {
135
            if (label.Equals("10%"))
136
            {
125 137
                return 0.1f;
126 138
            }
127
            else if (label.Equals("20%"))  {
139
            else if (label.Equals("20%"))
140
            {
128 141
                return 0.2f;
129 142
            }
130
            else if (label.Equals("30%")) {
143
            else if (label.Equals("30%"))
144
            {
131 145
                return 0.3f;
132 146
            }
133
            else if (label.Equals("40%")) {
147
            else if (label.Equals("40%"))
148
            {
134 149
                return 0.4f;
135 150
            }
136
            else if (label.Equals("50%")) {
151
            else if (label.Equals("50%"))
152
            {
137 153
                return 0.5f;
138 154
            }
139
            else if (label.Equals("60%")) {
155
            else if (label.Equals("60%"))
156
            {
140 157
                return 0.6f;
141 158
            }
142
            else if (label.Equals("70%")) {
159
            else if (label.Equals("70%"))
160
            {
143 161
                return 0.7f;
144 162
            }
145
            else if (label.Equals("80%")) {
163
            else if (label.Equals("80%"))
164
            {
146 165
                return 0.8f;
147 166
            }
148
            else if (label.Equals("90%")) {
167
            else if (label.Equals("90%"))
168
            {
149 169
                return 0.9f;
150 170
            }
151 171
            else
Server/ServerApp/Predictor/IPredictionController.cs
2 2
// Author: Roman Kalivoda
3 3
//
4 4

  
5
using System;
6 5
using System.Collections.Generic;
7
using System.IO;
8
using Microsoft.ML;
9 6
using ServerApp.Connection.XMLProtocolHandler;
10
using ServerApp.Parser.OutputInfo;
11 7

  
12 8
namespace ServerApp.Predictor
13 9
{
......
32 28
        /// </summary>
33 29
        /// <param name="locationKey">A string identifier of the location for which to load a predictor.</param>
34 30
        /// <param name="path">A path to folder with trained prediction model.</param>
35
        void Load(string locationKey = null,string path = null);
31
        void Load(string locationKey = null, string path = null);
36 32

  
37 33
        /// <summary>
38 34
        /// Predicts turnout level at given time supposing given weather conditions.
Server/ServerApp/Predictor/ModelInput.cs
13 13
    /// </summary>
14 14
    public class ModelInput
15 15
    {
16
        /// <summary>
17
        /// Start time of the information.
18
        /// </summary>
19
        public DateTime Time { get; set; }
20

  
16 21
        /// <summary>
17 22
        /// A label of this training input.
18 23
        /// </summary>
......
26 31
        public float Temp { get; set; }
27 32

  
28 33
        /// <summary>
29
        /// Time of the predicted turnout.
34
        /// Hour of the predicted turnout.
30 35
        /// </summary>
31
        [ColumnName("Time"), LoadColumn(2)]
32
        public DateTime Time { get; set; }
36
        [ColumnName("Hour"), LoadColumn(2)]
37
        public int Hour { get; set; }
33 38

  
34 39
        /// <summary>
35
        /// Wind velocity in ? units
40
        /// Wind velocity in m/s
36 41
        /// </summary>
37 42
        [ColumnName("Wind"), LoadColumn(3)]
38 43
        public float Wind { get; set; }
39 44

  
40 45
        /// <summary>
41
        /// Precipitation
46
        /// Precipitation in %
42 47
        /// </summary>
43 48
        [ColumnName("Rain"), LoadColumn(4)]
44 49
        public float Rain { get; set; }
Server/ServerApp/Predictor/ModelOutput.cs
2 2
// Author: Roman Kalivoda
3 3
//
4 4

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

  
8 7
namespace ServerApp.Predictor
......
16 15
        /// A predicted class.
17 16
        /// </summary>
18 17
        [ColumnName("PredictedLabel")]
19
        public String Prediction { get; set; }
18
        public string PredictedLabel { get; set; }
20 19

  
21 20
    }
22 21
}
Server/ServerApp/Predictor/NaiveBayesClassifier.cs
40 40
        {
41 41
            this._trainingDataView = _mlContext.Data.LoadFromEnumerable(trainInput);
42 42
            var pipeline = _mlContext.Transforms.Conversion.MapValueToKey(nameof(ModelInput.Label))
43
                .Append(_mlContext.Transforms.Concatenate("Features", new[] { "Temp" }))
43
                .Append(_mlContext.Transforms.Concatenate("Features", new[] { nameof(ModelInput.Temp), nameof(ModelInput.Rain), nameof(ModelInput.Wind) }))
44 44
                .Append(_mlContext.Transforms.NormalizeMinMax("Features", "Features"))
45 45
                .AppendCacheCheckpoint(_mlContext)
46 46
                .Append(_mlContext.MulticlassClassification.Trainers.NaiveBayes())
47
                .Append(_mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel")); ;
47
                .Append(_mlContext.Transforms.Conversion.MapKeyToValue(nameof(ModelOutput.PredictedLabel)));
48 48

  
49 49
            this._trainedModel = pipeline.Fit(this._trainingDataView);
50 50
            this._predictionEngine = _mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(this._trainedModel);
......
53 53

  
54 54
        public string Predict(ModelInput input)
55 55
        {
56
            return this._predictionEngine.Predict(input).Prediction;
56
            return this._predictionEngine.Predict(input).PredictedLabel;
57 57
        }
58 58

  
59 59
        public void Evaluate(IEnumerable<ModelInput> modelInputs)
60 60
        {
61 61
            var testDataView = this._mlContext.Data.LoadFromEnumerable(modelInputs);
62
            var testMetrics = _mlContext.MulticlassClassification.Evaluate(_trainedModel.Transform(testDataView));
62
            var data = _trainedModel.Transform(testDataView);
63
            var testMetrics = _mlContext.MulticlassClassification.Evaluate(data);
63 64

  
64 65
            Console.WriteLine($"*************************************************************************************************************");
65 66
            Console.WriteLine($"*       Metrics for Multi-class Classification model - Test Data     ");
Server/ServerApp/Predictor/PredictionController.cs
4 4

  
5 5
using System;
6 6
using System.Collections.Generic;
7
using System.IO;
8
using Microsoft.ML;
9 7
using ServerApp.Connection.XMLProtocolHandler;
10
using ServerApp.Parser.OutputInfo;
11 8
using ServerApp.Parser.Parsers;
12 9

  
13 10
namespace ServerApp.Predictor
......
43 40
            this.dataParser = dataParser;
44 41
            this.predictors = new List<IPredictor>();
45 42
            this.buildingsToAreas = new Dictionary<string, int>();
46
            this.featureExtractor = new FeatureExtractor(dataParser, buildingsToAreas);
43
            this.featureExtractor = new FeatureExtractor(this.dataParser, buildingsToAreas);
47 44

  
48 45
            // fill predictors with all available locationKeys
49 46
            // TODO Currently all locations use the same predictor. Try dividing locations into subareas with separate predictors.
......
97 94
                    Console.WriteLine("Evaluating predictor with {0} samples.", testData.Count);
98 95
                    this.predictors[i].Evaluate(testData);
99 96
                }
100
            } else
97
            }
98
            else
101 99
            // train specified predictor only
102 100
            {
103 101
                throw new NotImplementedException();

Také k dispozici: Unified diff