1 | #ifndef _theplu_yat_classifier_target_ |
---|
2 | #define _theplu_yat_classifier_target_ |
---|
3 | |
---|
4 | // $Id$ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005 Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
9 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
10 | |
---|
11 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
12 | |
---|
13 | The yat library is free software; you can redistribute it and/or |
---|
14 | modify it under the terms of the GNU General Public License as |
---|
15 | published by the Free Software Foundation; either version 2 of the |
---|
16 | License, or (at your option) any later version. |
---|
17 | |
---|
18 | The yat library is distributed in the hope that it will be useful, |
---|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with this program; if not, write to the Free Software |
---|
25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
26 | 02111-1307, USA. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "yat/utility/Exception.h" |
---|
30 | |
---|
31 | #include <iosfwd> |
---|
32 | #include <map> |
---|
33 | #include <stdexcept> |
---|
34 | #include <string> |
---|
35 | #include <vector> |
---|
36 | |
---|
37 | namespace theplu { |
---|
38 | namespace yat { |
---|
39 | namespace classifier { |
---|
40 | |
---|
41 | /// |
---|
42 | /// @brief Class for containing sample labels. |
---|
43 | /// |
---|
44 | |
---|
45 | class Target |
---|
46 | { |
---|
47 | |
---|
48 | public: |
---|
49 | /// |
---|
50 | /// @brief Constructor creating target with @a labels |
---|
51 | /// |
---|
52 | explicit Target(const std::vector<std::string>& labels); |
---|
53 | |
---|
54 | /// |
---|
55 | /// @brief istream constructor. |
---|
56 | /// |
---|
57 | Target(std::istream&, char sep='\0') |
---|
58 | throw (utility::IO_error,std::exception); |
---|
59 | |
---|
60 | /// |
---|
61 | /// @brief Copy Constructor |
---|
62 | /// |
---|
63 | //Target(const Target& other) |
---|
64 | // : classes_(other.classes_), class_map_(other.class_map_), |
---|
65 | // labels_(other.labels_) {} |
---|
66 | |
---|
67 | /// |
---|
68 | /// Constructor creating a sub-Target from Target @a org. @a vec |
---|
69 | /// defines which indices to use. |
---|
70 | /// |
---|
71 | /// @note class is preserved, i.e., operator() returns the same |
---|
72 | /// for the Target as the original Target. |
---|
73 | /// |
---|
74 | Target(const Target& org, const std::vector<size_t>& vec); |
---|
75 | |
---|
76 | /// |
---|
77 | /// @brief Destructor |
---|
78 | /// |
---|
79 | ~Target(); |
---|
80 | |
---|
81 | /// |
---|
82 | /// @return a map with label as key and class as value. |
---|
83 | /// |
---|
84 | const std::map<std::string,size_t>& classes(void) const; |
---|
85 | |
---|
86 | /// |
---|
87 | /// This function is equivalent to Target::classes().size() |
---|
88 | /// |
---|
89 | /// @return number of classes |
---|
90 | /// |
---|
91 | const size_t nof_classes(void) const; |
---|
92 | |
---|
93 | /// |
---|
94 | /// @brief Default binary is set to false for all classes except |
---|
95 | /// class 0. |
---|
96 | /// |
---|
97 | /// @return binary target for sample @a i |
---|
98 | /// |
---|
99 | /// @see set_binary |
---|
100 | /// |
---|
101 | bool binary(size_t i) const; |
---|
102 | |
---|
103 | /// |
---|
104 | /// The size of returned vector is equal to number of classes. To |
---|
105 | /// get label label of sample i you need to call |
---|
106 | /// target.labels()[target(i)] |
---|
107 | /// |
---|
108 | /// @return vector of labels for classes |
---|
109 | /// |
---|
110 | const std::vector<std::string>& labels(void); |
---|
111 | |
---|
112 | /// |
---|
113 | /// Binary target for each sample with class @a i is set to @a |
---|
114 | /// b. Default is binary set to false for each class except class |
---|
115 | /// 0 which is set to true. |
---|
116 | /// |
---|
117 | void set_binary(size_t i, bool b); |
---|
118 | |
---|
119 | /// |
---|
120 | /// @brief randomize labels |
---|
121 | /// |
---|
122 | /// Randomizes classes. Number of samples with a specific label is |
---|
123 | /// not modified, neither mapping from label to class. |
---|
124 | /// |
---|
125 | void random_shuffle(void); |
---|
126 | |
---|
127 | /// |
---|
128 | /// @return number of samples |
---|
129 | /// |
---|
130 | size_t size(void) const; |
---|
131 | |
---|
132 | /// |
---|
133 | /// @return number of samples with label @a label |
---|
134 | /// |
---|
135 | const size_t size(const std::string& label) const; |
---|
136 | |
---|
137 | /// |
---|
138 | /// @return number of samples with class @a cl |
---|
139 | /// |
---|
140 | const size_t size(size_t cl) const; |
---|
141 | |
---|
142 | |
---|
143 | /// |
---|
144 | /// @return the class of @a sample |
---|
145 | /// |
---|
146 | size_t operator()(size_t sample) const; |
---|
147 | |
---|
148 | /// |
---|
149 | /// @return the class of @a sample |
---|
150 | /// |
---|
151 | size_t operator[](size_t sample) const; |
---|
152 | |
---|
153 | /// |
---|
154 | /// @brief assignment operator |
---|
155 | /// |
---|
156 | const Target& operator=(const Target&); |
---|
157 | |
---|
158 | private: |
---|
159 | // binary target for class i |
---|
160 | std::vector<char> binary_; // avoid using vector<bool> |
---|
161 | std::vector<size_t> classes_; // class of sample i |
---|
162 | // map between class label and class index (inverse of labels_) |
---|
163 | std::map<std::string,size_t> class_map_; |
---|
164 | std::vector<std::string> labels_; // label of class i |
---|
165 | |
---|
166 | void init(const std::vector<std::string>&); |
---|
167 | |
---|
168 | }; |
---|
169 | |
---|
170 | /// |
---|
171 | /// The output operator for the Target class. |
---|
172 | /// |
---|
173 | std::ostream& operator<<(std::ostream&, const Target& ); |
---|
174 | |
---|
175 | }}} // of namespace classifier, yat, and theplu |
---|
176 | |
---|
177 | #endif |
---|