35 #ifndef SHARK_DATA_PRECOMPUTEDMATRIX_H 36 #define SHARK_DATA_PRECOMPUTEDMATRIX_H 75 template<
typename InputType,
typename LabelType>
81 bool scientific =
false,
82 unsigned int fieldwidth = 0
87 typedef typename Elements::reference ElementRef;
95 throw(std::invalid_argument(
"[export_kernel_matrix] Can't write to stream."));
102 double trace_factor = 1.0;
105 BOOST_FOREACH(ElementRef point, points)
107 trace += kernel.
eval(point.input, point.input);
110 trace_factor = 1.0 /
trace;
113 trace_factor *=
size;
118 double variance_factor = 0.0;
124 variance_factor = scaled.
factor();
129 RealVector rowmeans(size, 0.0);
133 for(std::size_t i = 0; i <
size; i++)
135 double k = kernel.
eval(points[i].input, points[i].input);
138 for(std::size_t j = 0; j < i; j++)
140 double k = kernel.
eval(points[i].input, points[j].input);
146 mean = mean / (double) size / (
double)
size;
152 for(std::size_t i = 0; i <
size; i++)
154 trace += kernel.
eval(points[i].input, points[i].input) - 2 * rowmeans(i) +
mean;
157 trace_factor = 1.0 /
trace;
165 out.setf(std::ios_base::scientific);
166 std::streamsize ss = out.precision();
170 double max_label = -1e100;
171 double min_label = -max_label;
173 bool regression =
false;
176 if(cur_label > max_label)
177 max_label = cur_label;
178 if(cur_label < min_label)
179 min_label = cur_label;
180 if((cur_label != (
int)cur_label) || cur_label < 0)
183 if(!regression && (min_label == 0) && (max_label == 1))
190 for(std::size_t i = 0; i <
size; i++)
196 out << std::setw(fieldwidth) << std::left << points[i].label <<
" ";
200 out << std::setw(fieldwidth) << std::left << (int)(points[i].label * 2 - 1) <<
" ";
204 out << std::setw(fieldwidth) << std::left << (
unsigned int)(points[i].label + 1) <<
" ";
207 out <<
"0:" << std::setw(fieldwidth) << std::left << i + 1;
211 if(normalizer ==
NONE)
213 for(std::size_t j = 0; j <
size; j++)
215 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << kernel.
eval(points[i].input, points[j].input);
221 for(std::size_t j = 0; j <
size; j++)
223 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << trace_factor * kernel.
eval(points[i].input, points[j].input);
229 for(std::size_t j = 0; j <
size; j++)
231 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << variance_factor *kernel.
eval(points[i].input, points[j].input);
237 for(std::size_t j = 0; j <
size; j++)
239 double tmp = kernel.
eval(points[i].input, points[j].input) - rowmeans(i) - rowmeans(j) +
mean;
240 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << tmp;
246 for(std::size_t j = 0; j <
size; j++)
248 double tmp = kernel.
eval(points[i].input, points[j].input) - rowmeans(i) - rowmeans(j) +
mean;
249 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << trace_factor *tmp;
255 throw SHARKEXCEPTION(
"[detail::export_kernel_matrix] Unknown normalization type.");
274 template<
typename InputType,
typename LabelType>
281 unsigned int width = 0
284 std::ofstream ofs(fn.c_str());
290 throw(std::invalid_argument(
"[detail::export_kernel_matrix] Stream cannot be opened for writing."));
298 template<
typename InputType,
typename LabelType>
304 bool scientific =
false,
305 unsigned int fieldwidth = 0
313 template<
typename InputType,
typename LabelType>
320 unsigned int width = 0
338 #endif // SHARK_DATA_PRECOMPUTEDMATRIX_H