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

Last change on this file since 575 was 520, checked in by Peter, 17 years ago

changed internal representation in Target

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: Target.h 520 2006-02-22 15:01:10Z 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
14namespace theplu {
15namespace 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 binary target for sample @a i
74    ///
75    /// @see set_binary
76    ///
77    inline bool binary(const size_t i) const 
78    { assert(i<size()); return binary_[operator()(i)]; }
79   
80    ///
81    /// Binary target for each sample with class @a i is set to @a
82    /// b. Default is binary set to false for each class except class
83    /// 0 which is set to true.
84    ///
85    inline void set_binary(const size_t i, const bool b) 
86    { assert(i<nof_classes()); binary_[i]=b; }
87
88    ///
89    /// @return number of samples
90    ///
91    inline size_t size(void) const { return classes_.size(); }
92
93    ///
94    /// @return number of samples with label @a label
95    ///
96    const size_t size(const std::string& label) const;
97
98    ///
99    /// @return the class of @a sample
100    ///
101    inline size_t operator()(const size_t sample) const 
102    { assert(sample<size()); return classes_[sample]; }
103
104
105  private:
106    // binary target for class i
107    std::vector<char> binary_; // avoid using vector<bool>
108    std::vector<size_t> classes_; // class of sample i
109    std::map<std::string,size_t> class_map_; 
110    std::vector<std::string> labels_; // label of class i
111   
112    void init(const std::vector<std::string>&);
113
114  }; 
115 
116  ///
117  /// The output operator for the Target class.
118  ///
119  std::ostream& operator<<(std::ostream&, const Target& );
120
121}} // of namespace classifier and namespace theplu
122
123#endif
Note: See TracBrowser for help on using the repository browser.