1 | // $Id$ |
---|
2 | |
---|
3 | #include <c++_tools/classifier/MatrixLookupWeighted.h> |
---|
4 | |
---|
5 | #include <c++_tools/utility/matrix.h> |
---|
6 | |
---|
7 | #ifndef NDEBUG |
---|
8 | #include <algorithm> |
---|
9 | #endif |
---|
10 | |
---|
11 | #include <fstream> |
---|
12 | |
---|
13 | namespace theplu { |
---|
14 | namespace classifier { |
---|
15 | |
---|
16 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data, |
---|
17 | const utility::matrix& weights) |
---|
18 | : DataLookup2D(), data_(&data), weights_(&weights),weights_owner_(false) |
---|
19 | { |
---|
20 | assert(data.rows()==weights.rows()); |
---|
21 | assert(data.columns()==weights.columns()); |
---|
22 | for(size_t i=0;i<(*data_).rows();i++) |
---|
23 | row_index_.push_back(i); |
---|
24 | for(size_t i=0;i<(*data_).columns();i++) |
---|
25 | column_index_.push_back(i); |
---|
26 | } |
---|
27 | |
---|
28 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data) |
---|
29 | : DataLookup2D(), data_(&data), weights_owner_(true) |
---|
30 | { |
---|
31 | utility::matrix weights; |
---|
32 | data_->nan(weights); |
---|
33 | weights_= new utility::matrix(weights); |
---|
34 | for(size_t i=0;i<(*data_).rows();i++) |
---|
35 | row_index_.push_back(i); |
---|
36 | for(size_t i=0;i<(*data_).columns();i++) |
---|
37 | column_index_.push_back(i); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data, |
---|
43 | const utility::matrix& weights, |
---|
44 | const std::vector<size_t>& row, |
---|
45 | const std::vector<size_t>& col) |
---|
46 | : DataLookup2D(row,col), data_(&data), weights_(&weights), |
---|
47 | weights_owner_(false) |
---|
48 | { |
---|
49 | // Checking that each row index is less than data.rows() |
---|
50 | assert(row.empty() || |
---|
51 | *(std::max_element(row.begin(),row.end()))<data.rows()); |
---|
52 | // Checking that each column index is less than data.column() |
---|
53 | assert(col.empty() || |
---|
54 | *(std::max_element(col.begin(),col.end()))<data.columns()); |
---|
55 | // Checking that each row index is less than weights.rows() |
---|
56 | assert(row.empty() || |
---|
57 | *(std::max_element(row.begin(),row.end()))<weights.rows()); |
---|
58 | // Checking that each column index is less than weights.column() |
---|
59 | assert(col.empty() || |
---|
60 | *(std::max_element(col.begin(),col.end()))<weights.columns()); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data, |
---|
66 | const utility::matrix& weights, |
---|
67 | const std::vector<size_t>& index, |
---|
68 | const bool row) |
---|
69 | : DataLookup2D(), data_(&data), weights_(&weights),weights_owner_(false) |
---|
70 | { |
---|
71 | if (row){ |
---|
72 | // Checking that each row index is less than data.rows() |
---|
73 | assert(index.empty() || |
---|
74 | *(std::max_element(index.begin(),index.end()))<data.rows()); |
---|
75 | // Checking that each row index is less than weights.rows() |
---|
76 | assert(index.empty() || |
---|
77 | *(std::max_element(index.begin(),index.end()))<weights.rows()); |
---|
78 | row_index_=index; |
---|
79 | assert(column_index_.empty()); |
---|
80 | column_index_.reserve(data.columns()); |
---|
81 | for (size_t i=0; i<data.columns(); i++) |
---|
82 | column_index_.push_back(i); |
---|
83 | } |
---|
84 | else{ |
---|
85 | // Checking that each column index is less than data.column() |
---|
86 | assert(index.empty() || |
---|
87 | *(std::max_element(index.begin(),index.end()))<data.columns()); |
---|
88 | // Checking that each column index is less than weights.column() |
---|
89 | assert(index.empty() || |
---|
90 | *(std::max_element(index.begin(),index.end()))<weights.columns()); |
---|
91 | column_index_=index; |
---|
92 | assert(row_index_.empty()); |
---|
93 | column_index_.reserve(data.rows()); |
---|
94 | for (size_t i=0; i<data.rows(); i++) |
---|
95 | row_index_.push_back(i); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /* |
---|
101 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& dv, |
---|
102 | const MatrixLookup& wv) |
---|
103 | : DataLookup2D(dv), data_(dv.data_), weights_(dv.data_) |
---|
104 | { |
---|
105 | } |
---|
106 | */ |
---|
107 | |
---|
108 | |
---|
109 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& dv) |
---|
110 | : DataLookup2D(dv), data_(dv.data_), weights_(dv.weights_), |
---|
111 | weights_owner_(false) |
---|
112 | { |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& ml, |
---|
118 | const std::vector<size_t>& row, |
---|
119 | const std::vector<size_t>& col) |
---|
120 | : DataLookup2D(ml,row,col), data_(ml.data_), weights_(ml.weights_), |
---|
121 | weights_owner_(false) |
---|
122 | { |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& ml, |
---|
128 | const std::vector<size_t>& index, |
---|
129 | bool row) |
---|
130 | : DataLookup2D(ml,index,row), data_(ml.data_), weights_(ml.weights_), |
---|
131 | weights_owner_(false) |
---|
132 | { |
---|
133 | // Checking that no index is out of range |
---|
134 | assert(row_index_.empty() || |
---|
135 | *(max_element(row_index_.begin(), row_index_.end()))<data_->rows()); |
---|
136 | assert(column_index_.empty() || |
---|
137 | *(max_element(column_index_.begin(), column_index_.end()))< |
---|
138 | data_->columns()); |
---|
139 | // Checking that no index is out of range |
---|
140 | assert(row_index_.empty() || |
---|
141 | *(max_element(row_index_.begin(), row_index_.end()))< |
---|
142 | weights_->rows()); |
---|
143 | assert(column_index_.empty() || |
---|
144 | *(max_element(column_index_.begin(), column_index_.end()))< |
---|
145 | weights_->columns()); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | MatrixLookupWeighted::MatrixLookupWeighted(const size_t rows, |
---|
151 | const size_t columns, |
---|
152 | const double value, |
---|
153 | const double weight) |
---|
154 | : DataLookup2D(rows,columns, true),weights_owner_(true) |
---|
155 | { |
---|
156 | data_ = new utility::matrix(1,1,value); |
---|
157 | weights_ = new utility::matrix(1,1,weight); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep) |
---|
162 | : DataLookup2D(true), weights_owner_(true) |
---|
163 | { |
---|
164 | data_ = new utility::matrix(is,sep); |
---|
165 | for(size_t i=0;i<(*data_).rows();i++) |
---|
166 | row_index_.push_back(i); |
---|
167 | for(size_t i=0;i<(*data_).columns();i++) |
---|
168 | column_index_.push_back(i); |
---|
169 | utility::matrix weights; |
---|
170 | data_->nan(weights); |
---|
171 | weights_= new utility::matrix(weights); |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | MatrixLookupWeighted::~MatrixLookupWeighted(void) |
---|
176 | { |
---|
177 | if (owner_) |
---|
178 | delete data_; |
---|
179 | if (weights_owner_) |
---|
180 | delete weights_; |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | const MatrixLookupWeighted* |
---|
185 | MatrixLookupWeighted::selected(const std::vector<size_t>& i) const |
---|
186 | { |
---|
187 | return new MatrixLookupWeighted(*this,i, true); |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | const MatrixLookupWeighted* |
---|
193 | MatrixLookupWeighted::training_data(const std::vector<size_t>& i) const |
---|
194 | { |
---|
195 | return new MatrixLookupWeighted(*this,i, false); |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | const MatrixLookupWeighted* |
---|
201 | MatrixLookupWeighted::training_data(const std::vector<size_t>& features, |
---|
202 | const std::vector<size_t>& samples) const |
---|
203 | { |
---|
204 | return new MatrixLookupWeighted(*this,features, samples); |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | |
---|
209 | const MatrixLookupWeighted* |
---|
210 | MatrixLookupWeighted::validation_data(const std::vector<size_t>& train, |
---|
211 | const std::vector<size_t>& val) const |
---|
212 | { |
---|
213 | return new MatrixLookupWeighted(*this,val, false); |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | |
---|
218 | const MatrixLookupWeighted* |
---|
219 | MatrixLookupWeighted::validation_data(const std::vector<size_t>& features, |
---|
220 | const std::vector<size_t>& training, |
---|
221 | const std::vector<size_t>& val) const |
---|
222 | { |
---|
223 | return new MatrixLookupWeighted(*this,features, val); |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | |
---|
228 | bool MatrixLookupWeighted::weighted(void) const |
---|
229 | { |
---|
230 | return true; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | const MatrixLookupWeighted& MatrixLookupWeighted::operator= |
---|
236 | (const MatrixLookupWeighted& other) |
---|
237 | { |
---|
238 | if (this!=&other){ |
---|
239 | if (owner_){ |
---|
240 | delete data_; |
---|
241 | delete weights_; |
---|
242 | owner_=false; |
---|
243 | } |
---|
244 | DataLookup2D::operator=(other); |
---|
245 | data_ = other.data_; |
---|
246 | weights_ = other.weights_; |
---|
247 | } |
---|
248 | return *this; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | std::ostream& operator<<(std::ostream& s, const MatrixLookupWeighted& m) |
---|
253 | { |
---|
254 | s.setf(std::ios::dec); |
---|
255 | s.precision(12); |
---|
256 | for(size_t i=0, j=0; i<m.rows(); i++) |
---|
257 | for (j=0; j<m.columns(); j++) { |
---|
258 | s << m(i,j); |
---|
259 | if (j<m.columns()-1) |
---|
260 | s << s.fill(); |
---|
261 | else if (i<m.rows()-1) |
---|
262 | s << "\n"; |
---|
263 | } |
---|
264 | return s; |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | }} // of namespace classifier and namespace theplu |
---|