45 SVMClassifier::SVMClassifier() {}
47 SVMClassifier::~SVMClassifier() {}
49 void SVMClassifier::save(
const std::string filename) {}
51 bool SVMClassifier::load(
const std::string filename)
56 void SVMClassifier::addTrainingPoint(std::string target_class,
const std::vector<double>
point)
58 class_data[target_class].push_back(point);
61 void SVMClassifier::train()
63 if (class_data.size() == 0) {
64 printf(
"SVMClassifier::train() -- No training data available! Doing nothing.\n");
68 int n_classes = class_data.size();
72 int dims = class_data.begin()->second[0].size();
74 for (ClassMap::iterator iter = class_data.begin(); iter != class_data.end(); iter++) {
77 if (cpl.size() == 1) {
88 svm_data.y =
new double[n_data];
91 for (
int i = 0; i < n_data; i++) {
92 svm_data.x[i] =
new svm_node[dims + 1];
96 label_str_to_int.clear();
97 label_int_to_str.clear();
100 for (ClassMap::iterator iter = class_data.begin(); iter != class_data.end(); iter++) {
101 std::string cname = iter->first;
102 label_str_to_int[cname] = label_n;
103 label_int_to_str[label_n] = cname;
109 scaling_factors =
new double *[dims];
111 for (
int i = 0; i < dims; i++) {
112 scaling_factors[i] =
new double[2];
116 for (
int j = 0; j < dims; j++) {
118 double minval = INFINITY;
119 double maxval = -INFINITY;
121 for (ClassMap::iterator iter = class_data.begin(); iter != class_data.end(); iter++) {
124 for (
size_t i = 0; i < cpl.size(); i++) {
125 if (cpl[i][j] < minval) {
128 if (cpl[i][j] > maxval) {
134 double factor = maxval - minval;
135 double offset = minval;
138 for (ClassMap::iterator iter = class_data.begin(); iter != class_data.end(); iter++) {
139 for (
size_t i = 0; i < iter->second.size(); i++) {
140 iter->second[i][j] = (iter->second[i][j] - offset) / factor;
144 scaling_factors[j][0] = offset;
145 scaling_factors[j][1] = factor;
150 for (ClassMap::iterator iter = class_data.begin(); iter != class_data.end(); iter++) {
151 std::string cname = iter->first;
155 if (cpl.size() == 1) {
156 svm_data.y[n] = label_str_to_int[cname];
157 svm_data.y[n + 1] = label_str_to_int[cname];
159 for (
int j = 0; j < dims; j++) {
160 svm_data.x[n][j].
index = j;
161 svm_data.x[n][j].value = cpl[0][j] + 0.001;
162 svm_data.x[n + 1][j].index = j;
163 svm_data.x[n + 1][j].value = cpl[0][j] + 0.001;
166 svm_data.x[n][dims].index = -1;
167 svm_data.x[n + 1][dims].index = -1;
170 for (
size_t i = 0; i < cpl.size(); i++) {
171 svm_data.y[n] = label_str_to_int[cname];
173 for (
int j = 0; j < dims; j++) {
174 svm_data.x[n][j].index = j;
175 svm_data.x[n][j].value = cpl[i][j];
178 svm_data.x[n][dims].index = -1;
201 printf(
"SVMClassifier::train() -- Bad SVM parameters!\n");
202 printf(
"%s\n", err_str);
208 double * resp =
new double[n_data];
209 double best_accy = 0.0;
214 for (
double c = -5.0;
c <= 15.0;
c += 2.0) {
215 for (
double g = 3.0;
g >= -15.0;
g -= 2.0) {
217 params.
C = pow(2,
c);
224 for (
int i = 0; i < n_data; i++) {
225 if (resp[i] == svm_data.y[i]) {
229 double accy =
static_cast<double>(correct) / static_cast<double>(n_data);
231 if (accy > best_accy) {
233 best_g = params.
gamma;
241 double start_c = best_c - 1.0;
242 double end_c = best_c + 1.0;
243 double start_g = best_g + 1.0;
244 double end_g = best_g - 1.0;
246 for (
double c = start_c;
c <= end_c;
c += 0.1) {
247 for (
double g = start_g;
g >= end_g;
g -= 0.1) {
249 params.
C = pow(2,
c);
255 for (
int i = 0; i < n_data; i++) {
256 if (resp[i] == svm_data.y[i]) {
260 double accy =
static_cast<double>(correct) / static_cast<double>(n_data);
262 if (accy > best_accy) {
264 best_g = params.
gamma;
272 params.
gamma = best_g;
276 "BEST PARAMS ncl: %i c: %f g: %f accy: %f \n\n",
283 trained_model =
svm_train(&svm_data, ¶ms);
286 void SVMClassifier::clear()
289 label_str_to_int.clear();
290 label_int_to_str.clear();
291 trained_model = NULL;
292 scaling_factors = NULL;
295 std::string SVMClassifier::classifyPoint(
const std::vector<double>
point)
298 int dims = point.size();
301 for (
int i = 0; i < dims; i++) {
302 test_pt[i].
index = i;
304 test_pt[i].
value = (point[i] - scaling_factors[i][0]) / scaling_factors[i][1];
307 test_pt[dims].
index = -1;
311 return label_int_to_str[label_n];
const char * svm_check_parameter(const svm_problem *prob, const svm_parameter *param)
def svm_train(arg1, arg2=None, arg3=None)
void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, int nr_fold, double *target)
def svm_predict(y, x, m, options="")
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
std::vector< CPoint > CPointList