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) 2006 Christian Gehl 00008 * Written (W) 1999-2009 Soeren Sonnenburg 00009 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00010 */ 00011 00012 #ifndef _KNN_H__ 00013 #define _KNN_H__ 00014 00015 #include <stdio.h> 00016 #include "lib/common.h" 00017 #include "lib/io.h" 00018 #include "features/Features.h" 00019 #include "distance/Distance.h" 00020 #include "classifier/DistanceMachine.h" 00021 00022 namespace shogun 00023 { 00024 class CDistanceMachine; 00025 00039 class CKNN : public CDistanceMachine 00040 { 00041 public: 00043 CKNN(); 00044 00051 CKNN(int32_t k, CDistance* d, CLabels* trainlab); 00052 virtual ~CKNN(); 00053 00058 virtual inline EClassifierType get_classifier_type() { return CT_KNN; } 00059 //inline EDistanceType get_distance_type() { return DT_KNN;} 00060 00069 virtual bool train(CFeatures* data=NULL); 00070 00075 virtual CLabels* classify(); 00076 00082 virtual CLabels* classify(CFeatures* data); 00083 00085 virtual float64_t classify_example(int32_t vec_idx) 00086 { 00087 SG_ERROR( "for performance reasons use classify() instead of classify_example\n"); 00088 return 0; 00089 } 00090 00097 void classify_for_multiple_k(int32_t** output, int32_t* num_vec, int32_t* k_out); 00098 00104 virtual bool load(FILE* srcfile); 00105 00111 virtual bool save(FILE* dstfile); 00112 00117 inline void set_k(int32_t p_k) 00118 { 00119 ASSERT(p_k>0); 00120 this->k=p_k; 00121 } 00122 00127 inline int32_t get_k() 00128 { 00129 return k; 00130 } 00131 00133 inline virtual const char* get_name() const { return "KNN"; } 00134 00135 protected: 00137 int32_t k; 00138 00140 int32_t num_classes; 00141 00143 int32_t min_label; 00144 00146 int32_t num_train_labels; 00147 00149 int32_t* train_labels; 00150 }; 00151 } 00152 #endif