-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path18EC10028_4.cpp
210 lines (191 loc) · 4.34 KB
/
18EC10028_4.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
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
//Roll No : 18EC10028
//Name : Kushal Kedia
//Assignment No. : 4
/*Have not normalised features because input is of same dimension and normalisation is not explicitly asked -
also used boost library to convert string values to float - other option was to use atof but requires c++11 compiler*/
#include<bits/stdc++.h>
#include<fstream>
#include<sstream>
#include<string.h>
#include <boost/lexical_cast.hpp>
using namespace std;
using boost::lexical_cast;
int main()
{
//No of k-means and no. of iterations
int k=3;
int iter=100;
//Reading of datset
fstream fin;
fin.open("data4_19.csv",ios::in);
vector <vector <string> > data;
vector <vector <float> > dataset;
vector <string> row;
vector <float> rowset;
string line,word,temp;
float x;
while(getline(fin,line))
{
stringstream s(line);
while(getline(s,word,','))
row.push_back(word);
data.push_back(row);
row.clear();
}
fin.close();
//Hardcoded this as .csv seems to have an extra blank line which is causing problems
int points=150;
//Convert values to float
for(int i=0;i<points;i++)
{
for(int j=0;j<data[0].size()-1;j++)
{
temp=data[i][j];
x = boost::lexical_cast<float>(temp);
rowset.push_back(x);
}
dataset.push_back(rowset);
rowset.clear();
}
//Initalise k random points as initial centroids
int d=data[0].size()-1;
vector < vector < float> > centroids;
vector <int> indexs;
int index=0;
int flag=0;
for(int i=0;i<k;i++)
{
//checking to see that we don't choose same random point again - symmetry will cause problems
do
{
flag=0;
index = rand()%points;
for(int j=0;j<indexs.size();j++)
{
if(index==indexs[j])
flag=1;
}
} while(flag);
indexs.push_back(index);
centroids.push_back(dataset[index]);
}
vector < vector <int> > clusters;
vector <int> v;
//run iter=10 iterations of centroid updation
for(int i=0;i<iter;i++)
{
//initalisation of clusters in each iteration
clusters.clear();
for(int i=0;i<k;i++)
{
clusters.push_back(v);
}
//classify each point to closest centroid
for(int j=0;j<points;j++)
{
int cluster=-1;
float min=-1;
for(int m=0;m<k;m++)
{
float sum=0;
for(int n=0;n<d;n++)
{
sum+=pow(dataset[j][n]-centroids[m][n],2);
}
if(min==-1||sum<min)
{
cluster=m;
min=sum;
}
}
clusters[cluster].push_back(j);
}
//update centroid to mean of each cluster
for(int m=0;m<clusters.size();m++)
{
vector <float> vec(d);
for(int j=0;j<clusters[m].size();j++)
{
for(int n=0;n<d;n++)
{
vec[n]+=dataset[clusters[m][j]][n];
}
}
for(int n=0;n<d;n++)
{
vec[n]/=clusters[m].size();
}
centroids[m]=vec;
}
}
//print the means of each cluster
for(int i=0;i<clusters.size();i++)
{
cout << "Mean of Cluster "<<i+1<<":\n";
for(int n=0;n<d;n++)
{
cout << centroids[i][n] << "\t";
}
cout << endl;
}
//extract the different ground truth labels
vector <string> labels;
for(int i=0;i<points;i++)
{
flag=0;
for(int j=0;j<labels.size();j++)
{
if(data[i][d]==labels[j])
flag=1;
}
if(!flag)
labels.push_back(data[i][d]);
}
int c=labels.size();
//computer jacquard distances of each cluster with each ground truth label
for(int i=0;i<clusters.size();i++)
{
cout << endl;
cout << "Jacquard Distances of Cluster "<<i+1<<":\n";
vector <float> intersection(c);
vector <float> unions(c);
float min=-1;
int index=-1;
for(int j=0;j<c;j++)
{
//find corresponding labels in data set
for(int m=0;m<points;m++)
{
if(data[m][d]==labels[j])
{
unions[j]++;
}
}
//find unions and intersections
for(int m=0;m<clusters[i].size();m++)
{
unions[j]++;
if(data[clusters[i][m]][d]==labels[j])
intersection[j]++;
}
//subtract double counts
unions[j]-=intersection[j];
//compute smallest Jacquard Distance
if(min==-1)
{
min=1-intersection[j]/unions[j];
index=j;
}
else if(1-intersection[j]/unions[j]<min)
{
min=1-intersection[j]/unions[j];
index=j;
}
cout << "\t Distance from " << labels[j] <<" = " << 1-intersection[j]/unions[j] << endl;
}
cout << "------------------------------------------------------------------------\n";
cout << "Cluster probably corresponds to -> " << labels[index] << endl;
cout << "------------------------------------------------------------------------\n";
}
return 0;
}