source: trunk/yat/classifier/Target.h

Last change on this file was 4051, checked in by Peter, 7 months ago

make Target::classifier() const; closes #972

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