-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmeans.cpp
More file actions
243 lines (209 loc) · 7.44 KB
/
Copy pathkmeans.cpp
File metadata and controls
243 lines (209 loc) · 7.44 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// kmeans.cpp
// VoterMLA
//
// Created by MD Shihabul Kabir on 12/5/16.
// Copyright © 2016 MD Shihabul Kabir. All rights reserved.
//
#include "county.h"
#include "kmeans.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <iostream>
using namespace std;
using namespace CountyStruct;
//K-Means Clustering Namespace
namespace KmeansCluster{
//find the closest cluster to add
void KMeans:: addToClosest(CountyStruct::County&acounty){
//check if county passed in centroid
if(current[0].compare(acounty) || current[1].compare(acounty) || current[2].compare(acounty) ) return;
//get the distance from the centroid to the county
float distance1 = current[0].distance(acounty);
float distance2 = current[1].distance(acounty);
float distance3 = current[2].distance(acounty);
//check which distance is the smallest
float smallest = distance1;
if(smallest > distance2){
smallest = distance2;
}
if(smallest > distance3){
smallest = distance3;
}
//based on the smallest distance add to cluster
if(smallest == distance1){
cluster1.push_back(acounty);
}
else if(smallest == distance2){
cluster2.push_back(acounty);
}
else{
cluster3.push_back(acounty);
}
}
//method to initialize
void KMeans:: initialize(vector<County> counties){
//initialize random
srand(time(NULL));
//get three random indexes
int index1 = rand() % 100;
int index2 = rand() % 500 + 100;
int index3 = rand() % 1000 + 500;
//get the three random centroids
County first = counties[index1];
County second = counties[index2];
County third = counties[index3];
//push them into the clusters and setup centroids
cluster1.push_back(first);
cluster2.push_back(second);
cluster3.push_back(third);
current.push_back(first);
current.push_back(second);
current.push_back(third);
//go through counties and add to each cluster
for(County c : counties){
addToClosest(c);
all.push_back(c);
}
}
//method to get the mean of a cluster
vector<float> KMeans::mean(std::vector<CountyStruct::County>&cluster){
vector<float>totals;
for(int i =DEMOCRAT; i < TOTAL_ATTR; ++i){
totals.push_back(0);
}
//go through and tally the total sum
for(County c : cluster){
for(int i =POPULATION; i < TOTAL_ATTR; ++i){
totals[i] += c.member(i);
}
}
//calculate the average sums
for(int i = POPULATION; i < TOTAL_ATTR; ++i){
totals[i] /= cluster.size();
}
return totals;
}
//method to get centroid closest to mean of cluster
County KMeans::getCentroid(std::vector<CountyStruct::County>&cluster,vector<float>mean){
//initialize global difference and centroid to return
County centroid = cluster[0];
float diff = 0;
for(int i =POPULATION; i < TOTAL_ATTR; ++i){
diff += powf(centroid.member(i)-mean[i],2);
}
diff = sqrtf(diff);
//loop through and find county closest to mean
for(County c : cluster){
float local = 0;
for(int i =POPULATION; i < TOTAL_ATTR; ++i){
local += powf(c.member(i)-mean[i],2);
}
local = sqrtf(local);
if(local < diff){
diff = local;
centroid = c;
}
}
return centroid;
}
//method to setup centroids
bool KMeans::setupCentroids(){
//get the centroids of each initialized clusters
County c1 = getCentroid(cluster1, mean(cluster1));
County c2 = getCentroid(cluster2, mean(cluster2));
County c3 = getCentroid(cluster3, mean(cluster3));
//if current and last are the same then return
if(current[0].compare(c1) && current[1].compare(c2) && current[2].compare(c3)) return false;
//otherwise clear the clusters and push back the clusters
current[0] = c1;
current[1] = c2;
current[2] = c3;
cluster1.clear();
cluster2.clear();
cluster3.clear();
cluster1.push_back(c1);
cluster2.push_back(c2);
cluster3.push_back(c3);
return true;
}
//method to make the clusters
void KMeans:: cluster(){
//while the centroids update
while(setupCentroids()){
//go through all the data set
for(County c : all){
//add to closest cluster
addToClosest(c);
}
}
}
//method to get the distance from a point to rest of cluster
float KMeans:: avgDistance(vector<County>&cluster,int index){
//cumilate euclidean distance
float total = 0;
for(int i = 0; i < cluster.size(); ++i){
if(i != index){
total += cluster[index].distance(cluster[i]);
}
}
//avg distance from a point to cluster
float avg = total/(cluster.size()-1);
return avg;
}
//method to find distance from cluster from a point
float KMeans:: distanceFromCluster(County&c,vector<County>&cluster){
//cumilate distance
float distance = 0;
for(County& a : cluster){
distance += c.distance(a);
}
//return distance
return distance;
}
//method to return silhoute value
float KMeans:: silh(vector<County>&a,vector<County>&b,int index){
float aval = avgDistance(a, index);
float bval = distanceFromCluster(a[index], b);
float sil = (bval - aval)/max(bval,aval);
return sil;
}
//method to print the silhoute for each cluster
void KMeans:: printSil(){
//find the value for cluster 1
float sil = 0;
for(int i = 0; i < cluster1.size(); ++i){
if(distanceFromCluster(cluster1[i],cluster2) < distanceFromCluster(cluster1[i],cluster3)){
sil += silh(cluster1,cluster2,i);
}else{
sil += silh(cluster1,cluster3,i);
}
}
float avsil = sil/cluster1.size();
cout << "cluster 1 similarity: " << avsil << endl;
//find the value for cluster 2
sil = 0;
for(int i = 0; i < cluster2.size(); ++i){
if(distanceFromCluster(cluster2[i],cluster3) < distanceFromCluster(cluster2[i],cluster1)){
sil += silh(cluster2,cluster3,i);
}else{
sil += silh(cluster2,cluster1,i);
}
}
avsil = sil/cluster2.size();
cout << "cluster 2 similarity: " << avsil << endl;
//find the value for cluster 3
sil = 0;
for(int i = 0; i < cluster3.size(); ++i){
if(distanceFromCluster(cluster3[i],cluster2) < distanceFromCluster(cluster3[i],cluster1)){
sil += silh(cluster3,cluster2,i);
}else{
sil += silh(cluster3,cluster1,i);
}
}
avsil = sil/cluster3.size();
cout << "cluster 3 similarity: " << avsil << endl;
}
}