-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrequencyTable.cpp
executable file
·65 lines (47 loc) · 1.66 KB
/
FrequencyTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// Created by karl on 05.05.19.
//
#include <numeric>
#include "FrequencyTable.h"
#include "EntropyCalculator.h"
#include <map>
#include <iostream>
FrequencyTable::FrequencyTable(const std::vector<std::vector<std::string>> &data, int column) {
bool first = true; // TODO: Ugly
// Build the frequency table by counting how many times the positive or negative result
// is reached for all possible attribute values
for (const auto &line : data) {
if (first) {
first = false;
continue;
}
std::string attributeVal = line[column];
std::string resultVal = line.back();
attributeFrequencies[attributeVal][resultVal] += 1;
}
}
double FrequencyTable::getGain() {
EntropyCalculator ec = EntropyCalculator();
double gain = 0;
for (const auto &attribute : attributeFrequencies) {
std::string attributeName = attribute.first;
std::map<std::string, int> countAtOutcome = attribute.second;
std::vector<int> line;
line.reserve(countAtOutcome.size());
for (const auto &count : countAtOutcome) {
line.push_back(count.second);
}
// Total number of occurrences of this attribute in data
int sum = std::accumulate(line.begin(), line.end(), 0);
// Here, the actual formula is applied
gain -= (double(sum) / attributeCount) * ec.getEntroy(line, sum);
}
return gain;
}
std::list<std::string> FrequencyTable::getAttributes() {
auto attributes = std::list<std::string>();
for (const auto &attribute : attributeFrequencies) {
attributes.push_back(attribute.first);
}
return attributes;
}