4050 |
04 Mar 21 |
peter |
1 |
#ifndef _theplu_yat_classifier_target_ |
4050 |
04 Mar 21 |
peter |
2 |
#define _theplu_yat_classifier_target_ |
474 |
22 Dec 05 |
peter |
3 |
|
675 |
10 Oct 06 |
jari |
// $Id$ |
474 |
22 Dec 05 |
peter |
5 |
|
675 |
10 Oct 06 |
jari |
6 |
/* |
831 |
27 Mar 07 |
peter |
Copyright (C) 2005 Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2021 Peter Johansson |
675 |
10 Oct 06 |
jari |
11 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
13 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
18 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
23 |
|
675 |
10 Oct 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
675 |
10 Oct 06 |
jari |
26 |
*/ |
675 |
10 Oct 06 |
jari |
27 |
|
1500 |
15 Sep 08 |
peter |
28 |
#include "yat/utility/deprecate.h" |
675 |
10 Oct 06 |
jari |
29 |
#include "yat/utility/Exception.h" |
675 |
10 Oct 06 |
jari |
30 |
|
680 |
11 Oct 06 |
jari |
31 |
#include <iosfwd> |
505 |
02 Feb 06 |
markus |
32 |
#include <map> |
757 |
19 Feb 07 |
jari |
33 |
#include <stdexcept> |
509 |
18 Feb 06 |
peter |
34 |
#include <string> |
474 |
22 Dec 05 |
peter |
35 |
#include <vector> |
474 |
22 Dec 05 |
peter |
36 |
|
474 |
22 Dec 05 |
peter |
37 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
38 |
namespace yat { |
1134 |
23 Feb 08 |
peter |
39 |
namespace utility { |
1134 |
23 Feb 08 |
peter |
40 |
class Index; |
1134 |
23 Feb 08 |
peter |
41 |
} |
4050 |
04 Mar 21 |
peter |
42 |
namespace classifier { |
474 |
22 Dec 05 |
peter |
43 |
|
474 |
22 Dec 05 |
peter |
44 |
/// |
767 |
22 Feb 07 |
peter |
/// @brief Class for containing sample labels. |
474 |
22 Dec 05 |
peter |
46 |
/// |
474 |
22 Dec 05 |
peter |
47 |
class Target |
474 |
22 Dec 05 |
peter |
48 |
{ |
4050 |
04 Mar 21 |
peter |
49 |
|
474 |
22 Dec 05 |
peter |
50 |
public: |
1100 |
18 Feb 08 |
peter |
51 |
/** |
1100 |
18 Feb 08 |
peter |
\brief default constructor |
1100 |
18 Feb 08 |
peter |
53 |
*/ |
1100 |
18 Feb 08 |
peter |
54 |
Target(void); |
1100 |
18 Feb 08 |
peter |
55 |
|
474 |
22 Dec 05 |
peter |
56 |
/// |
509 |
18 Feb 06 |
peter |
/// @brief Constructor creating target with @a labels |
474 |
22 Dec 05 |
peter |
58 |
/// |
615 |
31 Aug 06 |
peter |
59 |
explicit Target(const std::vector<std::string>& labels); |
474 |
22 Dec 05 |
peter |
60 |
|
860 |
07 Sep 07 |
peter |
61 |
/** |
4050 |
04 Mar 21 |
peter |
@brief Constructor |
860 |
07 Sep 07 |
peter |
63 |
*/ |
860 |
07 Sep 07 |
peter |
64 |
Target(const std::vector<std::string>& labels, const Target&); |
860 |
07 Sep 07 |
peter |
65 |
|
474 |
22 Dec 05 |
peter |
66 |
/// |
509 |
18 Feb 06 |
peter |
/// @brief istream constructor. |
474 |
22 Dec 05 |
peter |
68 |
/// |
3845 |
13 Sep 19 |
peter |
69 |
Target(std::istream&, char sep='\0'); |
474 |
22 Dec 05 |
peter |
70 |
|
474 |
22 Dec 05 |
peter |
71 |
/// |
509 |
18 Feb 06 |
peter |
/// Constructor creating a sub-Target from Target @a org. @a vec |
509 |
18 Feb 06 |
peter |
/// defines which indices to use. |
509 |
18 Feb 06 |
peter |
74 |
/// |
509 |
18 Feb 06 |
peter |
/// @note class is preserved, i.e., operator() returns the same |
509 |
18 Feb 06 |
peter |
/// for the Target as the original Target. |
509 |
18 Feb 06 |
peter |
77 |
/// |
1134 |
23 Feb 08 |
peter |
78 |
Target(const Target& org, const utility::Index& vec); |
474 |
22 Dec 05 |
peter |
79 |
|
474 |
22 Dec 05 |
peter |
80 |
/// |
509 |
18 Feb 06 |
peter |
/// @return a map with label as key and class as value. |
505 |
02 Feb 06 |
markus |
82 |
/// |
714 |
22 Dec 06 |
jari |
83 |
const std::map<std::string,size_t>& classes(void) const; |
4050 |
04 Mar 21 |
peter |
84 |
|
860 |
07 Sep 07 |
peter |
85 |
/** |
860 |
07 Sep 07 |
peter |
This function is equivalent to Target::classes().size() |
4050 |
04 Mar 21 |
peter |
87 |
|
860 |
07 Sep 07 |
peter |
\return number of classes |
4050 |
04 Mar 21 |
peter |
89 |
|
860 |
07 Sep 07 |
peter |
\note there might be empty classes, i.e., classes with zero |
4050 |
04 Mar 21 |
peter |
samples. This may happen when using |
860 |
07 Sep 07 |
peter |
Target(const std::vector<std::string>& labels, const Target&) or |
860 |
07 Sep 07 |
peter |
Target(const Target& org, const std::vector<size_t>& vec). |
860 |
07 Sep 07 |
peter |
94 |
*/ |
991 |
14 Nov 07 |
peter |
95 |
size_t nof_classes(void) const; |
509 |
18 Feb 06 |
peter |
96 |
|
509 |
18 Feb 06 |
peter |
97 |
/// |
706 |
19 Dec 06 |
jari |
/// @brief Default binary is set to false for all classes except |
706 |
19 Dec 06 |
jari |
/// class 0. |
509 |
18 Feb 06 |
peter |
100 |
/// |
520 |
22 Feb 06 |
peter |
/// @return binary target for sample @a i |
509 |
18 Feb 06 |
peter |
102 |
/// |
514 |
20 Feb 06 |
peter |
/// @see set_binary |
514 |
20 Feb 06 |
peter |
104 |
/// |
714 |
22 Dec 06 |
jari |
105 |
bool binary(size_t i) const; |
4050 |
04 Mar 21 |
peter |
106 |
|
505 |
02 Feb 06 |
markus |
107 |
/// |
745 |
10 Feb 07 |
peter |
/// The size of returned vector is equal to number of classes. To |
1867 |
17 Mar 09 |
peter |
/// get label of sample \a i you need to call |
745 |
10 Feb 07 |
peter |
/// target.labels()[target(i)] |
745 |
10 Feb 07 |
peter |
111 |
/// |
581 |
04 May 06 |
markus |
/// @return vector of labels for classes |
581 |
04 May 06 |
markus |
113 |
/// |
4051 |
04 Mar 21 |
peter |
114 |
const std::vector<std::string>& labels(void) const; |
581 |
04 May 06 |
markus |
115 |
|
581 |
04 May 06 |
markus |
116 |
/// |
520 |
22 Feb 06 |
peter |
/// Binary target for each sample with class @a i is set to @a |
520 |
22 Feb 06 |
peter |
/// b. Default is binary set to false for each class except class |
520 |
22 Feb 06 |
peter |
/// 0 which is set to true. |
474 |
22 Dec 05 |
peter |
120 |
/// |
714 |
22 Dec 06 |
jari |
121 |
void set_binary(size_t i, bool b); |
474 |
22 Dec 05 |
peter |
122 |
|
474 |
22 Dec 05 |
peter |
123 |
/// |
592 |
24 Aug 06 |
peter |
/// @brief randomize labels |
592 |
24 Aug 06 |
peter |
125 |
/// |
592 |
24 Aug 06 |
peter |
/// Randomizes classes. Number of samples with a specific label is |
592 |
24 Aug 06 |
peter |
/// not modified, neither mapping from label to class. |
592 |
24 Aug 06 |
peter |
128 |
/// |
592 |
24 Aug 06 |
peter |
129 |
void random_shuffle(void); |
592 |
24 Aug 06 |
peter |
130 |
|
592 |
24 Aug 06 |
peter |
131 |
/// |
509 |
18 Feb 06 |
peter |
/// @return number of samples |
474 |
22 Dec 05 |
peter |
133 |
/// |
714 |
22 Dec 06 |
jari |
134 |
size_t size(void) const; |
474 |
22 Dec 05 |
peter |
135 |
|
509 |
18 Feb 06 |
peter |
136 |
/// |
509 |
18 Feb 06 |
peter |
/// @return number of samples with label @a label |
509 |
18 Feb 06 |
peter |
138 |
/// |
990 |
14 Nov 07 |
peter |
139 |
size_t size(const std::string& label) const; |
474 |
22 Dec 05 |
peter |
140 |
|
509 |
18 Feb 06 |
peter |
141 |
/// |
581 |
04 May 06 |
markus |
/// @return number of samples with class @a cl |
581 |
04 May 06 |
markus |
143 |
/// |
990 |
14 Nov 07 |
peter |
144 |
size_t size(size_t cl) const; |
581 |
04 May 06 |
markus |
145 |
|
581 |
04 May 06 |
markus |
146 |
|
581 |
04 May 06 |
markus |
147 |
/// |
509 |
18 Feb 06 |
peter |
/// @return the class of @a sample |
509 |
18 Feb 06 |
peter |
149 |
/// |
714 |
22 Dec 06 |
jari |
150 |
size_t operator()(size_t sample) const; |
4050 |
04 Mar 21 |
peter |
151 |
|
581 |
04 May 06 |
markus |
152 |
/// |
581 |
04 May 06 |
markus |
/// @return the class of @a sample |
581 |
04 May 06 |
markus |
154 |
/// |
1343 |
18 Jun 08 |
peter |
/// \deprecated Provided for backward compatibility with the 0.4 |
1343 |
18 Jun 08 |
peter |
/// API. Use operator(). |
1343 |
18 Jun 08 |
peter |
157 |
/// |
1500 |
15 Sep 08 |
peter |
158 |
size_t operator[](size_t sample) const YAT_DEPRECATE; |
474 |
22 Dec 05 |
peter |
159 |
|
474 |
22 Dec 05 |
peter |
160 |
private: |
4050 |
04 Mar 21 |
peter |
/// @brief Copy Constructor |
860 |
07 Sep 07 |
peter |
// using compiler generated constructor |
4050 |
04 Mar 21 |
peter |
//Target(const Target& other); |
860 |
07 Sep 07 |
peter |
164 |
|
520 |
22 Feb 06 |
peter |
// binary target for class i |
520 |
22 Feb 06 |
peter |
166 |
std::vector<char> binary_; // avoid using vector<bool> |
509 |
18 Feb 06 |
peter |
167 |
std::vector<size_t> classes_; // class of sample i |
592 |
24 Aug 06 |
peter |
// map between class label and class index (inverse of labels_) |
4050 |
04 Mar 21 |
peter |
169 |
std::map<std::string,size_t> class_map_; |
509 |
18 Feb 06 |
peter |
170 |
std::vector<std::string> labels_; // label of class i |
4050 |
04 Mar 21 |
peter |
171 |
|
509 |
18 Feb 06 |
peter |
172 |
void init(const std::vector<std::string>&); |
474 |
22 Dec 05 |
peter |
173 |
|
4050 |
04 Mar 21 |
peter |
174 |
}; |
4050 |
04 Mar 21 |
peter |
175 |
|
474 |
22 Dec 05 |
peter |
176 |
/// |
474 |
22 Dec 05 |
peter |
/// The output operator for the Target class. |
474 |
22 Dec 05 |
peter |
178 |
/// |
1883 |
31 Mar 09 |
peter |
/// \relates Target |
1883 |
31 Mar 09 |
peter |
180 |
/// |
474 |
22 Dec 05 |
peter |
181 |
std::ostream& operator<<(std::ostream&, const Target& ); |
474 |
22 Dec 05 |
peter |
182 |
|
680 |
11 Oct 06 |
jari |
183 |
}}} // of namespace classifier, yat, and theplu |
474 |
22 Dec 05 |
peter |
184 |
|
474 |
22 Dec 05 |
peter |
185 |
#endif |