474 |
22 Dec 05 |
peter |
// $Id$ |
474 |
22 Dec 05 |
peter |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
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 |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2012, 2017, 2021 Peter Johansson |
474 |
22 Dec 05 |
peter |
9 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
11 |
|
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 |
16 |
|
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 |
21 |
|
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 |
24 |
*/ |
675 |
10 Oct 06 |
jari |
25 |
|
2881 |
18 Nov 12 |
peter |
26 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
27 |
|
680 |
11 Oct 06 |
jari |
28 |
#include "Target.h" |
675 |
10 Oct 06 |
jari |
29 |
#include "yat/random/random.h" |
1134 |
23 Feb 08 |
peter |
30 |
#include "yat/utility/Index.h" |
675 |
10 Oct 06 |
jari |
31 |
#include "yat/utility/stl_utility.h" |
675 |
10 Oct 06 |
jari |
32 |
#include "yat/utility/utility.h" |
675 |
10 Oct 06 |
jari |
33 |
|
591 |
24 Aug 06 |
peter |
34 |
#include <algorithm> |
509 |
18 Feb 06 |
peter |
35 |
#include <cassert> |
509 |
18 Feb 06 |
peter |
36 |
#include <iostream> |
509 |
18 Feb 06 |
peter |
37 |
#include <map> |
509 |
18 Feb 06 |
peter |
38 |
#include <string> |
474 |
22 Dec 05 |
peter |
39 |
#include <sstream> |
474 |
22 Dec 05 |
peter |
40 |
#include <utility> |
474 |
22 Dec 05 |
peter |
41 |
#include <vector> |
474 |
22 Dec 05 |
peter |
42 |
|
474 |
22 Dec 05 |
peter |
43 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
44 |
namespace yat { |
474 |
22 Dec 05 |
peter |
45 |
namespace classifier { |
474 |
22 Dec 05 |
peter |
46 |
|
1100 |
18 Feb 08 |
peter |
47 |
Target::Target(void) |
1100 |
18 Feb 08 |
peter |
48 |
{ |
1100 |
18 Feb 08 |
peter |
49 |
} |
1100 |
18 Feb 08 |
peter |
50 |
|
4050 |
04 Mar 21 |
peter |
51 |
|
509 |
18 Feb 06 |
peter |
52 |
Target::Target(const std::vector<std::string>& label) |
474 |
22 Dec 05 |
peter |
53 |
{ |
509 |
18 Feb 06 |
peter |
54 |
init(label); |
474 |
22 Dec 05 |
peter |
55 |
} |
860 |
07 Sep 07 |
peter |
56 |
|
4050 |
04 Mar 21 |
peter |
57 |
|
860 |
07 Sep 07 |
peter |
58 |
Target::Target(const std::vector<std::string>& label, |
860 |
07 Sep 07 |
peter |
59 |
const Target& t) |
514 |
20 Feb 06 |
peter |
60 |
: class_map_(t.class_map_) |
474 |
22 Dec 05 |
peter |
61 |
{ |
520 |
22 Feb 06 |
peter |
62 |
binary_=t.binary_; |
860 |
07 Sep 07 |
peter |
63 |
labels_ = t.labels_; |
860 |
07 Sep 07 |
peter |
64 |
classes_.reserve(label.size()); |
4050 |
04 Mar 21 |
peter |
65 |
for (std::vector<std::string>::const_iterator lab=label.begin(); |
860 |
07 Sep 07 |
peter |
66 |
lab!=label.end(); ++lab) { |
860 |
07 Sep 07 |
peter |
67 |
std::map<std::string,size_t>::iterator i=class_map_.lower_bound(*lab); |
860 |
07 Sep 07 |
peter |
68 |
if (i->first==*lab) // label exists |
860 |
07 Sep 07 |
peter |
69 |
classes_.push_back(i->second); |
860 |
07 Sep 07 |
peter |
70 |
else { |
860 |
07 Sep 07 |
peter |
71 |
binary_.push_back(0); |
860 |
07 Sep 07 |
peter |
72 |
classes_.push_back(labels_.size()); |
860 |
07 Sep 07 |
peter |
73 |
class_map_.insert(i, std::make_pair(*lab, labels_.size())); |
860 |
07 Sep 07 |
peter |
74 |
labels_.push_back(*lab); |
860 |
07 Sep 07 |
peter |
75 |
} |
509 |
18 Feb 06 |
peter |
76 |
} |
474 |
22 Dec 05 |
peter |
77 |
} |
474 |
22 Dec 05 |
peter |
78 |
|
482 |
02 Jan 06 |
peter |
79 |
|
3845 |
13 Sep 19 |
peter |
80 |
Target::Target(std::istream& is, char sep) |
474 |
22 Dec 05 |
peter |
81 |
{ |
509 |
18 Feb 06 |
peter |
82 |
std::vector<std::string> vec; |
509 |
18 Feb 06 |
peter |
83 |
std::string word; |
509 |
18 Feb 06 |
peter |
84 |
bool ok=true; |
509 |
18 Feb 06 |
peter |
85 |
while(ok) { |
509 |
18 Feb 06 |
peter |
86 |
if(sep=='\0') |
3579 |
16 Jan 17 |
peter |
87 |
ok=!(is>>word).fail(); |
4050 |
04 Mar 21 |
peter |
88 |
else |
3579 |
16 Jan 17 |
peter |
89 |
ok=!getline(is, word, sep).fail(); |
509 |
18 Feb 06 |
peter |
90 |
|
509 |
18 Feb 06 |
peter |
// ignore empty words |
509 |
18 Feb 06 |
peter |
92 |
if (!word.size() || !ok ) |
509 |
18 Feb 06 |
peter |
93 |
continue; |
4050 |
04 Mar 21 |
peter |
94 |
|
509 |
18 Feb 06 |
peter |
95 |
vec.push_back(word); |
474 |
22 Dec 05 |
peter |
96 |
} |
474 |
22 Dec 05 |
peter |
97 |
|
474 |
22 Dec 05 |
peter |
// manipulate the state of the stream to be good |
474 |
22 Dec 05 |
peter |
99 |
is.clear(std::ios::goodbit); |
474 |
22 Dec 05 |
peter |
100 |
|
509 |
18 Feb 06 |
peter |
101 |
init(vec); |
474 |
22 Dec 05 |
peter |
102 |
} |
860 |
07 Sep 07 |
peter |
103 |
|
4050 |
04 Mar 21 |
peter |
104 |
|
4050 |
04 Mar 21 |
peter |
105 |
Target::Target(const Target& t, |
1134 |
23 Feb 08 |
peter |
106 |
const utility::Index& index) |
860 |
07 Sep 07 |
peter |
107 |
: class_map_(t.class_map_) |
860 |
07 Sep 07 |
peter |
108 |
{ |
860 |
07 Sep 07 |
peter |
109 |
binary_=t.binary_; |
860 |
07 Sep 07 |
peter |
110 |
classes_.resize(index.size()); |
860 |
07 Sep 07 |
peter |
111 |
for (size_t i=0; i<index.size(); i++) { |
860 |
07 Sep 07 |
peter |
112 |
assert(index[i]<t.size()); |
860 |
07 Sep 07 |
peter |
113 |
classes_[i]=t.classes_[index[i]]; |
860 |
07 Sep 07 |
peter |
114 |
} |
860 |
07 Sep 07 |
peter |
115 |
labels_ = t.labels_; |
860 |
07 Sep 07 |
peter |
116 |
} |
860 |
07 Sep 07 |
peter |
117 |
|
860 |
07 Sep 07 |
peter |
118 |
|
714 |
22 Dec 06 |
jari |
119 |
bool Target::binary(size_t i) const |
714 |
22 Dec 06 |
jari |
120 |
{ |
714 |
22 Dec 06 |
jari |
121 |
assert(i<size()); |
714 |
22 Dec 06 |
jari |
122 |
return binary_[operator()(i)]; |
714 |
22 Dec 06 |
jari |
123 |
} |
714 |
22 Dec 06 |
jari |
124 |
|
714 |
22 Dec 06 |
jari |
125 |
const std::map<std::string,size_t>& Target::classes(void) const |
714 |
22 Dec 06 |
jari |
126 |
{ |
714 |
22 Dec 06 |
jari |
127 |
return class_map_; |
714 |
22 Dec 06 |
jari |
128 |
} |
714 |
22 Dec 06 |
jari |
129 |
|
4050 |
04 Mar 21 |
peter |
130 |
void Target::init(const std::vector<std::string>& label) |
505 |
02 Feb 06 |
markus |
131 |
{ |
509 |
18 Feb 06 |
peter |
132 |
classes_.clear(); |
509 |
18 Feb 06 |
peter |
133 |
classes_.reserve(label.size()); |
509 |
18 Feb 06 |
peter |
134 |
class_map_.clear(); |
509 |
18 Feb 06 |
peter |
135 |
labels_.clear(); |
4050 |
04 Mar 21 |
peter |
136 |
|
509 |
18 Feb 06 |
peter |
137 |
for (size_t i=0; i<label.size(); i++) { |
4050 |
04 Mar 21 |
peter |
138 |
std::map<std::string,size_t>::iterator iter = |
509 |
18 Feb 06 |
peter |
139 |
class_map_.lower_bound(label[i]); |
474 |
22 Dec 05 |
peter |
140 |
|
509 |
18 Feb 06 |
peter |
// label in map |
4050 |
04 Mar 21 |
peter |
142 |
if (iter != class_map_.end() && |
509 |
18 Feb 06 |
peter |
143 |
!(class_map_.key_comp()(label[i],iter->first)) ){ |
482 |
02 Jan 06 |
peter |
144 |
|
509 |
18 Feb 06 |
peter |
145 |
classes_.push_back(iter->second); |
509 |
18 Feb 06 |
peter |
146 |
} |
509 |
18 Feb 06 |
peter |
// label not found in map |
509 |
18 Feb 06 |
peter |
148 |
else{ |
509 |
18 Feb 06 |
peter |
149 |
classes_.push_back(class_map_.size()); |
4050 |
04 Mar 21 |
peter |
150 |
class_map_.insert(iter, std::make_pair(label[i],classes_[i])); |
509 |
18 Feb 06 |
peter |
151 |
labels_.push_back(label[i]); |
509 |
18 Feb 06 |
peter |
152 |
} |
509 |
18 Feb 06 |
peter |
153 |
} |
514 |
20 Feb 06 |
peter |
// setting binary to false for every class |
520 |
22 Feb 06 |
peter |
155 |
binary_=std::vector<char>(nof_classes(),false); |
4050 |
04 Mar 21 |
peter |
156 |
|
514 |
20 Feb 06 |
peter |
157 |
set_binary(0,true); |
505 |
02 Feb 06 |
markus |
158 |
} |
505 |
02 Feb 06 |
markus |
159 |
|
4051 |
04 Mar 21 |
peter |
160 |
const std::vector<std::string>& Target::labels(void) const |
714 |
22 Dec 06 |
jari |
161 |
{ |
714 |
22 Dec 06 |
jari |
162 |
return labels_; |
714 |
22 Dec 06 |
jari |
163 |
} |
714 |
22 Dec 06 |
jari |
164 |
|
991 |
14 Nov 07 |
peter |
165 |
size_t Target::nof_classes(void) const |
714 |
22 Dec 06 |
jari |
166 |
{ |
714 |
22 Dec 06 |
jari |
167 |
return classes().size(); |
714 |
22 Dec 06 |
jari |
168 |
} |
714 |
22 Dec 06 |
jari |
169 |
|
591 |
24 Aug 06 |
peter |
170 |
void Target::random_shuffle(void) |
591 |
24 Aug 06 |
peter |
171 |
{ |
1004 |
23 Jan 08 |
peter |
172 |
random::random_shuffle(classes_.begin(), classes_.end()); |
591 |
24 Aug 06 |
peter |
173 |
} |
591 |
24 Aug 06 |
peter |
174 |
|
714 |
22 Dec 06 |
jari |
175 |
void Target::set_binary(size_t i, bool b) |
714 |
22 Dec 06 |
jari |
176 |
{ |
714 |
22 Dec 06 |
jari |
177 |
assert(i<nof_classes()); |
714 |
22 Dec 06 |
jari |
178 |
binary_[i]=b; |
714 |
22 Dec 06 |
jari |
179 |
} |
714 |
22 Dec 06 |
jari |
180 |
|
4050 |
04 Mar 21 |
peter |
181 |
size_t Target::size(const std::string& label) const |
581 |
04 May 06 |
markus |
182 |
{ |
4050 |
04 Mar 21 |
peter |
183 |
std::map<std::string,size_t>::const_iterator i=class_map_.find(label); |
581 |
04 May 06 |
markus |
184 |
if(i==class_map_.end()) |
581 |
04 May 06 |
markus |
185 |
return 0; |
581 |
04 May 06 |
markus |
186 |
assert(i->first==label); |
581 |
04 May 06 |
markus |
187 |
return size(i->second); |
581 |
04 May 06 |
markus |
188 |
} |
4050 |
04 Mar 21 |
peter |
189 |
|
714 |
22 Dec 06 |
jari |
190 |
size_t Target::size(void) const |
714 |
22 Dec 06 |
jari |
191 |
{ |
714 |
22 Dec 06 |
jari |
192 |
return classes_.size(); |
714 |
22 Dec 06 |
jari |
193 |
} |
714 |
22 Dec 06 |
jari |
194 |
|
990 |
14 Nov 07 |
peter |
195 |
size_t Target::size(size_t cl) const |
581 |
04 May 06 |
markus |
196 |
{ |
581 |
04 May 06 |
markus |
197 |
return std::count(classes_.begin(),classes_.end(),cl); |
581 |
04 May 06 |
markus |
198 |
} |
581 |
04 May 06 |
markus |
199 |
|
714 |
22 Dec 06 |
jari |
200 |
size_t Target::operator()(size_t sample) const |
714 |
22 Dec 06 |
jari |
201 |
{ |
714 |
22 Dec 06 |
jari |
202 |
assert(sample<size()); |
714 |
22 Dec 06 |
jari |
203 |
return classes_[sample]; |
714 |
22 Dec 06 |
jari |
204 |
} |
581 |
04 May 06 |
markus |
205 |
|
714 |
22 Dec 06 |
jari |
206 |
size_t Target::operator[](size_t sample) const |
474 |
22 Dec 05 |
peter |
207 |
{ |
714 |
22 Dec 06 |
jari |
208 |
return this->operator()(sample); |
474 |
22 Dec 05 |
peter |
209 |
} |
474 |
22 Dec 05 |
peter |
210 |
|
663 |
29 Sep 06 |
peter |
211 |
|
714 |
22 Dec 06 |
jari |
212 |
std::ostream& operator<<(std::ostream& s, const Target& a) |
714 |
22 Dec 06 |
jari |
213 |
{ |
714 |
22 Dec 06 |
jari |
214 |
for (size_t j = 0; j < a.size(); ++j) { |
714 |
22 Dec 06 |
jari |
215 |
s << a(j); |
714 |
22 Dec 06 |
jari |
216 |
if ( (j+1)<a.size() ) |
714 |
22 Dec 06 |
jari |
217 |
s << " "; |
714 |
22 Dec 06 |
jari |
218 |
} |
714 |
22 Dec 06 |
jari |
219 |
|
714 |
22 Dec 06 |
jari |
220 |
return s; |
714 |
22 Dec 06 |
jari |
221 |
} |
714 |
22 Dec 06 |
jari |
222 |
|
714 |
22 Dec 06 |
jari |
223 |
|
680 |
11 Oct 06 |
jari |
224 |
}}} // of namespace classifier, yat, and theplu |