X-Boost  2.3.8
MultiClassDecisionStump.h
Go to the documentation of this file.
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _MULTICLASS_DECISIONSTUMP_H
22 #define _MULTICLASS_DECISIONSTUMP_H
23 
29 #include <string>
30 #include <iostream>
31 #include <vector>
32 
34 template<class DataType>
36 
38  DataType th;
39 
42  std::vector<int> responses;
43 public:
44 
46  MultiClassDecisionStumpSingleThreshold(int n_classes) : responses(n_classes) { }
47 
48  static std::string signature() {
49  return "mdecisionstumpst";
50  }
51 
53  inline void evaluate_feature(int *response, DataType value) const
54  {
55  if(value > th)
56  {
57  for(int k =0; k<responses.size(); ++k)
58  response[k] = responses[k];
59  }
60  else
61  {
62  for(int k =0; k<responses.size(); ++k)
63  response[k] = -responses[k];
64  }
65  }
66 
67 };
68 
71 template<class DataType>
73 
76  std::vector<std::pair<DataType, int> > responses;
77 public:
78 
80  MultiClassDecisionStumpMultiThreshold(int n_classes) : responses(n_classes) { }
81 
82  static std::string signature() {
83  return "mdecisionstumpmt";
84  }
85 
87  inline void evaluate_feature(int *response, DataType value) const
88  {
89  for(int k =0; k<responses.size(); ++k)
90  if(value > responses[k].first) // TODO: parity
91  {
92  response[k] = responses[k].second;
93  }
94  else
95  {
96  response[k] = -responses[k].second;
97  }
98  }
99 
100 };
101 
102 
103 template<class DataType>
104 inline std::istream & operator >> (std::istream & in, MultiClassDecisionStumpSingleThreshold<DataType> & s)
105 {
106  in >> s.th;
107  for(int i = 0;i<s.responses.size(); ++i)
108  in >> s.responses[i];
109 
110  return in;
111 }
112 
113 template<class DataType>
114 inline std::ostream & operator << (std::ostream & out, const MultiClassDecisionStumpSingleThreshold<DataType> & s)
115 {
116  out << s.th;
117 
118  for(int i = 0;i<s.responses.size(); ++i)
119  out << ' ' << s.responses[i];
120 
121  return out;
122 }
123 
124 template<class DataType>
125 inline std::istream & operator >> (std::istream & in, MultiClassDecisionStumpMultiThreshold<DataType> & s)
126 {
127  for(int i = 0;i<s.responses.size(); ++i)
128  in >> s.responses[i].first >> s.responses[i].second;
129 
130  return in;
131 }
132 
133 template<class DataType>
134 inline std::ostream & operator << (std::ostream & out, const MultiClassDecisionStumpMultiThreshold<DataType> & s)
135 {
136  for(int i = 0;i<s.responses.size(); ++i)
137  {
138  if(i!=0)
139  out << ' ';
140  out << s.responses[i].first << ' ' << s.responses[i].second;
141  }
142 
143  return out;
144 }
145 
146 #endif
std::vector< int > responses
Definition: MultiClassDecisionStump.h:42
void evaluate_feature(int *response, DataType value) const
convert the feature value to {-1,+1} using internal threshold
Definition: MultiClassDecisionStump.h:53
Definition: MultiClassDecisionStump.h:72
DataType th
threshold value
Definition: MultiClassDecisionStump.h:38
MultiClassDecisionStumpMultiThreshold(int n_classes)
initialize the vector memory
Definition: MultiClassDecisionStump.h:80
A simple decision stump for multiclass problem using only one threshold for all problems.
Definition: MultiClassDecisionStump.h:35
void evaluate_feature(int *response, DataType value) const
convert the feature value to {-1,+1} using internal threshold
Definition: MultiClassDecisionStump.h:87
std::vector< std::pair< DataType, int > > responses
Definition: MultiClassDecisionStump.h:76
MultiClassDecisionStumpSingleThreshold(int n_classes)
initialize the vector memory
Definition: MultiClassDecisionStump.h:46