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