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

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

Fixed #include statemenst and forward declarations. asserts in headers removed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 4.1 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 <iosfwd>
30#include <map>
31#include <stdexcept>
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    //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    const std::map<std::string,size_t>& classes(void) const;
83   
84    ///
85    /// This function is equivalent to Target::classes().size()
86    ///
87    /// @return number of classes
88    ///
89    const size_t nof_classes(void) const;
90
91    ///
92    /// @brief Default binary is set to false for all classes except
93    /// class 0.
94    ///
95    /// @return binary target for sample @a i
96    ///
97    /// @see set_binary
98    ///
99    bool binary(size_t i) const;
100   
101    ///
102    /// The size of returned vector is equal to number of classes. To
103    /// get label label of sample i you need to call
104    /// target.labels()[target(i)]
105    ///
106    /// @return vector of labels for classes
107    ///
108    const std::vector<std::string>& labels(void);
109
110    ///
111    /// Binary target for each sample with class @a i is set to @a
112    /// b. Default is binary set to false for each class except class
113    /// 0 which is set to true.
114    ///
115    void set_binary(size_t i, bool b);
116
117    ///
118    /// @brief randomize labels
119    ///
120    /// Randomizes classes. Number of samples with a specific label is
121    /// not modified, neither mapping from label to class.
122    ///
123    void random_shuffle(void);
124
125    ///
126    /// @return number of samples
127    ///
128    size_t size(void) const;
129
130    ///
131    /// @return number of samples with label @a label
132    ///
133    const size_t size(const std::string& label) const;
134
135    ///
136    /// @return number of samples with class @a cl
137    ///
138    const size_t size(size_t cl) const;
139
140
141    ///
142    /// @return the class of @a sample
143    ///
144    size_t operator()(size_t sample) const;
145   
146    ///
147    /// @return the class of @a sample
148    ///
149    size_t operator[](size_t sample) const;
150
151    ///
152    /// @brief assignment operator
153    ///
154    const Target& operator=(const Target&);
155
156  private:
157    // binary target for class i
158    std::vector<char> binary_; // avoid using vector<bool>
159    std::vector<size_t> classes_; // class of sample i
160    // map between class label and class index (inverse of labels_)
161    std::map<std::string,size_t> class_map_; 
162    std::vector<std::string> labels_; // label of class i
163   
164    void init(const std::vector<std::string>&);
165
166  }; 
167 
168  ///
169  /// The output operator for the Target class.
170  ///
171  std::ostream& operator<<(std::ostream&, const Target& );
172
173}}} // of namespace classifier, yat, and theplu
174
175#endif
Note: See TracBrowser for help on using the repository browser.