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