00001 /*!@file Learn/DecisionTree.H Decision Tree Classifier */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Dan Parks <danielfp@usc.edu> 00033 // $HeadURL$ 00034 // $Id$ 00035 // 00036 00037 #ifndef DECISIONTREE_H_DEFINED 00038 #define DECISIONTREE_H_DEFINED 00039 00040 #include "rutz/shared_ptr.h" 00041 #include <cstdlib> 00042 #include <deque> 00043 #include <map> 00044 #include <list> 00045 #include <vector> 00046 #include <iostream> 00047 #include <fstream> 00048 00049 class DecisionNode; // Forward declaration 00050 00051 class DecisionTree 00052 { 00053 public: 00054 DecisionTree(int maxSplits=1); 00055 // Train a tree on the data, limited by the maximum allowed splits 00056 void train(const std::vector<std::vector<float> >& data, const std::vector<int>& labels,std::vector<float> weights=std::vector<float>()); 00057 // Predict the result for this binary classifier on the given data using the given weights on the nodes 00058 std::vector<int> predict(const std::vector<std::vector<float> >& data, std::vector<float> weights=std::vector<float>()); 00059 void printTree(); 00060 std::deque<rutz::shared_ptr<DecisionNode> > getNodes(); 00061 void addNode(rutz::shared_ptr<DecisionNode> node); 00062 private: 00063 size_t itsMaxSplits; 00064 std::deque<rutz::shared_ptr<DecisionNode> > itsNodes; 00065 }; 00066 00067 // One node within a decision tree 00068 class DecisionNode 00069 { 00070 public: 00071 DecisionNode(); 00072 std::vector<int> decide(const std::vector<std::vector<float> >& data); 00073 float split(const std::vector<std::vector<float> >& data, const std::vector<int>& labels, const std::vector<float>& weights, rutz::shared_ptr<DecisionNode>& left, rutz::shared_ptr<DecisionNode>& right, const rutz::shared_ptr<DecisionNode> parent=rutz::shared_ptr<DecisionNode>(NULL)); 00074 size_t getDim(); 00075 bool isValid(); 00076 int printNode(std::string& output, int depth=0); 00077 void writeNode(std::ostream& outstream, bool needEnd=true); 00078 rutz::shared_ptr<DecisionNode> readNode(std::istream& instream); 00079 void setDim(size_t dim); 00080 void setLeaf(bool isLeaf); 00081 void setParent(rutz::shared_ptr<DecisionNode> parent); 00082 void setLeftConstraint(float constraint); 00083 void setRightConstraint(float constraint); 00084 void setClass(int classId); 00085 int getClass(); 00086 private: 00087 int itsDim; 00088 //float itsThreshold; 00089 //int itsSign; 00090 bool itsLeaf; 00091 float itsLeftConstraint; 00092 float itsRightConstraint; 00093 int itsClass; 00094 // Parent node to the current node 00095 rutz::shared_ptr<DecisionNode> itsParent; 00096 }; 00097 00098 00099 #endif 00100 00101 // ###################################################################### 00102 /* So things look consistent in everyone's emacs... */ 00103 /* Local Variables: */ 00104 /* indent-tabs-mode: nil */ 00105 /* End: */ 00106 00107