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