1 | // $Id: Target.h 514 2006-02-20 09:45:34Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_classifier_target_ |
---|
4 | #define _theplu_classifier_target_ |
---|
5 | |
---|
6 | #include <c++_tools/utility/Exception.h> |
---|
7 | |
---|
8 | #include <cassert> |
---|
9 | #include <map> |
---|
10 | #include <string> |
---|
11 | #include <vector> |
---|
12 | |
---|
13 | |
---|
14 | namespace theplu { |
---|
15 | namespace classifier { |
---|
16 | |
---|
17 | /// |
---|
18 | /// Class for targets |
---|
19 | /// |
---|
20 | |
---|
21 | class Target |
---|
22 | { |
---|
23 | |
---|
24 | public: |
---|
25 | /// |
---|
26 | /// @brief Constructor creating target with @a labels |
---|
27 | /// |
---|
28 | Target(const std::vector<std::string>& labels); |
---|
29 | |
---|
30 | /// |
---|
31 | /// @brief istream constructor. |
---|
32 | /// |
---|
33 | Target(std::istream&, char sep='\0') |
---|
34 | throw (utility::IO_error,std::exception); |
---|
35 | |
---|
36 | /// |
---|
37 | /// @brief Copy Constructor |
---|
38 | /// |
---|
39 | //inline Target(const Target& other) |
---|
40 | // : classes_(other.classes_), class_map_(other.class_map_), |
---|
41 | // labels_(other.labels_) {} |
---|
42 | |
---|
43 | /// |
---|
44 | /// Constructor creating a sub-Target from Target @a org. @a vec |
---|
45 | /// defines which indices to use. |
---|
46 | /// |
---|
47 | /// @note class is preserved, i.e., operator() returns the same |
---|
48 | /// for the Target as the original Target. |
---|
49 | /// |
---|
50 | Target(const Target& org, const std::vector<size_t>& vec); |
---|
51 | |
---|
52 | /// |
---|
53 | /// @brief Destructor |
---|
54 | /// |
---|
55 | ~Target(); |
---|
56 | |
---|
57 | /// |
---|
58 | /// @return a map with label as key and class as value. |
---|
59 | /// |
---|
60 | inline const std::map<std::string,size_t>& classes(void) const |
---|
61 | { return class_map_; } |
---|
62 | |
---|
63 | /// |
---|
64 | /// This function is equivalent to Target::classes().size() |
---|
65 | /// |
---|
66 | /// @return number of classes |
---|
67 | /// |
---|
68 | inline const size_t nof_classes(void) const { return classes().size(); } |
---|
69 | |
---|
70 | /// |
---|
71 | /// Default binary is set to false for all classes except class 0 |
---|
72 | /// |
---|
73 | /// @return true if class of sample @a i is set to true |
---|
74 | /// |
---|
75 | /// @see set_binary |
---|
76 | /// |
---|
77 | inline bool binary(const size_t i) const |
---|
78 | { assert(i<size()); return binary_[i]; } |
---|
79 | |
---|
80 | /// |
---|
81 | /// Class @a target is set to @a b. Default is binary set to false |
---|
82 | /// for each class. |
---|
83 | /// |
---|
84 | void set_binary(const size_t target, const bool b); |
---|
85 | |
---|
86 | /// |
---|
87 | /// @return number of samples |
---|
88 | /// |
---|
89 | inline size_t size(void) const { return classes_.size(); } |
---|
90 | |
---|
91 | /// |
---|
92 | /// @return number of samples with label @a label |
---|
93 | /// |
---|
94 | const size_t size(const std::string& label) const; |
---|
95 | |
---|
96 | /// |
---|
97 | /// @return the class of @a sample |
---|
98 | /// |
---|
99 | inline size_t operator()(const size_t sample) const |
---|
100 | { assert(sample<size()); return classes_[sample]; } |
---|
101 | |
---|
102 | |
---|
103 | private: |
---|
104 | std::vector<short> binary_; // avoid using vector<bool> |
---|
105 | std::vector<size_t> classes_; // class of sample i |
---|
106 | std::map<std::string,size_t> class_map_; |
---|
107 | std::vector<std::string> labels_; // label of class i |
---|
108 | |
---|
109 | void init(const std::vector<std::string>&); |
---|
110 | |
---|
111 | }; |
---|
112 | |
---|
113 | /// |
---|
114 | /// The output operator for the Target class. |
---|
115 | /// |
---|
116 | std::ostream& operator<<(std::ostream&, const Target& ); |
---|
117 | |
---|
118 | }} // of namespace classifier and namespace theplu |
---|
119 | |
---|
120 | #endif |
---|