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

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

fixes #313 - SVM constructor is void and passing kernel and target in train function instead

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