38 #ifndef FCL_TEST_LIBSVM_CLASSIFIER_H
39 #define FCL_TEST_LIBSVM_CLASSIFIER_H
41 #include "fcl/learning/classifier.h"
48 template<std::
size_t N>
128 virtual void learn(
const std::vector<Item<N> >& data)
130 if(data.size() == 0)
return;
145 for(std::size_t i = 0; i < data.size(); ++i)
148 Vecnf<N> q_scaled =
scaler.scale(data[i].q);
149 for(std::size_t j = 0; j < N; ++j)
151 cur_x_space[j].
index = j + 1;
152 cur_x_space[j].
value = q_scaled[j];
154 cur_x_space[N].
index = -1;
157 problem.
y[i] = (data[i].label ? 1 : -1);
165 virtual std::vector<PredictResult>
predict(
const std::vector<Vecnf<N> >& qs)
const
167 std::vector<PredictResult> predict_results;
170 double* prob_estimates = NULL;
174 prob_estimates = (
double*)malloc(nr_class *
sizeof(
double));
177 for(std::size_t i = 0; i < qs.size(); ++i)
180 for(std::size_t j = 0; j < N; ++j)
187 double predict_label;
192 predict_label = (predict_label > 0) ? 1 : 0;
193 predict_results.push_back(PredictResult(predict_label, *prob_estimates));
198 predict_label = (predict_label > 0) ? 1 : 0;
199 predict_results.push_back(PredictResult(predict_label));
206 return predict_results;
209 virtual PredictResult
predict(
const Vecnf<N>& q)
const
211 return (
predict(std::vector<Vecnf<N> >(1, q)))[0];
214 void save(
const std::string& filename)
const
222 std::vector<Item<N> > results;
224 for(std::size_t i = 0; i < (std::size_t)
model->
l; ++i)
226 for(std::size_t j = 0; j < N; ++j)
228 item.q =
scaler.unscale(item.q);
231 results.push_back(item);