-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcls_cluster_k_means.h
executable file
·128 lines (108 loc) · 2.45 KB
/
cls_cluster_k_means.h
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
#include "lib_import.h"
template<typename FUNC>
class cls_cluster_k_means
:cls_cluster
{
protected:
vector<vector<double> > center;
int n_pat;
int n_feauture;
int n_times;
public:
cls_cluster_k_means(int num_pattern,int num_feature,int max_times = 1000)
:n_pat(num_pattern),
n_feauture(num_feature),
center(num_pattern),
n_times(max_times)
{
vector<vector<double> >::iterator itor;
for(itor=center.begin();itor!=center.end();itor++)
{
itor->resize(n_feauture);
}
}
virtual vector<int> train_example(cls_training_set & ts )
{
vector<int> re(ts.get_size());
FUNC func;
if (ts.get_size() <= n_pat)
{
throw cls_exception_not_sense(string(__FUNCTION__) + "() - the dataset is too too too small.\n");
}
int cnt = 0;
ts.initialize();
while(!ts.epoch_over())
{
if (cnt >= n_pat)
break;
get_next_pair_for_ts(ts,a,b);
center[cnt++] = a;
}
int num_times = n_times;
bool b_change = true;
while(b_change && num_times--)
{
b_change = false;
ts.initialize();
while(!ts.epoch_over())
{
get_next_pair_for_ts(ts,a,b);
vector<double> d_re(n_pat);
vector<vector<double> >::iterator itor;
vector<double>::iterator itor_re;
for(itor_re=d_re.begin(),itor=center.begin();
itor!=center.end();
itor++,itor_re++)
{
*itor_re = func(a,*itor);
}
int indx = 0;
for(int cnt=1;cnt<n_pat;cnt++)
{
if (d_re[cnt] < d_re[indx])
{
indx = cnt;
}
}
if (re[ts.get_count() - 1] != indx)
{
re[ts.get_count()-1] = indx;
b_change = true;
}
}
for(int cnt=0;cnt<n_pat;cnt++)
{
fill(center[cnt].begin(),center[cnt].end(),0.0);
}
vector<int> num(n_pat);
fill(num.begin(),num.end(),0);
const vector<vector<double> > & inputs = ts.get_inputs();
vector<int>::iterator itor;
int pos=0;
for(pos=0,itor=re.begin();
itor!=re.end();
pos++,itor++)
{
num[*itor]++;
for(int cnt=0;cnt<n_feauture;cnt++)
{
center[*itor][cnt] += inputs[pos][cnt];
}
}
int cnt=0;
vector<vector<double> >::iterator itor_v;
for(cnt=0,itor_v=center.begin();
itor_v!=center.end();
cnt++,itor_v++)
{
if (num[cnt] == 0)
continue;
for(int pos=0;pos<n_feauture;pos++)
{
itor_v->operator [](pos) /= 1.0 * num[cnt];
}
}
}
return re;
}
};