SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KMeans.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2008 Gunnar Raetsch
8  * Written (W) 2007-2009 Soeren Sonnenburg
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #ifndef _KMEANS_H__
13 #define _KMEANS_H__
14 
15 #include <shogun/lib/config.h>
16 
17 #include <shogun/lib/common.h>
18 #include <shogun/io/SGIO.h>
22 
23 namespace shogun
24 {
25 class CDistanceMachine;
26 
29 {
32 
33  /* Standard KMeans with Lloyds algorithm */
35 };
36 
57 class CKMeans : public CDistanceMachine
58 {
59  public:
61  CKMeans();
62 
69  CKMeans(int32_t k, CDistance* d, EKMeansMethod f);
70 
78  CKMeans(int32_t k, CDistance* d, bool kmeanspp=false, EKMeansMethod f=KMM_LLOYD);
79 
86  CKMeans(int32_t k_i, CDistance* d_i, SGMatrix<float64_t> centers_i, EKMeansMethod f=KMM_LLOYD);
87  virtual ~CKMeans();
88 
89 
91 
92 
97 
103  virtual bool load(FILE* srcfile);
104 
110  virtual bool save(FILE* dstfile);
111 
116  void set_k(int32_t p_k);
117 
122  int32_t get_k();
123 
128  void set_use_kmeanspp(bool kmpp);
129 
134  bool get_use_kmeanspp() const;
135 
140  void set_fixed_centers(bool fixed);
141 
146  bool get_fixed_centers();
147 
152  void set_max_iter(int32_t iter);
153 
159 
165 
171 
176  int32_t get_dimensions();
177 
179  virtual const char* get_name() const { return "KMeans"; }
180 
185  virtual void set_initial_centers(SGMatrix<float64_t> centers);
186 
192 
198 
203  void set_mbKMeans_batch_size(int32_t b);
204 
209  int32_t get_mbKMeans_batch_size() const;
210 
215  void set_mbKMeans_iter(int32_t t);
216 
221  int32_t get_mbKMeans_iter() const;
222 
228  void set_mbKMeans_params(int32_t b, int32_t t);
229 
230  private:
239  virtual bool train_machine(CFeatures* data=NULL);
240 
242  virtual void store_model_features();
243 
244  virtual bool train_require_labels() const { return false; }
245 
250  SGMatrix<float64_t> kmeanspp();
251  void init();
252 
257  void set_random_centers(SGVector<float64_t> weights_set, SGVector<int32_t> ClList, int32_t XSize);
258  void set_initial_centers(SGVector<float64_t> weights_set,
259  SGVector<int32_t> ClList, int32_t XSize);
260  void compute_cluster_variances();
261 
262  private:
264  int32_t max_iter;
265 
267  bool fixed_centers;
268 
270  int32_t k;
271 
273  int32_t dimensions;
274 
276  SGVector<float64_t> R;
277 
279  SGMatrix<float64_t> mus_initial;
280 
282  bool use_kmeanspp;
283 
285  int32_t batch_size;
286 
288  int32_t minib_iter;
289 
291  SGMatrix<float64_t> mus;
292 
294  EKMeansMethod train_method;
295 };
296 }
297 #endif
298 

SHOGUN Machine Learning Toolbox - Documentation