source: branches/0.4-stable/yat/classifier/Target.h @ 1392

Last change on this file since 1392 was 1392, checked in by Peter, 13 years ago

trac has moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 4.6 KB
Line 
1#ifndef _theplu_yat_classifier_target_
2#define _theplu_yat_classifier_target_
3
4// $Id$
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2008 Peter Johansson
11
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 2 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with this program; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27  02111-1307, USA.
28*/
29
30#include "yat/utility/Exception.h"
31
32#include <iosfwd>
33#include <map>
34#include <stdexcept>
35#include <string>
36#include <vector>
37
38namespace theplu {
39namespace yat {
40  namespace utility {
41    class Index;
42  }
43namespace classifier { 
44
45  ///
46  /// @brief Class for containing sample labels.
47  ///
48  class Target
49  {
50 
51  public:
52    /**
53      \brief default constructor
54    */
55    Target(void);
56
57    ///
58    /// @brief Constructor creating target with @a labels
59    ///
60    explicit Target(const std::vector<std::string>& labels);
61
62    /**
63       @brief Constructor
64    */
65    Target(const std::vector<std::string>& labels, const Target&);
66
67    ///
68    /// @brief istream constructor.
69    ///
70    Target(std::istream&, char sep='\0') 
71      throw (utility::IO_error,std::exception);
72
73    ///
74    /// Constructor creating a sub-Target from Target @a org. @a vec
75    /// defines which indices to use.
76    ///
77    /// @note class is preserved, i.e., operator() returns the same
78    /// for the Target as the original Target.
79    ///
80    Target(const Target& org, const utility::Index& vec);
81
82    ///
83    /// @brief Destructor
84    ///
85    ~Target();
86
87    ///
88    /// @return a map with label as key and class as value.
89    ///
90    const std::map<std::string,size_t>& classes(void) const;
91   
92    /**
93       This function is equivalent to Target::classes().size()
94       
95       \return number of classes
96       
97       \note there might be empty classes, i.e., classes with zero
98       samples. This may happen when using
99       Target(const std::vector<std::string>& labels, const Target&) or
100       Target(const Target& org, const std::vector<size_t>& vec).
101    */
102    size_t nof_classes(void) const;
103
104    ///
105    /// @brief Default binary is set to false for all classes except
106    /// class 0.
107    ///
108    /// @return binary target for sample @a i
109    ///
110    /// @see set_binary
111    ///
112    bool binary(size_t i) const;
113   
114    ///
115    /// The size of returned vector is equal to number of classes. To
116    /// get label label of sample i you need to call
117    /// target.labels()[target(i)]
118    ///
119    /// @return vector of labels for classes
120    ///
121    const std::vector<std::string>& labels(void);
122
123    ///
124    /// Binary target for each sample with class @a i is set to @a
125    /// b. Default is binary set to false for each class except class
126    /// 0 which is set to true.
127    ///
128    void set_binary(size_t i, bool b);
129
130    ///
131    /// @brief randomize labels
132    ///
133    /// Randomizes classes. Number of samples with a specific label is
134    /// not modified, neither mapping from label to class.
135    ///
136    void random_shuffle(void);
137
138    ///
139    /// @return number of samples
140    ///
141    size_t size(void) const;
142
143    ///
144    /// @return number of samples with label @a label
145    ///
146    size_t size(const std::string& label) const;
147
148    ///
149    /// @return number of samples with class @a cl
150    ///
151    size_t size(size_t cl) const;
152
153
154    ///
155    /// @return the class of @a sample
156    ///
157    size_t operator()(size_t sample) const;
158   
159    ///
160    /// @return the class of @a sample
161    ///
162    size_t operator[](size_t sample) const;
163
164    ///
165    /// @brief assignment operator
166    ///
167    const Target& operator=(const Target&);
168
169  private:
170    /// @brief Copy Constructor
171    // using compiler generated constructor
172    //Target(const Target& other);
173
174    // binary target for class i
175    std::vector<char> binary_; // avoid using vector<bool>
176    std::vector<size_t> classes_; // class of sample i
177    // map between class label and class index (inverse of labels_)
178    std::map<std::string,size_t> class_map_; 
179    std::vector<std::string> labels_; // label of class i
180   
181    void init(const std::vector<std::string>&);
182
183  }; 
184 
185  ///
186  /// The output operator for the Target class.
187  ///
188  std::ostream& operator<<(std::ostream&, const Target& );
189
190}}} // of namespace classifier, yat, and theplu
191
192#endif
Note: See TracBrowser for help on using the repository browser.