SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
KNN.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) 2006 Christian Gehl
00008  * Written (W) 1999-2009 Soeren Sonnenburg
00009  * Written (W) 2011 Sergey Lisitsyn
00010  * Written (W) 2012 Fernando José Iglesias García, cover tree support
00011  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
00012  */
00013 
00014 #ifndef _KNN_H__
00015 #define _KNN_H__
00016 
00017 #include <stdio.h>
00018 #include <shogun/lib/common.h>
00019 #include <shogun/io/SGIO.h>
00020 #include <shogun/features/Features.h>
00021 #include <shogun/distance/Distance.h>
00022 #include <shogun/machine/DistanceMachine.h>
00023 
00024 namespace shogun
00025 {
00026 
00027 class CDistanceMachine;
00028 
00055 class CKNN : public CDistanceMachine
00056 {
00057     public:
00058         MACHINE_PROBLEM_TYPE(PT_MULTICLASS)
00059 
00060         
00061         CKNN();
00062 
00069         CKNN(int32_t k, CDistance* d, CLabels* trainlab);
00070         virtual ~CKNN();
00071 
00076         virtual EMachineType get_classifier_type() { return CT_KNN; }
00077 
00087         SGMatrix<index_t> nearest_neighbors();
00088 
00094         virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL);
00095 
00097         virtual float64_t apply_one(int32_t vec_idx)
00098         {
00099             SG_ERROR("for performance reasons use apply() instead of apply(int32_t vec_idx)\n")
00100             return 0;
00101         }
00102 
00106         SGMatrix<int32_t> classify_for_multiple_k();
00107 
00113         virtual bool load(FILE* srcfile);
00114 
00120         virtual bool save(FILE* dstfile);
00121 
00126         inline void set_k(int32_t k)
00127         {
00128             ASSERT(k>0)
00129             m_k=k;
00130         }
00131 
00136         inline int32_t get_k()
00137         {
00138             return m_k;
00139         }
00140 
00144         inline void set_q(float64_t q)
00145         {
00146             ASSERT(q<=1.0 && q>0.0)
00147             m_q = q;
00148         }
00149 
00153         inline float64_t get_q() { return m_q; }
00154 
00158         inline void set_use_covertree(bool use_covertree)
00159         {
00160             m_use_covertree = use_covertree;
00161         }
00162 
00166         inline bool get_use_covertree() const { return m_use_covertree; }
00167 
00169         virtual const char* get_name() const { return "KNN"; }
00170 
00171     protected:
00176         virtual void store_model_features();
00177 
00181         virtual CMulticlassLabels* classify_NN();
00182 
00186         void init_distance(CFeatures* data);
00187 
00196         virtual bool train_machine(CFeatures* data=NULL);
00197 
00198     private:
00199         void init();
00200 
00213         int32_t choose_class(float64_t* classes, int32_t* train_lab);
00214 
00227         void choose_class_for_multiple_k(int32_t* output, int32_t* classes, int32_t* train_lab, int32_t step);
00228 
00229     protected:
00231         int32_t m_k;
00232 
00234         float64_t m_q;
00235 
00237         bool m_use_covertree;
00238 
00240         int32_t m_num_classes;
00241 
00243         int32_t m_min_label;
00244 
00246         SGVector<int32_t> m_train_labels;
00247 };
00248 
00249 }
00250 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation