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-2009 Soeren Sonnenburg 00009 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00010 */ 00011 00012 #ifndef _KMEANS_H__ 00013 #define _KMEANS_H__ 00014 00015 #include <stdio.h> 00016 #include <shogun/lib/common.h> 00017 #include <shogun/io/SGIO.h> 00018 #include <shogun/features/SimpleFeatures.h> 00019 #include <shogun/distance/Distance.h> 00020 #include <shogun/machine/DistanceMachine.h> 00021 00022 namespace shogun 00023 { 00024 class CDistanceMachine; 00025 00039 class CKMeans : public CDistanceMachine 00040 { 00041 public: 00043 CKMeans(); 00044 00050 CKMeans(int32_t k, CDistance* d); 00051 virtual ~CKMeans(); 00052 00057 virtual inline EClassifierType get_classifier_type() { return CT_KMEANS; } 00058 00064 virtual bool load(FILE* srcfile); 00065 00071 virtual bool save(FILE* dstfile); 00072 00077 void set_k(int32_t p_k); 00078 00083 int32_t get_k(); 00084 00089 void set_max_iter(int32_t iter); 00090 00095 float64_t get_max_iter(); 00096 00101 SGVector<float64_t> get_radiuses(); 00102 00107 SGMatrix<float64_t> get_cluster_centers(); 00108 00113 int32_t get_dimensions(); 00114 00116 inline virtual const char* get_name() const { return "KMeans"; } 00117 00118 protected: 00124 void clustknb(bool use_old_mus, float64_t *mus_start); 00125 00134 virtual bool train_machine(CFeatures* data=NULL); 00135 00137 virtual void store_model_features(); 00138 00139 private: 00140 void init(); 00141 00142 protected: 00144 int32_t max_iter; 00145 00147 int32_t k; 00148 00150 int32_t dimensions; 00151 00153 SGVector<float64_t> R; 00154 00155 private: 00156 /* temporary variable for weighting over the train data */ 00157 SGVector<float64_t> Weights; 00158 00159 /* temp variable for cluster centers */ 00160 SGMatrix<float64_t> mus; 00161 00162 }; 00163 } 00164 #endif 00165