64 printf(
"SVMClassifier::train() -- No training data available! Doing nothing.\n");
72 int dims =
class_data.begin()->second[0].size();
77 if (cpl.size() == 1) {
91 for (
int i = 0; i < n_data; i++) {
101 std::string cname = iter->first;
111 for (
int i = 0; i < dims; i++) {
116 for (
int j = 0; j < dims; j++) {
118 double minval = INFINITY;
119 double maxval = -INFINITY;
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;
139 for (
size_t i = 0; i < iter->second.size(); i++) {
140 iter->second[i][j] = (iter->second[i][j] - offset) / factor;
151 std::string cname = iter->first;
155 if (cpl.size() == 1) {
159 for (
int j = 0; j < dims; j++) {
170 for (
size_t i = 0; i < cpl.size(); i++) {
173 for (
int j = 0; j < dims; j++) {
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++) {
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++) {
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",
298 int dims = point.size();
301 for (
int i = 0; i < dims; i++) {
302 test_pt[i].
index = i;
307 test_pt[dims].
index = -1;
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)
std::map< int, std::string > label_int_to_str
def svm_predict(y, x, m, options="")
bool load(const std::string filename)
double ** scaling_factors
void addTrainingPoint(std::string target_class, const std::vector< double > point)
std::string classifyPoint(const std::vector< double > point)
std::map< std::string, int > label_str_to_int
void save(const std::string filename)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
std::vector< CPoint > CPointList
svm_model * trained_model