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

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

Fixes #170. Almost all inlines removed, some classes have no cc file.

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