source: trunk/yat/classifier/Target.h

Last change on this file was 4245, checked in by Peter, 2 weeks ago

GCC -Wdeprecated-copy complains when some but not all of copy ctor, dtor and operator= are user-implemented. When the implementation is identical with the one generated by the compiler, there is no reason to keep the code. DataWeightProxy? is a different story (see #986)

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