-
Notifications
You must be signed in to change notification settings - Fork 3
/
ClustersComparer.h
206 lines (189 loc) · 7.91 KB
/
ClustersComparer.h
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef _COMPARE_CLUSTERS_H_
#define _COMPARE_CLUSTERS_H_
#include <string>
#include <sstream>
#include <fstream>
#include <map>
#include <assert.h>
#include <vector>
#include <set>
// this class implements the variation of information metric for comparing two clusterings of the same data points.
// details can be found at http://www.cs.cmu.edu/~wammar/www.stat.washington.edu/mmp/Papers/jasa-compare.ps
class ClustersComparer {
ClustersComparer() {
}
// assumptions:
// - confusionMatrix is empty
static void BuildConfusionMatrix(const std::vector<std::string> &a, const std::vector<std::string> &b,
std::map<std::string, std::map<std::string, unsigned> > &confusionMatrix) {
assert(confusionMatrix.size() == 0);
// initialize the confusion matrix to all zeros
std::set<std::string> aClusters, bClusters;
for(unsigned i = 0; i < a.size(); i++) {
aClusters.insert(a[i]);
bClusters.insert(b[i]);
}
for(std::set<std::string>::const_iterator aClustersIter = aClusters.begin();
aClustersIter != aClusters.end();
aClustersIter++) {
for(std::set<std::string>::const_iterator bClustersIter = bClusters.begin();
bClustersIter != bClusters.end();
bClustersIter++) {
confusionMatrix[*aClustersIter][*bClustersIter] = 0;
}
}
// fill in the entries
for(unsigned i = 0; i < a.size(); i++) {
const std::string &aCluster = a[i], &bCluster = b[i];
confusionMatrix[aCluster][bCluster]++;
}
}
static std::string PrintConfusionMatrix(const std::map<std::string, std::map<std::string, unsigned> > &confusionMatrix) {
assert(confusionMatrix.size() != 0);
std::stringstream ss;
// print headers (i.e. b clusters)
ss << "\t";
for(std::map<std::string, unsigned>::const_iterator bIter = confusionMatrix.begin()->second.begin();
bIter != confusionMatrix.begin()->second.end();
bIter++) {
ss << bIter->first << "\t";
}
ss << std::endl << std::endl;
for(std::map<std::string, std::map<std::string, unsigned> >::const_iterator aIter = confusionMatrix.begin();
aIter != confusionMatrix.end();
aIter++) {
// print column followed by values
ss << aIter->first << "\t";
for(std::map<std::string, unsigned>::const_iterator bIter = aIter->second.begin();
bIter != aIter->second.end();
bIter++) {
ss << bIter->second << "\t";
}
ss << std::endl << std::endl;
}
return ss.str();
}
// assumtpions:
// - clusterSizes is empty
static void ComputeClusterSizes(const std::vector<std::string> &clustering,
std::map<std::string, unsigned> &clusterSizes) {
assert(clusterSizes.size() == 0);
for(std::vector<std::string>::const_iterator aIter = clustering.begin(); aIter != clustering.end(); aIter++) {
clusterSizes[*aIter]++;
}
}
static double ComputeClusterEntropies(const std::map<std::string, unsigned> &clusterSizes, unsigned dataSize) {
assert(dataSize != 0);
double entropy = 0.0;
for(std::map<std::string, unsigned>::const_iterator sizeIter = clusterSizes.begin(); sizeIter != clusterSizes.end(); sizeIter++) {
entropy -= ((double)sizeIter->second / dataSize) * log((double)sizeIter->second / dataSize);
}
return entropy;
}
static bool VerifyTwoClusteringsAreValid(const std::vector<std::string> &a, const std::vector<std::string> &b) {
bool valid = true;
// make sure both a and b are clusterings of the same set of data points.
if(a.size() != b.size() || a.size() == 0) {
valid = false;
}
return valid;
}
static double ComputeMutualInformation(const std::map<std::string, std::map<std::string, unsigned> > confusionMatrix,
const std::map<std::string, unsigned> aCounts,
const std::map<std::string, unsigned> bCounts,
int dataSize) {
assert(dataSize != 0);
double mutualInformation = 0.0;
for(std::map<std::string, unsigned>::const_iterator aIter = aCounts.begin(); aIter != aCounts.end(); aIter++) {
for(std::map<std::string, unsigned>::const_iterator bIter = bCounts.begin(); bIter != bCounts.end(); bIter++) {
unsigned intersectionSize = confusionMatrix.find(aIter->first)->second.find(bIter->first)->second;
if(intersectionSize == 0) {
continue;
}
double term = (double) intersectionSize / dataSize;
term *= log(term / ((double)aIter->second/dataSize * bIter->second/dataSize));
assert(!std::isnan(term));
mutualInformation += term;
}
}
return mutualInformation;
}
public:
// a and b are the two clusterings.
// keys of a and b are identical, representing the data points.
// a[point] and b[point] are the classes to which 'point' belongs in clustering a and b (respectively).
static double ComputeVariationOfInformation(const std::vector<std::string> &a, const std::vector<std::string> &b) {
assert(VerifyTwoClusteringsAreValid(a, b));
std::map<std::string, std::map<std::string, unsigned> > confusionMatrix;
BuildConfusionMatrix(a, b, confusionMatrix);
std::cerr << "confusion matrix:" << std::endl << PrintConfusionMatrix(confusionMatrix) << std::endl;
std::map<std::string, unsigned> aCounts, bCounts;
ComputeClusterSizes(a, aCounts);
ComputeClusterSizes(b, bCounts);
double aEntropy = ComputeClusterEntropies(aCounts, a.size());
std::cerr << "H(A) = " << aEntropy << std::endl;
double bEntropy = ComputeClusterEntropies(bCounts, b.size());
std::cerr << "H(B) = " << bEntropy << std::endl;
double mi = ComputeMutualInformation(confusionMatrix, aCounts, bCounts, a.size());
std::cerr << "MI(A,B) = " << mi << std::endl;
double vi = aEntropy - mi + bEntropy - mi;
std::cerr << "VI = " << vi << std::endl;
double ht = bEntropy, hc = aEntropy, ht2c = hc - mi, hc2t = ht - mi, h = 1 - hc2t / ht, c = 1 - ht2c / hc, vm = 2 * h * c / (h + c);
std::cerr << "VMeasure = " << vm << std::endl;
return vi;
}
// assumes b is the gold/reference
static double ComputeManyToOne(const std::vector<std::string> &a, const std::vector<std::string> &b) {
assert(VerifyTwoClusteringsAreValid(a, b));
std::map<std::string, std::map<std::string, unsigned> > confusionMatrix;
BuildConfusionMatrix(a, b, confusionMatrix);
unsigned correct = 0;
for(std::map<std::string, std::map<std::string, unsigned> >::const_iterator aIter = confusionMatrix.begin();
aIter != confusionMatrix.end();
aIter++) {
unsigned max = 0;
for(std::map<std::string, unsigned>::const_iterator bIter = aIter->second.begin();
bIter != aIter->second.end();
bIter++) {
if(bIter->second > max) {
max = bIter->second;
}
}
correct += max;
}
return 1.0 * correct / a.size();
}
// assumes b is the gold/reference
static double ComputeOneToOne(const std::vector<std::string> &a, const std::vector<std::string> &b) {
assert(VerifyTwoClusteringsAreValid(a, b));
std::map<std::string, std::map<std::string, unsigned> > confusionMatrix;
BuildConfusionMatrix(a, b, confusionMatrix);
std::set<std::string> consumedALabels, consumedBLabels;
unsigned correct = 0;
while( consumedALabels.size() < confusionMatrix.size() ){
// find the largest intersection which hasn't been consumed yet
unsigned max = 0;
string maxA, maxB;
for(std::map<std::string, std::map<std::string, unsigned> >::const_iterator aIter = confusionMatrix.begin();
aIter != confusionMatrix.end();
aIter++) {
if(consumedALabels.count(aIter->first) == 1) {continue;}
for(std::map<std::string, unsigned>::const_iterator bIter = aIter->second.begin();
bIter != aIter->second.end();
bIter++) {
if(consumedALabels.count(bIter->first) == 1) {continue;}
if(bIter->second > max) {
max = bIter->second;
maxA = aIter->first;
maxB = bIter->first;
}
}
}
correct += max;
consumedALabels.insert(maxA);
consumedBLabels.insert(maxB);
}
return 1.0 * correct / a.size();
}
};
#endif