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

Last change on this file since 865 was 865, checked in by Peter, 14 years ago

changing URL to http://trac.thep.lu.se/trac/yat

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