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 <stdio.h>
18 #include <shogun/lib/common.h>
19 #include <shogun/io/SGIO.h>
23 
24 namespace shogun
25 {
26 class CDistanceMachine;
27 
30 {
33 
34  /* Standard KMeans with Lloyds algorithm */
36 };
37 
58 class CKMeans : public CDistanceMachine
59 {
60  public:
62  CKMeans();
63 
70  CKMeans(int32_t k, CDistance* d, EKMeansMethod f);
71 
79  CKMeans(int32_t k, CDistance* d, bool kmeanspp=false, EKMeansMethod f=KMM_LLOYD);
80 
87  CKMeans(int32_t k_i, CDistance* d_i, SGMatrix<float64_t> centers_i, EKMeansMethod f=KMM_LLOYD);
88  virtual ~CKMeans();
89 
90 
92 
93 
98 
104  virtual bool load(FILE* srcfile);
105 
111  virtual bool save(FILE* dstfile);
112 
117  void set_k(int32_t p_k);
118 
123  int32_t get_k();
124 
129  void set_use_kmeanspp(bool kmpp);
130 
135  bool get_use_kmeanspp() const;
136 
141  void set_fixed_centers(bool fixed);
142 
147  bool get_fixed_centers();
148 
153  void set_max_iter(int32_t iter);
154 
160 
166 
172 
177  int32_t get_dimensions();
178 
180  virtual const char* get_name() const { return "KMeans"; }
181 
186  virtual void set_initial_centers(SGMatrix<float64_t> centers);
187 
193 
199 
204  void set_mbKMeans_batch_size(int32_t b);
205 
210  int32_t get_mbKMeans_batch_size() const;
211 
216  void set_mbKMeans_iter(int32_t t);
217 
222  int32_t get_mbKMeans_iter() const;
223 
229  void set_mbKMeans_params(int32_t b, int32_t t);
230 
231  private:
240  virtual bool train_machine(CFeatures* data=NULL);
241 
243  virtual void store_model_features();
244 
245  virtual bool train_require_labels() const { return false; }
246 
251  SGMatrix<float64_t> kmeanspp();
252  void init();
253 
258  void set_random_centers(SGVector<float64_t> weights_set, SGVector<int32_t> ClList, int32_t XSize);
259  void set_initial_centers(SGVector<float64_t> weights_set,
260  SGVector<int32_t> ClList, int32_t XSize);
261  void compute_cluster_variances();
262 
263  private:
265  int32_t max_iter;
266 
268  bool fixed_centers;
269 
271  int32_t k;
272 
274  int32_t dimensions;
275 
277  SGVector<float64_t> R;
278 
280  SGMatrix<float64_t> mus_initial;
281 
283  bool use_kmeanspp;
284 
286  int32_t batch_size;
287 
289  int32_t minib_iter;
290 
292  SGMatrix<float64_t> mus;
293 
295  EKMeansMethod train_method;
296 };
297 }
298 #endif
299 

SHOGUN Machine Learning Toolbox - Documentation