source: trunk/yat/classifier/Target.h @ 706

Last change on this file since 706 was 706, checked in by Jari Häkkinen, 15 years ago

Addresses #65 and #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 4.3 KB
Line 
1#ifndef _theplu_yat_classifier_target_
2#define _theplu_yat_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 <iosfwd>
31#include <map>
32#include <string>
33#include <vector>
34
35namespace theplu {
36namespace yat {
37namespace 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    /// @brief Default binary is set to false for all classes except
94    /// class 0.
95    ///
96    /// @return binary target for sample @a i
97    ///
98    /// @see set_binary
99    ///
100    inline bool binary(const size_t i) const { 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) { binary_[i]=b; }
113
114    ///
115    /// @brief randomize labels
116    ///
117    /// Randomizes classes. Number of samples with a specific label is
118    /// not modified, neither mapping from label to class.
119    ///
120    void random_shuffle(void);
121
122    ///
123    /// @return number of samples
124    ///
125    inline size_t size(void) const { return classes_.size(); }
126
127    ///
128    /// @return number of samples with label @a label
129    ///
130    const size_t size(const std::string& label) const;
131
132    ///
133    /// @return number of samples with class @a cl
134    ///
135    const size_t size(size_t cl) const;
136
137
138    ///
139    /// @return the class of @a sample
140    ///
141    inline size_t operator()(const size_t sample) const 
142    { return classes_[sample]; }
143   
144    ///
145    /// @return the class of @a sample
146    ///
147    inline size_t operator[](const size_t sample) const 
148    { return this->operator()(sample); }
149
150    ///
151    /// @brief assignment operator
152    ///
153    const Target& operator=(const Target&);
154
155  private:
156    // binary target for class i
157    std::vector<char> binary_; // avoid using vector<bool>
158    std::vector<size_t> classes_; // class of sample i
159    // map between class label and class index (inverse of labels_)
160    std::map<std::string,size_t> class_map_; 
161    std::vector<std::string> labels_; // label of class i
162   
163    void init(const std::vector<std::string>&);
164
165  }; 
166 
167  ///
168  /// The output operator for the Target class.
169  ///
170  std::ostream& operator<<(std::ostream&, const Target& );
171
172}}} // of namespace classifier, yat, and theplu
173
174#endif
Note: See TracBrowser for help on using the repository browser.