aswi2021tri-musketyri-gitlab/Server/ServerApp/Predictor/FeatureExtractor.cs @ 60a60164
1 |
// |
---|---|
2 |
// Author: Roman Kalivoda
|
3 |
//
|
4 |
|
5 |
using System.Collections.Generic; |
6 |
using System; |
7 |
using ServerApp.Parser.Parsers; |
8 |
using ServerApp.Parser.OutputInfo; |
9 |
using System.Linq; |
10 |
|
11 |
namespace ServerApp.Predictor |
12 |
{
|
13 |
/// <summary>
|
14 |
/// A class responsible for preparation of features for classifiers.
|
15 |
/// </summary>
|
16 |
public class FeatureExtractor |
17 |
{
|
18 |
/// <summary>
|
19 |
/// A DataParser instance used to access info objects.
|
20 |
/// </summary>
|
21 |
private readonly IDataParser dataParser; |
22 |
|
23 |
private Dictionary<string, int> buildingsToAreas; |
24 |
|
25 |
/// <summary>
|
26 |
/// Instantiates new FeatureExtractor class.
|
27 |
/// </summary>
|
28 |
/// <param name="dataParser">Data parser used to access training data.</param>
|
29 |
public FeatureExtractor(IDataParser dataParser, Dictionary<string, int> buildingsToAreas) |
30 |
{
|
31 |
this.dataParser = dataParser; |
32 |
this.buildingsToAreas = buildingsToAreas; |
33 |
}
|
34 |
|
35 |
/// <summary>
|
36 |
/// TODO comment
|
37 |
/// </summary>
|
38 |
/// <param name="area"></param>
|
39 |
/// <param name="startDate"></param>
|
40 |
/// <param name="endDate"></param>
|
41 |
/// <param name="interval"></param>
|
42 |
/// <param name="wholeDay"></param>
|
43 |
/// <returns></returns>
|
44 |
public List<ModelInput> PrepareTrainingInput(int area, DateTime startDate, DateTime endDate, int interval = 1, bool wholeDay = true) |
45 |
{
|
46 |
dataParser.Parse(startDate, endDate, interval, wholeDay); |
47 |
List<string> buildings = new List<string>(); |
48 |
|
49 |
// find all buildings in area
|
50 |
foreach (KeyValuePair<string, int> kvp in buildingsToAreas) |
51 |
{
|
52 |
if (kvp.Value == area) |
53 |
{
|
54 |
buildings.Add(kvp.Key); |
55 |
}
|
56 |
}
|
57 |
|
58 |
var res = new List<ModelInput>(); |
59 |
foreach (WeatherInfo val in dataParser.WeatherList) |
60 |
{
|
61 |
res.Add(new ModelInput |
62 |
{
|
63 |
Time = val.startTime, |
64 |
Temp = (float)val.temp, |
65 |
Hour = val.startTime.Hour, |
66 |
Wind = (float)val.wind, |
67 |
Rain = (float)val.rain, |
68 |
});
|
69 |
}
|
70 |
|
71 |
List<ActivityInfo> attendance = dataParser.AttendanceList; |
72 |
foreach (ModelInput input in res) |
73 |
{
|
74 |
List<int> amounts = new List<int>(); |
75 |
List<int> maxima = new List<int>(); |
76 |
foreach (string building in buildings) |
77 |
{
|
78 |
|
79 |
amounts.Add(attendance.Where(activity => activity.building.Equals(building) && activity.startTime.Equals(input.Time)).Select(activity => activity.amount).First()); |
80 |
maxima.Add(attendance.Where(activity => activity.building.Equals(building)).Select(activity => activity.amount).Max()); |
81 |
}
|
82 |
double ratio = amounts.Sum() / (double)maxima.Sum(); |
83 |
input.Label = RatioToLabel(ratio); |
84 |
}
|
85 |
|
86 |
return res; |
87 |
}
|
88 |
|
89 |
private string RatioToLabel(double ratio) |
90 |
{
|
91 |
if (ratio < 0.1f) |
92 |
{
|
93 |
return "10%"; |
94 |
}
|
95 |
else if (ratio < 0.2f) |
96 |
{
|
97 |
return "20%"; |
98 |
}
|
99 |
else if (ratio < 0.3f) |
100 |
{
|
101 |
return "30%"; |
102 |
}
|
103 |
else if (ratio < 0.4f) |
104 |
{
|
105 |
return "40%"; |
106 |
}
|
107 |
else if (ratio < 0.5f) |
108 |
{
|
109 |
return "50%"; |
110 |
}
|
111 |
else if (ratio < 0.6f) |
112 |
{
|
113 |
return "60%"; |
114 |
}
|
115 |
else if (ratio < 0.7f) |
116 |
{
|
117 |
return "70%"; |
118 |
}
|
119 |
else if (ratio < 0.8f) |
120 |
{
|
121 |
return "80%"; |
122 |
}
|
123 |
else if (ratio < 0.9f) |
124 |
{
|
125 |
return "90%"; |
126 |
}
|
127 |
else
|
128 |
{
|
129 |
return "100%"; |
130 |
}
|
131 |
}
|
132 |
|
133 |
private double LabelToRatio(string label) |
134 |
{
|
135 |
if (label.Equals("10%")) |
136 |
{
|
137 |
return 0.1f; |
138 |
}
|
139 |
else if (label.Equals("20%")) |
140 |
{
|
141 |
return 0.2f; |
142 |
}
|
143 |
else if (label.Equals("30%")) |
144 |
{
|
145 |
return 0.3f; |
146 |
}
|
147 |
else if (label.Equals("40%")) |
148 |
{
|
149 |
return 0.4f; |
150 |
}
|
151 |
else if (label.Equals("50%")) |
152 |
{
|
153 |
return 0.5f; |
154 |
}
|
155 |
else if (label.Equals("60%")) |
156 |
{
|
157 |
return 0.6f; |
158 |
}
|
159 |
else if (label.Equals("70%")) |
160 |
{
|
161 |
return 0.7f; |
162 |
}
|
163 |
else if (label.Equals("80%")) |
164 |
{
|
165 |
return 0.8f; |
166 |
}
|
167 |
else if (label.Equals("90%")) |
168 |
{
|
169 |
return 0.9f; |
170 |
}
|
171 |
else
|
172 |
{
|
173 |
return 1.0f; |
174 |
}
|
175 |
}
|
176 |
}
|
177 |
}
|