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 std::size_t size = points.
size();
93 throw(std::invalid_argument(
"[export_kernel_matrix] Can't write to stream."));
100 double trace_factor = 1.0;
103 for(
auto point: points)
105 trace += kernel.
eval(point.input, point.input);
108 trace_factor = 1.0 / trace;
111 trace_factor *= size;
116 double variance_factor = 0.0;
122 variance_factor = scaled.
factor();
127 RealVector rowmeans(size, 0.0);
131 for(std::size_t i = 0; i < size; i++)
133 double k = kernel.
eval(points[i].input, points[i].input);
136 for(std::size_t j = 0; j < i; j++)
138 double k = kernel.
eval(points[i].input, points[j].input);
144 mean = mean / (double) size / (
double) size;
150 for(std::size_t i = 0; i < size; i++)
152 trace += kernel.
eval(points[i].input, points[i].input) - 2 * rowmeans(i) +
mean;
155 trace_factor = 1.0 / trace;
163 out.setf(std::ios_base::scientific);
164 std::streamsize ss = out.precision();
168 double max_label = -1e100;
169 double min_label = -max_label;
171 bool regression =
false;
174 if(cur_label > max_label)
175 max_label = cur_label;
176 if(cur_label < min_label)
177 min_label = cur_label;
178 if((cur_label != (
int)cur_label) || cur_label < 0)
181 if(!regression && (min_label == 0) && (max_label == 1))
188 for(std::size_t i = 0; i < size; i++)
194 out << std::setw(fieldwidth) << std::left << points[i].label <<
" ";
198 out << std::setw(fieldwidth) << std::left << (int)(points[i].label * 2 - 1) <<
" ";
202 out << std::setw(fieldwidth) << std::left << (
unsigned int)(points[i].label + 1) <<
" ";
205 out <<
"0:" << std::setw(fieldwidth) << std::left << i + 1;
209 if(normalizer ==
NONE)
211 for(std::size_t j = 0; j < size; j++)
213 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << kernel.
eval(points[i].input, points[j].input);
219 for(std::size_t j = 0; j < size; j++)
221 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << trace_factor * kernel.
eval(points[i].input, points[j].input);
227 for(std::size_t j = 0; j < size; j++)
229 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << variance_factor *kernel.
eval(points[i].input, points[j].input);
235 for(std::size_t j = 0; j < size; j++)
237 double tmp = kernel.
eval(points[i].input, points[j].input) - rowmeans(i) - rowmeans(j) +
mean;
238 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << tmp;
244 for(std::size_t j = 0; j < size; j++)
246 double tmp = kernel.
eval(points[i].input, points[j].input) - rowmeans(i) - rowmeans(j) +
mean;
247 out <<
" " << j + 1 <<
":" << std::setw(fieldwidth) << std::left << trace_factor *tmp;
253 throw SHARKEXCEPTION(
"[detail::export_kernel_matrix] Unknown normalization type.");
272 template<
typename InputType,
typename LabelType>
279 unsigned int width = 0
282 std::ofstream ofs(fn.c_str());
288 throw(std::invalid_argument(
"[detail::export_kernel_matrix] Stream cannot be opened for writing."));
296 template<
typename InputType,
typename LabelType>
302 bool scientific =
false,
303 unsigned int fieldwidth = 0
311 template<
typename InputType,
typename LabelType>
318 unsigned int width = 0
336 #endif // SHARK_DATA_PRECOMPUTEDMATRIX_H