-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.hpp
123 lines (119 loc) · 2.04 KB
/
kmeans.hpp
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
#include<vector>
#include<math.h>
#include<stdlib.h>
using namespace std;
class KMeans
{
private:
int K;
int rows;
vector<float> *centroids;
vector<int>pred;
public:
KMeans()
{
K = 0;
rows = 0;
centroids = new vector<float>[K];
}
KMeans(int K)
{
this->K = K;
rows = 0;
centroids = new vector<float>[K];
}
void initialize(vector<float>image[])
{
srand(time(0));
for(int i=0;i<K;i++)
{
int select = rand()%rows;
centroids[i] = image[select];
}
}
float distance(vector<float> v1,vector<float> v2)
{
float dist;
for(int i=0;i<v1.size();i++)
dist = dist + pow((v1[i] - v2[i]),2);
dist = sqrt(dist);
return dist;
}
vector<float> Mean(vector<vector<float>> v,vector<float>image[])
{
vector<float> m;
if(v.size() == 0)
{
srand(time(0));
m = image[rand()%rows];
}
else
{
for(int i=0;i<v[0].size();i++)
{
float sum = 0;
for(int j=0;j<v.size();j++)
sum = sum + v[j][i];
m.push_back(sum/v.size());
}
}
return m;
}
void fit(vector<float>image[],int rows,int iterations = 10)
{
this->rows = rows;
initialize(image);
while(iterations--)
{
vector<vector<float>>cluster[K];
for(int i=0;i<rows;i++)
{
float Min = 1000.0;
int centroid_no = -1;
for(int j=0;j<K;j++)
{
if(distance(image[i],centroids[j]) < Min)
{
Min = distance(image[i],centroids[j]);
centroid_no = j;
}
}
cluster[centroid_no].push_back(image[i]);
}
for(int i=0;i<K;i++)
{
centroids[i] = Mean(cluster[i],image);
}
}
}
void predict(vector<float>image[])
{
for(int j=0;j<rows;j++)
{
int val = -1;
float Min = 10000000.0;
for(int i=0;i<K;i++)
{
if(distance(image[j],centroids[i]) < Min)
{
Min = distance(image[j],centroids[i]);
val = i;
}
}
pred.push_back(val);
}
}
vector<float> *getCentroids()
{
return centroids;
}
vector<int> getPredictions()
{
return pred;
}
vector<float> getCentroid(int index)
{
int actual_index = pred.at(index);
return centroids[actual_index];
}
};