source: trunk/c++_tools/classifier/Target.h @ 663

Last change on this file since 663 was 663, checked in by Peter, 15 years ago

added assignment operator in Target

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.5 KB
Line 
1// $Id$
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#include <iostream>
14
15namespace theplu {
16namespace classifier { 
17
18  ///
19  /// Class for targets
20  ///
21
22  class Target
23  {
24 
25  public:
26    ///
27    /// @brief Constructor creating target with @a labels
28    ///
29    explicit Target(const std::vector<std::string>& labels);
30
31    ///
32    /// @brief istream constructor.
33    ///
34    Target(std::istream&, char sep='\0') 
35      throw (utility::IO_error,std::exception);
36
37    ///
38    /// @brief Copy Constructor
39    ///
40    //inline Target(const Target& other)
41    //  : classes_(other.classes_), class_map_(other.class_map_),
42    //    labels_(other.labels_) {}
43
44    ///
45    /// Constructor creating a sub-Target from Target @a org. @a vec
46    /// defines which indices to use.
47    ///
48    /// @note class is preserved, i.e., operator() returns the same
49    /// for the Target as the original Target.
50    ///
51    Target(const Target& org, const std::vector<size_t>& vec);
52
53    ///
54    /// @brief Destructor
55    ///
56    ~Target();
57
58    ///
59    /// @return a map with label as key and class as value.
60    ///
61    inline const std::map<std::string,size_t>& classes(void) const 
62    { return class_map_; }
63   
64    ///
65    /// This function is equivalent to Target::classes().size()
66    ///
67    /// @return number of classes
68    ///
69    inline const size_t nof_classes(void) const { return classes().size(); }
70
71    ///
72    /// Default binary is set to false for all classes except class 0.
73    ///
74    /// @return binary target for sample @a i
75    ///
76    /// @see set_binary
77    ///
78    inline bool binary(const size_t i) const 
79    { assert(i<size()); return binary_[operator()(i)]; }
80   
81    ///
82    /// @return vector of labels for classes
83    ///
84    inline const std::vector<std::string>& labels() { return labels_; }
85
86    ///
87    /// Binary target for each sample with class @a i is set to @a
88    /// b. Default is binary set to false for each class except class
89    /// 0 which is set to true.
90    ///
91    inline void set_binary(const size_t i, const bool b) 
92    { assert(i<nof_classes()); binary_[i]=b; }
93
94    ///
95    /// @brief randomize labels
96    ///
97    /// Randomizes classes. Number of samples with a specific label is
98    /// not modified, neither mapping from label to class.
99    ///
100    void random_shuffle(void);
101
102    ///
103    /// @return number of samples
104    ///
105    inline size_t size(void) const { return classes_.size(); }
106
107    ///
108    /// @return number of samples with label @a label
109    ///
110    const size_t size(const std::string& label) const;
111
112    ///
113    /// @return number of samples with class @a cl
114    ///
115    const size_t size(size_t cl) const;
116
117
118    ///
119    /// @return the class of @a sample
120    ///
121    inline size_t operator()(const size_t sample) const 
122    { assert(sample<size()); return classes_[sample]; }
123   
124    ///
125    /// @return the class of @a sample
126    ///
127    inline size_t operator[](const size_t sample) const 
128    { return this->operator()(sample); }
129
130    ///
131    /// @brief assignment operator
132    ///
133    const Target& operator=(const Target&);
134
135  private:
136    // binary target for class i
137    std::vector<char> binary_; // avoid using vector<bool>
138    std::vector<size_t> classes_; // class of sample i
139    // map between class label and class index (inverse of labels_)
140    std::map<std::string,size_t> class_map_; 
141    std::vector<std::string> labels_; // label of class i
142   
143    void init(const std::vector<std::string>&);
144
145  }; 
146 
147  ///
148  /// The output operator for the Target class.
149  ///
150  std::ostream& operator<<(std::ostream&, const Target& );
151
152}} // of namespace classifier and namespace theplu
153
154#endif
Note: See TracBrowser for help on using the repository browser.