1 | // $Id: Target.h 509 2006-02-18 13:47:32Z 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 | /// @return true if class of sample @a i is equal to variable one |
---|
72 | /// |
---|
73 | /// @see set_one(const size_t one) |
---|
74 | /// |
---|
75 | inline bool one(const size_t i) const |
---|
76 | { assert(i<size()); return classes_[i]==one_; } |
---|
77 | |
---|
78 | /// |
---|
79 | /// Function to set variable one. This variable is used in 2-class |
---|
80 | /// algorithm in rder to use the Targer object in a one-versus-all |
---|
81 | /// manner. |
---|
82 | /// |
---|
83 | /// @see one(const size_t i) |
---|
84 | /// |
---|
85 | inline void set_one(const size_t one) { assert(one<size()); one_=one; } |
---|
86 | |
---|
87 | /// |
---|
88 | /// @return number of samples |
---|
89 | /// |
---|
90 | inline size_t size(void) const { return classes_.size(); } |
---|
91 | |
---|
92 | /// |
---|
93 | /// @return number of samples with label @a label |
---|
94 | /// |
---|
95 | const size_t size(const std::string& label) const; |
---|
96 | |
---|
97 | // |
---|
98 | //@brief sort |
---|
99 | // |
---|
100 | //inline void sort(void) { std::sort(classes_.begin(), classes_.end()); } |
---|
101 | |
---|
102 | /// |
---|
103 | /// @return the class of @a sample |
---|
104 | /// |
---|
105 | inline size_t operator()(const size_t sample) const |
---|
106 | { assert(sample<size()); return classes_[sample]; } |
---|
107 | |
---|
108 | /// |
---|
109 | /// @brief assignment operator |
---|
110 | /// |
---|
111 | //const Target& operator=(const Target& other); |
---|
112 | |
---|
113 | private: |
---|
114 | std::vector<size_t> classes_; // class of sample i |
---|
115 | std::map<std::string,size_t> class_map_; |
---|
116 | std::vector<std::string> labels_; // label of class i |
---|
117 | size_t one_; |
---|
118 | |
---|
119 | void init(const std::vector<std::string>&); |
---|
120 | |
---|
121 | }; |
---|
122 | |
---|
123 | /// |
---|
124 | /// The output operator for the Target class. |
---|
125 | /// |
---|
126 | std::ostream& operator<<(std::ostream&, const Target& ); |
---|
127 | |
---|
128 | }} // of namespace classifier and namespace theplu |
---|
129 | |
---|
130 | #endif |
---|