-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-kmeans-2.cpp
74 lines (60 loc) · 1.4 KB
/
test-kmeans-2.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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <readline/readline.h>
#include <unistd.h>
#include "Data.h"
#include "Cluster.h"
using namespace std;
int main() {
int set_size = 0;
int num_features = 4;
ifstream iris_data("iris.txt");
vector<Data*> data;
string line;
if (iris_data.is_open()) {
while ( getline (iris_data,line) )
{
if (!line.length())
break;
char* cline = strdup(line.c_str());
char* elem = strtok(cline, ",");
float vals[4] = {0};
for (int i = 0; i < 4; i++) {
vals[i] = atof(elem);
elem = strtok(NULL, ",");
}
Data *newData = new Data(num_features);
newData->setFeatures(num_features, vals);
data.push_back(newData);
set_size++;
free(cline);
}
iris_data.close();
}
if (!data.size())
return 1;
// Repeatedly classify
srand(time(NULL));
for (int p = 0; p < 10; p++) {
Cluster learner(3, data);
learner.update();
float** centroids = learner.centroids();
for (int i = 0; i < 3; i++) {
cout << "Centroid " << i << endl;
for (int j = 0; j < 4; j++) {
cout << "\t" << centroids[i][j];
}
cout << endl;
}
for (int i = 0; i < 3; i++)
free(centroids[i]);
free(centroids);
cout << endl << endl;
}
for (int i = 0; i < set_size; i++) {
delete data[i];
}
return 0;
}