-
Notifications
You must be signed in to change notification settings - Fork 0
/
arm.cpp
284 lines (158 loc) · 4.66 KB
/
arm.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include<bits/stdc++.h>
#include<armadillo>
using namespace std;
// COMMAND TO COMPILE FILE
// g++ arm.cpp -o arm -O1 -larmadillo
// RUN COMMAND
// ./arm
template<typename T>
void get_object_type(T object){
cout<<typeid(T).name()<<endl;
}
arma::Mat<double> read_csv(string filename, bool skip = true){
vector< vector<double> > data;
ifstream fin;
fin.open(filename);
vector<double> row;
string temp,line,word;
long long int count = 0;
if(skip){
getline(fin,line);
}
while(fin){
row.clear();
getline(fin, line);
if(line.empty()){
break;
}
stringstream s(line);
try{
while(getline(s, word,',')){
row.emplace_back(stod(word));
}
}
catch(...){
cout<<"KMeans Algorithm Requries Data in Numerical Format Only"<<count<<endl;
exit(1);
}
data.emplace_back(row);
}
fin.close();
arma::Mat<double> dataset(data.size(), data[0].size());
for(int i=0;i<data.size();i++){
dataset.row(i) = arma::rowvec(data[i]);
}
return dataset;
}
tuple< arma::Row<double>, arma::Row<double> > min_max_of_features(arma::Mat<double>& dataset){
long long int cols = dataset.n_cols;
arma::Row<double> minimum(cols),maximum(cols);
tuple<arma::Row<double>, arma::Row<double> > minimum_maximum;
for(long long int i=0;i<cols;i++){
minimum[i] = dataset.col(i).min();
maximum[i] = dataset.col(i).max();
}
minimum_maximum = {minimum, maximum};
return minimum_maximum;
}
arma::Mat<double> initialize(arma::Mat<double> &dataset, long long int clusters
,arma::Row<double> minimum, arma::Row<double> maximum){
arma::Mat<double> means(clusters, dataset.n_cols);
means.fill(0);
long long int rows = dataset.n_rows, cols = dataset.n_cols,i,j;
random_device rd;
mt19937 gen(rd());
for(i=0;i<clusters;i++){
for(j=0;j<cols;j++){
uniform_real_distribution<> uniform_values(minimum[j] + 1, maximum[j] - 1);
means.row(i).col(j) = uniform_values(gen);
}
}
return means;
}
arma::Row<double> update_means(long long int size_of_cluster, arma::Row<double> &mean, arma::Row<double> &feature){
long long int i;
double value;
for(i=0;i<mean.n_cols;i++){
value = mean[i];
value = (value)*(size_of_cluster-1)+feature[i];
value /= size_of_cluster;
mean[i] = value;
}
return mean;
}
double euclidean_distance(arma::Row<double> &mean, arma::Row<double> &feature){
double sum = 0;
long long int i,cols = mean.n_cols;
for(i=0;i<cols;i++){
sum += pow(feature[i] - mean[i],2);
}
return sqrt(sum);
}
long long int identify_cluster(arma::Mat<double> &means, arma::Row<double> &feature){
long long int index = -1,i;
double temp = DBL_MAX,distance;
for(i=0;i<means.n_rows;i++){
arma::Row<double> mean = means.row(i);
distance = euclidean_distance(mean, feature);
if(distance < temp){
temp = distance;
index = i;
}
}
return index;
}
void find_clusters(arma::Mat<double> &dataset, arma::Mat<double> &means){
map<long long int, vector< arma::Row<double> > > cluster_map;
long long int i,rows = dataset.n_rows;
for(i=0;i<rows;i++){
arma::Row<double> dataset_row = dataset.row(i);
long long int key = identify_cluster(means,dataset_row);
cluster_map[key].emplace_back(dataset_row);
}
for(auto itr : cluster_map){
cout<<"CLUSTER "<<itr.first<<endl;
vector< arma::Row<double> > values;
for(auto x : itr.second){
cout<<x<<endl;
}
}
}
arma::Mat<double> KMEANS(arma::Mat<double> &dataset, long long int clusters,long long int max_iterations = 10000){
long long int cols = dataset.n_cols,index,size_of_cluster;
arma::Row<double> minimum(cols),maximum(cols);
tie(minimum, maximum) = min_max_of_features(dataset);
arma::Mat<double> means(clusters, cols);
means = initialize(dataset, clusters, minimum, maximum);
arma::Row<double> cluster_size(clusters), belongsTo(dataset.n_rows);
cluster_size.fill(0);
belongsTo.fill(0);
for(long long int i = 1;i<=max_iterations;i++){
bool flag = true;
for(long long int j = 0;j<dataset.n_rows;j++){
arma::Row<double> feature_row = dataset.row(j);
index = identify_cluster(means,feature_row);
cluster_size[index]++;
size_of_cluster = cluster_size[index];
arma::Row<double> mean_row = means.row(index);
means.row(index) = update_means(size_of_cluster,mean_row,feature_row);
if(index != belongsTo[j]){
flag = false;
}
}
if(flag){
break;
}
}
return means;
}
int main(){
arma::Mat<double> dataset = read_csv("iris.csv");
arma::Mat<double> means = KMEANS(dataset,5);
cout<<"Data Points"<<endl<<endl;
find_clusters(dataset, means);
arma::Mat<double> predict = { {1.00,1.5000,2.3000,4.780} , {6.15,3.45,5.48,2.45} };
cout<<"Prediction"<<endl<<endl;
find_clusters(predict, means);
return 0;
}