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

Last change on this file since 1883 was 1883, checked in by Peter, 12 years ago

refs #512. added relates and relatesalso tags on some free functions in namespace classifier.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 4.7 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, 2008 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2009 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28#include "yat/utility/deprecate.h"
29#include "yat/utility/Exception.h"
30
31#include <iosfwd>
32#include <map>
33#include <stdexcept>
34#include <string>
35#include <vector>
36
37namespace theplu {
38namespace yat {
39  namespace utility {
40    class Index;
41  }
42namespace classifier { 
43
44  ///
45  /// @brief Class for containing sample labels.
46  ///
47  class Target
48  {
49 
50  public:
51    /**
52      \brief default constructor
53    */
54    Target(void);
55
56    ///
57    /// @brief Constructor creating target with @a labels
58    ///
59    explicit Target(const std::vector<std::string>& labels);
60
61    /**
62       @brief Constructor
63    */
64    Target(const std::vector<std::string>& labels, const Target&);
65
66    ///
67    /// @brief istream constructor.
68    ///
69    Target(std::istream&, char sep='\0') 
70      throw (utility::IO_error,std::exception);
71
72    ///
73    /// Constructor creating a sub-Target from Target @a org. @a vec
74    /// defines which indices to use.
75    ///
76    /// @note class is preserved, i.e., operator() returns the same
77    /// for the Target as the original Target.
78    ///
79    Target(const Target& org, const utility::Index& vec);
80
81    ///
82    /// @brief Destructor
83    ///
84    ~Target();
85
86    ///
87    /// @return a map with label as key and class as value.
88    ///
89    const std::map<std::string,size_t>& classes(void) const;
90   
91    /**
92       This function is equivalent to Target::classes().size()
93       
94       \return number of classes
95       
96       \note there might be empty classes, i.e., classes with zero
97       samples. This may happen when using
98       Target(const std::vector<std::string>& labels, const Target&) or
99       Target(const Target& org, const std::vector<size_t>& vec).
100    */
101    size_t nof_classes(void) const;
102
103    ///
104    /// @brief Default binary is set to false for all classes except
105    /// class 0.
106    ///
107    /// @return binary target for sample @a i
108    ///
109    /// @see set_binary
110    ///
111    bool binary(size_t i) const;
112   
113    ///
114    /// The size of returned vector is equal to number of classes. To
115    /// get label of sample \a i you need to call
116    /// target.labels()[target(i)]
117    ///
118    /// @return vector of labels for classes
119    ///
120    const std::vector<std::string>& labels(void);
121
122    ///
123    /// Binary target for each sample with class @a i is set to @a
124    /// b. Default is binary set to false for each class except class
125    /// 0 which is set to true.
126    ///
127    void set_binary(size_t i, bool b);
128
129    ///
130    /// @brief randomize labels
131    ///
132    /// Randomizes classes. Number of samples with a specific label is
133    /// not modified, neither mapping from label to class.
134    ///
135    void random_shuffle(void);
136
137    ///
138    /// @return number of samples
139    ///
140    size_t size(void) const;
141
142    ///
143    /// @return number of samples with label @a label
144    ///
145    size_t size(const std::string& label) const;
146
147    ///
148    /// @return number of samples with class @a cl
149    ///
150    size_t size(size_t cl) const;
151
152
153    ///
154    /// @return the class of @a sample
155    ///
156    size_t operator()(size_t sample) const;
157   
158    ///
159    /// @return the class of @a sample
160    ///
161    /// \deprecated Provided for backward compatibility with the 0.4
162    /// API. Use operator().
163    ///
164    size_t operator[](size_t sample) const YAT_DEPRECATE;
165
166    ///
167    /// @brief assignment operator
168    ///
169    const Target& operator=(const Target&);
170
171  private:
172    /// @brief Copy Constructor
173    // using compiler generated constructor
174    //Target(const Target& other);
175
176    // binary target for class i
177    std::vector<char> binary_; // avoid using vector<bool>
178    std::vector<size_t> classes_; // class of sample i
179    // map between class label and class index (inverse of labels_)
180    std::map<std::string,size_t> class_map_; 
181    std::vector<std::string> labels_; // label of class i
182   
183    void init(const std::vector<std::string>&);
184
185  }; 
186 
187  ///
188  /// The output operator for the Target class.
189  ///
190  /// \relates Target
191  ///
192  std::ostream& operator<<(std::ostream&, const Target& );
193
194}}} // of namespace classifier, yat, and theplu
195
196#endif
Note: See TracBrowser for help on using the repository browser.