Revize 662b2404
Přidáno uživatelem Roman Kalivoda před téměř 4 roky(ů)
Server/ServerApp/Predictor/FeatureExtractor.cs | ||
---|---|---|
52 | 52 |
} |
53 | 53 |
|
54 | 54 |
var res = new List<ModelInput>(); |
55 |
// TODO use iterators to access records about attendance and weather together |
|
56 |
foreach (Parser.OutputInfo.ActivityInfo val in dataParser.AttendanceList) { |
|
57 |
if (buildings.Contains(val.building)) { |
|
55 |
// use enumerator to access records about attendance and weather together |
|
56 |
IEnumerator<Parser.OutputInfo.WeatherInfo> weatherEnumerator = dataParser.WeatherList.GetEnumerator(); |
|
57 |
weatherEnumerator.MoveNext(); |
|
58 |
foreach (Parser.OutputInfo.ActivityInfo val in dataParser.AttendanceList) |
|
59 |
{ |
|
60 |
if (!weatherEnumerator.Current.startTime.Equals(val.startTime)) |
|
61 |
{ |
|
62 |
weatherEnumerator.MoveNext(); |
|
63 |
} |
|
64 |
if (buildings.Contains(val.building)) |
|
65 |
{ |
|
66 |
Parser.OutputInfo.WeatherInfo weatherInfo = weatherEnumerator.Current; |
|
58 | 67 |
res.Add(new ModelInput |
59 | 68 |
{ |
60 |
Temp = 0,
|
|
69 |
Temp = (float)weatherInfo.temp,
|
|
61 | 70 |
Time = val.startTime, |
71 |
Wind = (float)weatherInfo.wind, |
|
72 |
Rain = (float)weatherInfo.rain, |
|
62 | 73 |
// TODO convert val.amount to label |
63 | 74 |
}); |
64 | 75 |
} |
Server/ServerApp/Predictor/IPredictionController.cs | ||
---|---|---|
17 | 17 |
{ |
18 | 18 |
/// <summary> |
19 | 19 |
/// </summary> |
20 |
/// <returns>A dictionary with all existing predictors.</returns>
|
|
20 |
/// <returns>A list with all existing predictors.</returns>
|
|
21 | 21 |
List<string> GetPredictors(); |
22 | 22 |
|
23 | 23 |
/// <summary> |
Server/ServerApp/Predictor/IPredictor.cs | ||
---|---|---|
30 | 30 |
/// <param name="input">A collection of model feature vectors in <c>ModelInput</c> instances.</param> |
31 | 31 |
/// <returns>A collection of <c>PredictionResult</c> instances.</returns> |
32 | 32 |
IDataView Predict(IEnumerable<ModelInput> input); |
33 |
|
|
34 |
// TODO define Save method |
|
33 | 35 |
} |
34 | 36 |
} |
Server/ServerApp/Predictor/ModelInput.cs | ||
---|---|---|
30 | 30 |
/// </summary> |
31 | 31 |
[ColumnName("Time"), LoadColumn(2)] |
32 | 32 |
public DateTime Time { get; set; } |
33 |
|
|
34 |
/// <summary> |
|
35 |
/// Wind velocity in ? units |
|
36 |
/// </summary> |
|
37 |
[ColumnName("Wind"), LoadColumn(3)] |
|
38 |
public float Wind { get; set; } |
|
39 |
|
|
40 |
/// <summary> |
|
41 |
/// Precipitation |
|
42 |
/// </summary> |
|
43 |
[ColumnName("Rain"), LoadColumn(4)] |
|
44 |
public float Rain { get; set; } |
|
33 | 45 |
} |
34 | 46 |
} |
Server/ServerApp/Predictor/ModelOutput.cs | ||
---|---|---|
15 | 15 |
/// <summary> |
16 | 16 |
/// A predicted class. |
17 | 17 |
/// </summary> |
18 |
[ColumnName("prediction")]
|
|
18 |
[ColumnName("PredictedLabel")]
|
|
19 | 19 |
public String Prediction { get; set; } |
20 | 20 |
|
21 |
/// <summary> |
|
22 |
/// The score of prediction probability into individual classes. |
|
23 |
/// </summary> |
|
24 |
public float[] Score { get; set; } |
|
25 |
|
|
26 | 21 |
} |
27 | 22 |
} |
Server/ServerApp/Predictor/NaiveBayesClassifier.cs | ||
---|---|---|
35 | 35 |
public void Fit(IEnumerable<ModelInput> trainInput) |
36 | 36 |
{ |
37 | 37 |
IDataView trainingDataView = mlContext.Data.LoadFromEnumerable(trainInput); |
38 |
var dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey(nameof(ModelInput.Label)) |
|
39 |
.Append(mlContext.Transforms.Concatenate("Features", new[] { "temp" }) |
|
40 |
.Append(mlContext.Transforms.NormalizeMinMax("Features", "Features"))); |
|
41 |
var trainer = mlContext.MulticlassClassification.Trainers.NaiveBayes(); |
|
42 |
var traininingPipeline = dataProcessPipeline.Append(trainer) |
|
43 |
.Append(mlContext.Transforms.Conversion.MapKeyToValue("prediction", "PredictedLabel")); |
|
44 |
|
|
45 |
this.model = traininingPipeline.Fit(trainingDataView); |
|
38 |
var pipeline = mlContext.Transforms.Conversion.MapValueToKey(nameof(ModelInput.Label)) |
|
39 |
.Append(mlContext.Transforms.Concatenate("Features", new[] { "Temp" })) |
|
40 |
.Append(mlContext.Transforms.NormalizeMinMax("Features", "Features")) |
|
41 |
.Append(mlContext.MulticlassClassification.Trainers.NaiveBayes()); |
|
42 |
|
|
43 |
this.model =pipeline.Fit(trainingDataView); |
|
46 | 44 |
|
47 | 45 |
} |
48 | 46 |
|
Server/ServerApp/Predictor/PredictionController.cs | ||
---|---|---|
92 | 92 |
// TODO A single predictor is used for all areas, so training is done only once now. |
93 | 93 |
for (int i = 0; i < this.predictors.Count; i++) |
94 | 94 |
{ |
95 |
// TODO change datetimes when parser interface is ready to parse only downloaded data. |
|
96 |
//IEnumerable<ModelInput> data = featureExtractor.PrepareModelInput(i, DateTime.MinValue, DateTime.MaxValue); |
|
97 |
IEnumerable<ModelInput> data = featureExtractor.PrepareModelInput(i, new DateTime(2019, 10, 5), new DateTime(2020, 6, 30)); |
|
95 |
// train on all available data |
|
96 |
IEnumerable<ModelInput> data = featureExtractor.PrepareModelInput(i, DateTime.MinValue, DateTime.MaxValue); |
|
98 | 97 |
this.predictors[i].Fit(data); |
99 | 98 |
} |
100 | 99 |
} else |
Server/ServerApp/Program.cs | ||
---|---|---|
84 | 84 |
|
85 | 85 |
// TODO nastavit čas |
86 | 86 |
IDataParser p = new DataParser(dd); |
87 |
DateTime startT = new DateTime(2019, 10, 5); |
|
88 |
DateTime endT = new DateTime(2020, 6, 30); |
|
89 | 87 |
IPredictionController predictionController = new PredictionController(p); |
90 | 88 |
predictionController.Train(); |
91 | 89 |
//var results = predictionController.Predict() |
Také k dispozici: Unified diff
Re #8832 integration