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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
// Copyright (C) 2008 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_KKMEANs_ABSTRACT_
#ifdef DLIB_KKMEANs_ABSTRACT_
#include <cmath>
#include "../matrix/matrix_abstract.h"
#include "../algs.h"
#include "../serialize.h"
#include "kernel_abstract.h"
#include "kcentroid_abstract.h"
#include "../noncopyable.h"
namespace dlib
{
template <
typename kernel_type
>
class kkmeans : public noncopyable
{
/*!
REQUIREMENTS ON kernel_type
is a kernel function object as defined in dlib/svm/kernel_abstract.h
INITIAL VALUE
- number_of_centers() == 1
- get_min_change() == 0.01
WHAT THIS OBJECT REPRESENTS
This is an implementation of a kernelized k-means clustering algorithm.
It performs k-means clustering by using the kcentroid object.
!*/
public:
typedef typename kernel_type::scalar_type scalar_type;
typedef typename kernel_type::sample_type sample_type;
typedef typename kernel_type::mem_manager_type mem_manager_type;
kkmeans (
const kcentroid<kernel_type>& kc_
);
/*!
ensures
- #number_of_centers() == 1
- #get_min_change() == 0.01
- #get_kcentroid(0) == a copy of kc_
!*/
~kkmeans(
);
/*!
ensures
- all resources associated with *this have been released
!*/
void set_kcentroid (
const kcentroid<kernel_type>& kc_
);
/*!
ensures
- for all idx:
- #get_kcentroid(idx) == a copy of kc_
!*/
const kcentroid<kernel_type>& get_kcentroid (
unsigned long i
) const;
/*!
requires
- i < number_of_centers()
ensures
- returns a const reference to the ith kcentroid object contained in
this object. Each kcentroid represents one of the centers found
by the k-means clustering algorithm.
!*/
const kernel_type& get_kernel (
) const;
/*!
ensures
- returns a const reference to the kernel used by this object
!*/
void set_number_of_centers (
unsigned long num
);
/*!
requires
- num > 0
ensures
- #number_of_centers() == num
!*/
unsigned long number_of_centers (
) const;
/*!
ensures
- returns the number of centers used in this instance of the k-means clustering
algorithm.
!*/
template <
typename matrix_type,
typename matrix_type2
>
void train (
const matrix_type& samples,
const matrix_type2& initial_centers,
long max_iter = 1000
);
/*!
requires
- matrix_type and matrix_type2 must either be dlib::matrix objects or convertible to dlib::matrix
via mat()
- matrix_type::type == sample_type (i.e. matrix_type should contain sample_type objects)
- matrix_type2::type == sample_type (i.e. matrix_type2 should contain sample_type objects)
- initial_centers.nc() == 1 (i.e. must be a column vector)
- samples.nc() == 1 (i.e. must be a column vector)
- initial_centers.nr() == number_of_centers()
ensures
- performs k-means clustering of the given set of samples. The initial center points
are taken from the initial_centers argument.
- loops over the data and continues to refine the clustering until either less than
get_min_change() fraction of the data points change clusters or we have done max_iter
iterations over the data.
- After this function finishes you can call the operator() function below
to determine which centroid a given sample is closest to.
!*/
unsigned long operator() (
const sample_type& sample
) const;
/*!
ensures
- returns a number idx such that:
- idx < number_of_centers()
- get_kcentroid(idx) == the centroid that is closest to the given
sample.
!*/
void set_min_change (
scalar_type min_change
);
/*!
requires
- 0 <= min_change < 1
ensures
- #get_min_change() == min_change
!*/
const scalar_type get_min_change (
) const;
/*!
ensures
- returns the minimum fraction of data points that need to change
centers in an iteration of kmeans for the algorithm to keep going.
!*/
void swap (
kkmeans& item
);
/*!
ensures
- swaps *this and item
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename kernel_type
>
void swap(
kkmeans<kernel_type>& a,
kkmeans<kernel_type>& b
) { a.swap(b); }
/*!
provides a global swap function
!*/
template <
typename kernel_type
>
void serialize (
const kkmeans<kernel_type>& item,
std::ostream& out
);
/*!
provides serialization support for kkmeans objects
!*/
template <
typename kernel_type
>
void deserialize (
kkmeans<kernel_type>& item,
std::istream& in
);
/*!
provides serialization support for kkmeans objects
!*/
// ----------------------------------------------------------------------------------------
template <
typename vector_type1,
typename vector_type2,
typename kernel_type
>
void pick_initial_centers(
long num_centers,
vector_type1& centers,
const vector_type2& samples,
const kernel_type& k,
double percentile = 0.01
);
/*!
requires
- num_centers > 1
- 0 <= percentile < 1
- samples.size() > 1
- vector_type1 == something with an interface compatible with std::vector
- vector_type2 == something with an interface compatible with std::vector
- k(samples[0],samples[0]) must be a valid expression that returns a double
- both centers and samples must be able to contain kernel_type::sample_type
objects
ensures
- finds num_centers candidate cluster centers in the data in the samples
vector. Assumes that k is the kernel that will be used during clustering
to define the space in which clustering occurs.
- The centers are found by looking for points that are far away from other
candidate centers. However, if the data is noisy you probably want to
ignore the farthest way points since they will be outliers. To do this
set percentile to the fraction of outliers you expect the data to contain.
- #centers.size() == num_centers
- #centers == a vector containing the candidate centers found
!*/
// ----------------------------------------------------------------------------------------
template <
typename vector_type1,
typename vector_type2
>
void pick_initial_centers(
long num_centers,
vector_type1& centers,
const vector_type2& samples,
double percentile = 0.01
);
/*!
requires
- num_centers > 1
- 0 <= percentile < 1
- samples.size() > 1
- vector_type1 == something with an interface compatible with std::vector
- vector_type2 == something with an interface compatible with std::vector
- Both centers and samples must be able to contain dlib::matrix based row or
column vectors.
ensures
- performs: pick_initial_centers(num_centers, centers, samples, linear_kernel<sample_type>(), percentile)
(i.e. this function is simply an overload that uses the linear kernel.
!*/
// ----------------------------------------------------------------------------------------
template <
typename array_type,
typename sample_type,
typename alloc
>
void find_clusters_using_kmeans (
const array_type& samples,
std::vector<sample_type, alloc>& centers,
unsigned long max_iter = 1000
);
/*!
requires
- samples.size() > 0
- samples == a bunch of row or column vectors and they all must be of the
same length.
- centers.size() > 0
- array_type == something with an interface compatible with std::vector
and it must contain row or column vectors capable of being stored in
sample_type objects.
- sample_type == a dlib::matrix capable of representing vectors
ensures
- performs regular old linear kmeans clustering on the samples. The clustering
begins with the initial set of centers given as an argument to this function.
When it finishes #centers will contain the resulting centers.
- no more than max_iter iterations will be performed before this function
terminates.
!*/
// ----------------------------------------------------------------------------------------
template <
typename array_type,
typename sample_type,
typename alloc
>
void find_clusters_using_angular_kmeans (
const array_type& samples,
std::vector<sample_type, alloc>& centers,
unsigned long max_iter = 1000
);
/*!
requires
- samples.size() > 0
- samples == a bunch of row or column vectors and they all must be of the
same length.
- centers.size() > 0
- array_type == something with an interface compatible with std::vector
and it must contain row or column vectors capable of being stored in
sample_type objects.
- sample_type == a dlib::matrix capable of representing vectors
ensures
- performs linear kmeans clustering on the samples, except instead of using
Euclidean distance to compare samples to the centers it uses the angle
between a sample and a center (with respect to the origin). So we try to
cluster samples together if they have small angles with respect to each
other. The clustering begins with the initial set of centers given as an
argument to this function. When it finishes #centers will contain the
resulting centers.
- for all valid i:
- length(#centers[i]) == 1
(i.e. the output centers are scaled to be unit vectors since their
magnitude is irrelevant. Moreover, this makes it so you can use
functions like nearest_center() with #centers to find the cluster
assignments.)
- No more than max_iter iterations will be performed before this function
terminates.
!*/
// ----------------------------------------------------------------------------------------
template <
typename array_type,
typename EXP
>
unsigned long nearest_center (
const array_type& centers,
const matrix_exp<EXP>& sample
);
/*!
requires
- centers.size() > 0
- sample.size() > 0
- is_vector(sample) == true
- centers must be an array of vectors such that the following expression is
valid: length_squared(sample - centers[0]). (e.g. centers could be a
std::vector of matrix objects holding column vectors).
ensures
- returns the index that identifies the element of centers that is nearest to
sample. That is, returns a number IDX such that centers[IDX] is the element
of centers that minimizes length(centers[IDX]-sample).
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_KKMEANs_ABSTRACT_
|