LIBSVM训练数据格式(x值在svm_node为svm_problem)(LIBSVM train

2019-10-19 04:23发布

我使用LIBSVM以编程方式做一个简单的XOR分类,试图了解函数是如何工作的。 我有以下自述的说明尽可能靠近设置的问题。 我仍然使用svm_predict时得到错误的输出(始终为1或-1)。

在一个相关的问题,有人建议,使用很少的训练样本时可能出现的问题。 我试图提高了实例的个数,以20但这并没有帮助。

我怀疑,这个问题是某处prob.x和/或prob.y的定义,但可以不明白的地方。 你能帮助阐明如何界定prob.x和使用svm_node prob.y?

我哈得彻底搜查,但找不到答案......比如这里, 这里, 这里, 这里, 这里。

提前致谢!

这里是我的代码:

//Parameters
svm_parameter param;
param.svm_type = C_SVC;
param.kernel_type = RBF;
param.degree = 3;
param.gamma = 0;
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100;
param.C = 0.4;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.probability = 0;
param.nr_weight = 0;
param.weight_label = NULL;
param.weight = NULL;



//Problem definition
svm_problem prob;


//Length
prob.l = 4;                             //number of training examples


//x values

svm_node** x = new svm_node *[prob.l];  //Array of pointers to pointers to arrays

svm_node* x_space1 = new svm_node[3];   //Fist training example
svm_node* x_space2 = new svm_node[3];   //Second training example
svm_node* x_space3 = new svm_node[3];   //Third training example
svm_node* x_space4 = new svm_node[3];   //Fourth training example

x_space1[0].index = 1;                  //Fist training example
x_space1[0].value = 1;
x_space1[1].index = 2;
x_space1[1].value = 1;
x_space1[2].index = -1;

x_space2[0].index = 1;                  //Second training example
x_space2[0].value = 1;
x_space2[1].index = 2;
x_space2[1].value = 0;
x_space2[2].index = -1;

x_space3[0].index = 1;                  //Third training example
x_space3[0].value = 0;
x_space3[1].index = 2;
x_space3[1].value = 1;
x_space3[2].index = -1;

x_space4[0].index = 1;                  //Fourth training example
x_space4[0].value = 0;
x_space4[1].index = 2;
x_space4[1].value = 0;
x_space4[2].index = -1;

x[0] = x_space1;                        //Set each training example to x
x[1] = x_space2;
x[2] = x_space3;
x[3] = x_space4;

prob.x = x;                             //Assign x to the struct field prob.x


//yvalues
prob.y = new double[prob.l];
prob.y[0] = -1;
prob.y[1] = 1;
prob.y[2] = 1;
prob.y[3] = -1;


//Train model
svm_model *model = svm_train(&prob,&param);


//Test model
svm_node* testnode = new svm_node[3];
testnode[0].index = 1;
testnode[0].value = 1;
testnode[1].index = 2;
testnode[1].value = 0;
testnode[2].index = -1;

double retval = svm_predict(model,testnode);
qDebug()<<retval;                               //Should return +1 but returns -1

Answer 1:

这似乎与你的参数有问题。 例如,如果您使用的是RBF核param.gamma不应该是零。



Answer 2:

  1. 为什么你的XOR问题3维? 你并不需要在每个点上的第三个维度(其实你定义它,但不使用它,我不知道会做LIBSVM,但可以肯定会影响到所选择的伽马,作为LIBSVM启发式选择1 / number_of_dimensions)
  2. C参数看起来suspucious( 0.4可能的方式为低,尝试1000


文章来源: LIBSVM training data format (x values in svm_node for svm_problem)