PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // KNNClassifier.h 00004 // 00005 // Copyright (C) 2004 Nicolas Chapados 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: KNNClassifier.h 9703 2008-11-20 19:36:41Z plearner $ 00037 ******************************************************* */ 00038 00039 // Authors: Nicolas Chapados 00040 00044 #ifndef KNNClassifier_INC 00045 #define KNNClassifier_INC 00046 00047 #include <plearn_learners/generic/PLearner.h> 00048 #include <plearn_learners/nearest_neighbors/GenericNearestNeighbors.h> 00049 #include <plearn/ker/Kernel.h> 00050 00051 namespace PLearn { 00052 00094 class KNNClassifier: public PLearner 00095 { 00096 typedef PLearner inherited; 00097 00098 protected: 00100 mutable Vec knn_output; 00101 00103 mutable Vec knn_costs; 00104 00106 mutable Vec class_weights; 00107 00114 mutable Mat multi_k_output; 00115 00117 mutable Vec multi_k_input; 00118 00119 public: 00120 //##### Public Build Options ############################################ 00121 00124 PP<GenericNearestNeighbors> knn; 00125 00127 int nclasses; 00128 00130 int kmin; 00131 00134 real kmult; 00135 00138 real kpow; 00139 00142 bool use_knn_costs_as_weights; 00143 00147 00152 Ker kernel; 00153 00157 TVec<int> multi_k; 00158 00159 public: 00160 //##### Object Methods ################################################## 00161 00163 KNNClassifier(); 00164 00166 virtual void build(); 00167 00169 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00170 00171 // Declares other standard object methods. 00172 // If your class is not instantiatable (it has pure virtual methods) 00173 // you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT. 00174 PLEARN_DECLARE_OBJECT(KNNClassifier); 00175 00176 public: 00177 //##### PLearner Methods #################################################### 00178 00180 virtual void setTrainingSet(VMat training_set, bool call_forget=true); 00181 00184 virtual int outputsize() const; 00185 00187 virtual void forget(); 00188 00190 virtual void train(); 00191 00193 virtual void computeOutput(const Vec& input, Vec& output) const; 00194 00196 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00197 const Vec& target, Vec& costs) const; 00198 00201 virtual TVec<std::string> getTestCostNames() const; 00202 00205 virtual TVec<std::string> getTrainCostNames() const; 00206 00207 private: 00209 void build_(); 00210 00211 protected: 00213 static void declareOptions(OptionList& ol); 00214 }; 00215 00216 // Declares a few other classes and functions related to this class. 00217 DECLARE_OBJECT_PTR(KNNClassifier); 00218 00219 } // end of namespace PLearn 00220 00221 #endif 00222 00223 00224 /* 00225 Local Variables: 00226 mode:c++ 00227 c-basic-offset:4 00228 c-file-style:"stroustrup" 00229 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00230 indent-tabs-mode:nil 00231 fill-column:79 00232 End: 00233 */ 00234 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :