1 | // $Id$ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) The authors contributing to this file. |
---|
5 | |
---|
6 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 2 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "yat/classifier/MatrixLookupWeighted.h" |
---|
25 | |
---|
26 | #include "yat/utility/matrix.h" |
---|
27 | |
---|
28 | #ifndef NDEBUG |
---|
29 | #include <algorithm> |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <fstream> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace classifier { |
---|
36 | |
---|
37 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data, |
---|
38 | const utility::matrix& weights, |
---|
39 | const bool own) |
---|
40 | : DataLookup2D(own), data_(&data), weights_(&weights), |
---|
41 | ref_count_weights_(NULL) |
---|
42 | { |
---|
43 | assert(data.rows()==weights.rows()); |
---|
44 | assert(data.columns()==weights.columns()); |
---|
45 | for(size_t i=0;i<(*data_).rows();i++) |
---|
46 | row_index_.push_back(i); |
---|
47 | for(size_t i=0;i<(*data_).columns();i++) |
---|
48 | column_index_.push_back(i); |
---|
49 | } |
---|
50 | |
---|
51 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data) |
---|
52 | : DataLookup2D(), data_(&data) |
---|
53 | { |
---|
54 | utility::matrix weights; |
---|
55 | data_->nan(weights); |
---|
56 | weights_= new utility::matrix(weights); |
---|
57 | ref_count_weights_=new u_int(1); |
---|
58 | for(size_t i=0;i<(*data_).rows();i++) |
---|
59 | row_index_.push_back(i); |
---|
60 | for(size_t i=0;i<(*data_).columns();i++) |
---|
61 | column_index_.push_back(i); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data, |
---|
67 | const utility::matrix& weights, |
---|
68 | const std::vector<size_t>& row, |
---|
69 | const std::vector<size_t>& col) |
---|
70 | : DataLookup2D(row,col), data_(&data), weights_(&weights), |
---|
71 | ref_count_weights_(NULL) |
---|
72 | { |
---|
73 | // Checking that each row index is less than data.rows() |
---|
74 | assert(row.empty() || |
---|
75 | *(std::max_element(row.begin(),row.end()))<data.rows()); |
---|
76 | // Checking that each column index is less than data.column() |
---|
77 | assert(col.empty() || |
---|
78 | *(std::max_element(col.begin(),col.end()))<data.columns()); |
---|
79 | // Checking that each row index is less than weights.rows() |
---|
80 | assert(row.empty() || |
---|
81 | *(std::max_element(row.begin(),row.end()))<weights.rows()); |
---|
82 | // Checking that each column index is less than weights.column() |
---|
83 | assert(col.empty() || |
---|
84 | *(std::max_element(col.begin(),col.end()))<weights.columns()); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data, |
---|
90 | const utility::matrix& weights, |
---|
91 | const std::vector<size_t>& index, |
---|
92 | const bool row) |
---|
93 | : DataLookup2D(), data_(&data), weights_(&weights), |
---|
94 | ref_count_weights_(NULL) |
---|
95 | { |
---|
96 | if (row){ |
---|
97 | // Checking that each row index is less than data.rows() |
---|
98 | assert(index.empty() || |
---|
99 | *(std::max_element(index.begin(),index.end()))<data.rows()); |
---|
100 | // Checking that each row index is less than weights.rows() |
---|
101 | assert(index.empty() || |
---|
102 | *(std::max_element(index.begin(),index.end()))<weights.rows()); |
---|
103 | row_index_=index; |
---|
104 | assert(column_index_.empty()); |
---|
105 | column_index_.reserve(data.columns()); |
---|
106 | for (size_t i=0; i<data.columns(); i++) |
---|
107 | column_index_.push_back(i); |
---|
108 | } |
---|
109 | else{ |
---|
110 | // Checking that each column index is less than data.column() |
---|
111 | assert(index.empty() || |
---|
112 | *(std::max_element(index.begin(),index.end()))<data.columns()); |
---|
113 | // Checking that each column index is less than weights.column() |
---|
114 | assert(index.empty() || |
---|
115 | *(std::max_element(index.begin(),index.end()))<weights.columns()); |
---|
116 | column_index_=index; |
---|
117 | assert(row_index_.empty()); |
---|
118 | column_index_.reserve(data.rows()); |
---|
119 | for (size_t i=0; i<data.rows(); i++) |
---|
120 | row_index_.push_back(i); |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /* |
---|
126 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& dv, |
---|
127 | const MatrixLookup& wv) |
---|
128 | : DataLookup2D(dv), data_(dv.data_), weights_(dv.data_) |
---|
129 | { |
---|
130 | } |
---|
131 | */ |
---|
132 | |
---|
133 | |
---|
134 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other) |
---|
135 | : DataLookup2D(other), data_(other.data_), weights_(other.weights_) |
---|
136 | { |
---|
137 | ref_count_ = other.ref_count_; |
---|
138 | if (ref_count_) |
---|
139 | ++(*ref_count_); |
---|
140 | ref_count_weights_ = other.ref_count_weights_; |
---|
141 | if (ref_count_weights_) |
---|
142 | ++(*ref_count_weights_); |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other, |
---|
149 | const std::vector<size_t>& row, |
---|
150 | const std::vector<size_t>& col) |
---|
151 | : DataLookup2D(other,row,col), data_(other.data_), weights_(other.weights_) |
---|
152 | { |
---|
153 | ref_count_ = other.ref_count_; |
---|
154 | if (ref_count_) |
---|
155 | ++(*ref_count_); |
---|
156 | ref_count_weights_ = other.ref_count_weights_; |
---|
157 | if (ref_count_weights_) |
---|
158 | ++(*ref_count_weights_); |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other, |
---|
164 | const std::vector<size_t>& index, |
---|
165 | bool row) |
---|
166 | : DataLookup2D(other,index,row), data_(other.data_), |
---|
167 | weights_(other.weights_) |
---|
168 | { |
---|
169 | ref_count_ = other.ref_count_; |
---|
170 | if (ref_count_) |
---|
171 | ++(*ref_count_); |
---|
172 | ref_count_weights_ = other.ref_count_weights_; |
---|
173 | if (ref_count_weights_) |
---|
174 | ++(*ref_count_weights_); |
---|
175 | |
---|
176 | // Checking that no index is out of range |
---|
177 | assert(row_index_.empty() || |
---|
178 | *(max_element(row_index_.begin(), row_index_.end()))<data_->rows()); |
---|
179 | assert(column_index_.empty() || |
---|
180 | *(max_element(column_index_.begin(), column_index_.end()))< |
---|
181 | data_->columns()); |
---|
182 | // Checking that no index is out of range |
---|
183 | assert(row_index_.empty() || |
---|
184 | *(max_element(row_index_.begin(), row_index_.end()))< |
---|
185 | weights_->rows()); |
---|
186 | assert(column_index_.empty() || |
---|
187 | *(max_element(column_index_.begin(), column_index_.end()))< |
---|
188 | weights_->columns()); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | MatrixLookupWeighted::MatrixLookupWeighted(const size_t rows, |
---|
194 | const size_t columns, |
---|
195 | const double value, |
---|
196 | const double weight) |
---|
197 | : DataLookup2D(rows,columns) |
---|
198 | { |
---|
199 | data_ = new utility::matrix(1,1,value); |
---|
200 | ref_count_=new u_int(1); |
---|
201 | weights_ = new utility::matrix(1,1,weight); |
---|
202 | ref_count_weights_=new u_int(1); |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep) |
---|
207 | : DataLookup2D() |
---|
208 | { |
---|
209 | data_ = new utility::matrix(is,sep); |
---|
210 | ref_count_=new u_int(1); |
---|
211 | for(size_t i=0;i<(*data_).rows();i++) |
---|
212 | row_index_.push_back(i); |
---|
213 | for(size_t i=0;i<(*data_).columns();i++) |
---|
214 | column_index_.push_back(i); |
---|
215 | utility::matrix weights; |
---|
216 | data_->nan(weights); |
---|
217 | weights_= new utility::matrix(weights); |
---|
218 | ref_count_weights_=new u_int(1); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | MatrixLookupWeighted::~MatrixLookupWeighted(void) |
---|
223 | { |
---|
224 | if (ref_count_) |
---|
225 | if (!--(*ref_count_)) |
---|
226 | delete data_; |
---|
227 | if (ref_count_weights_) |
---|
228 | if (!--(*ref_count_weights_)) |
---|
229 | delete weights_; |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | const MatrixLookupWeighted* |
---|
234 | MatrixLookupWeighted::selected(const std::vector<size_t>& i) const |
---|
235 | { |
---|
236 | return new MatrixLookupWeighted(*this,i, true); |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | const MatrixLookupWeighted* |
---|
242 | MatrixLookupWeighted::training_data(const std::vector<size_t>& i) const |
---|
243 | { |
---|
244 | return new MatrixLookupWeighted(*this,i, false); |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | |
---|
249 | const MatrixLookupWeighted* |
---|
250 | MatrixLookupWeighted::validation_data(const std::vector<size_t>& train, |
---|
251 | const std::vector<size_t>& val) const |
---|
252 | { |
---|
253 | return new MatrixLookupWeighted(*this,val, false); |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | |
---|
258 | bool MatrixLookupWeighted::weighted(void) const |
---|
259 | { |
---|
260 | return true; |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | |
---|
265 | const MatrixLookupWeighted& MatrixLookupWeighted::operator= |
---|
266 | (const MatrixLookupWeighted& other) |
---|
267 | { |
---|
268 | if (this!=&other){ |
---|
269 | if (ref_count_ && !--(*ref_count_)) |
---|
270 | delete data_; |
---|
271 | if (ref_count_weights_ && !--(*ref_count_weights_)) |
---|
272 | delete weights_; |
---|
273 | DataLookup2D::operator=(other); |
---|
274 | data_ = other.data_; |
---|
275 | ref_count_=other.ref_count_; |
---|
276 | if (ref_count_) |
---|
277 | ++(*ref_count_); |
---|
278 | weights_ = other.weights_; |
---|
279 | ref_count_weights_ = other.ref_count_weights_; |
---|
280 | if (ref_count_weights_) |
---|
281 | ++(*ref_count_weights_); |
---|
282 | } |
---|
283 | return *this; |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | std::ostream& operator<<(std::ostream& s, const MatrixLookupWeighted& m) |
---|
288 | { |
---|
289 | s.setf(std::ios::dec); |
---|
290 | s.precision(12); |
---|
291 | for(size_t i=0, j=0; i<m.rows(); i++) |
---|
292 | for (j=0; j<m.columns(); j++) { |
---|
293 | s << m(i,j); |
---|
294 | if (j<m.columns()-1) |
---|
295 | s << s.fill(); |
---|
296 | else if (i<m.rows()-1) |
---|
297 | s << "\n"; |
---|
298 | } |
---|
299 | return s; |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | }} // of namespace classifier and namespace theplu |
---|