-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpurityFunctions.cpp
52 lines (33 loc) · 1.3 KB
/
impurityFunctions.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
#include "impurityFunctions.h"
double calculateGiniIndex(const std::vector<unsigned int>& classCounts)
{
double giniIndex = 0.0;
double currentProbability = 0.0;
//sum of class counts is at the back of the vector, generatred while they were calculated
double totalSamples = (double)classCounts.back();
std::vector<unsigned int>::const_iterator classCountsIt = classCounts.begin();
for (classCountsIt; classCountsIt != classCounts.end() - 1; classCountsIt++)
{
if (*classCountsIt == 0)
continue;
currentProbability = (double)(*classCountsIt) / totalSamples;
giniIndex += currentProbability * (1 - currentProbability);
}
return giniIndex;
}
double calculateShannonEntropy(const std::vector<unsigned int>& classCounts)
{
double shannonEntropy = 0.0;
double currentProbability = 0.0;
//sum of class counts is at the back of the vector, generatred while they were calculated
double totalSamples = (double)classCounts.back();
std::vector<unsigned int>::const_iterator classCountsIt = classCounts.begin();
for (classCountsIt; classCountsIt != classCounts.end() - 1; classCountsIt++)
{
if (*classCountsIt == 0)
continue;
currentProbability = (double)(*classCountsIt) / totalSamples;
shannonEntropy += currentProbability * std::log2(currentProbability);
}
return -shannonEntropy;
}