KMeans.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2008 Gunnar Raetsch
00008  * Written (W) 2007-2008 Soeren Sonnenburg
00009  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #ifndef _KMEANS_H__
00013 #define _KMEANS_H__
00014 
00015 #include <stdio.h>
00016 #include "lib/common.h"
00017 #include "lib/io.h"
00018 #include "features/RealFeatures.h"
00019 #include "distance/Distance.h"
00020 #include "distance/DistanceMachine.h"
00021 
00022 class CDistanceMachine;
00023 
00025 class CKMeans : public CDistanceMachine
00026 {
00027     public:
00029         CKMeans();
00030 
00036         CKMeans(INT k, CDistance* d);
00037         virtual ~CKMeans();
00038 
00043         virtual inline EClassifierType get_classifier_type() { return CT_KMEANS; }
00044 
00049         virtual bool train();
00050 
00056         virtual bool load(FILE* srcfile);
00057 
00063         virtual bool save(FILE* dstfile);
00064 
00069         inline void set_k(INT p_k)
00070         {
00071             ASSERT(p_k>0);
00072             this->k=p_k;
00073         }
00074 
00079         inline INT get_k()
00080         {
00081             return k;
00082         }
00083 
00088         inline void set_max_iter(INT iter)
00089         {
00090             ASSERT(iter>0);
00091             max_iter=iter;
00092         }
00093 
00098         inline DREAL get_max_iter()
00099         {
00100             return max_iter;
00101         }
00102 
00108         inline void get_radi(DREAL*& radi, INT& num)
00109         {
00110             radi=R;
00111             num=k;
00112         }
00113 
00120         inline void get_centers(DREAL*& centers, INT& dim, INT& num)
00121         {
00122             centers=mus;
00123             dim=dimensions;
00124             num=k;
00125         }
00126 
00132         inline void get_radiuses(DREAL** radii, INT* num)
00133         {
00134             size_t sz=sizeof(*R)*k;
00135             *radii=(DREAL*) malloc(sz);
00136             ASSERT(*radii);
00137 
00138             memcpy(*radii, R, sz);
00139             *num=k;
00140         }
00141 
00148         inline void get_cluster_centers(DREAL** centers, INT* dim, INT* num)
00149         {
00150             size_t sz=sizeof(*mus)*dimensions*k;
00151             *centers=(DREAL*) malloc(sz);
00152             ASSERT(*centers);
00153 
00154             memcpy(*centers, mus, sz);
00155             *dim=dimensions;
00156             *num=k;
00157         }
00158 
00163         inline INT get_dimensions()
00164         {
00165             return dimensions;
00166         }
00167 
00168 
00169     protected:
00180         void sqdist(double * x, CRealFeatures* y, double *z,
00181                 int n1, int offs, int n2, int m);
00182 
00188         void clustknb(bool use_old_mus, double *mus_start);
00189 
00190     protected:
00192         INT max_iter;
00193 
00195         INT k;
00196 
00198         INT dimensions;
00199 
00201         DREAL* R;
00202         
00204         DREAL* mus;
00205 
00206     private:
00208         DREAL* Weights;
00209 };
00210 #endif
00211 

SHOGUN Machine Learning Toolbox - Documentation