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