I have a text and I want to train by adding feature using the java API. Looking at the examples the main class to build the training set is the svm_problem. It appear like the svm_node represents a feature (the index is the feature and the value is the weight of the feature).

What I have done is to have a map (just to simplify the problem) that keeps an association between the feature and an index. For each of my weight> example I do create a new node :

  svm_node currentNode = new svm_node();
  int index = feature.getIndexInMap();
  double value = feature.getWeight();
  currentNode.index = index;
  currentNode.value = value;

Is my intuition correct? What does the svm_problem.y refers to? Does it refer to the index of the label? Is the svm_problem.l just the length of the two vectors?

有帮助吗?

解决方案

Your intuition is very close, but svm_node is a pattern not a feature. The variable svm_problem.y is an array that contains the labels of each pattern and svm_problem.l is the size of the training set.

Also, beware that svm_parameter.nr_weight is the weight of each label (useful if you have an unbalanced training set) but if you are not going to use it you must set that value to zero.

Let me show you a simple example in C++:

#include "svm.h"
#include <iostream>

using namespace std;

int main()
{
    svm_parameter params;


    params.svm_type = C_SVC;
    params.kernel_type = RBF;
    params.C = 1;
    params.gamma = 1;
    params.nr_weight = 0;
    params.p= 0.0001;

    svm_problem problem;
    problem.l = 4;
    problem.y = new double[4]{1,-1,-1,1};
    problem.x = new svm_node*[4];

    {
    problem.x[0] = new svm_node[3];
    problem.x[0][0].index = 1;
    problem.x[0][0].value = 0;
    problem.x[0][1].index = 2;
    problem.x[0][1].value = 0;
    problem.x[0][2].index = -1;

    }

    {
    problem.x[1] = new svm_node[3];
    problem.x[1][0].index = 1;
    problem.x[1][0].value = 1;
    problem.x[1][1].index = 2;
    problem.x[1][1].value = 0;
    problem.x[1][2].index = -1;
    }

    {
    problem.x[2] = new svm_node[3];
    problem.x[2][0].index = 1;
    problem.x[2][0].value = 0;
    problem.x[2][1].index = 2;
    problem.x[2][1].value = 1;
    problem.x[2][2].index = -1;
    }

   {
    problem.x[3] = new svm_node[3];
    problem.x[3][0].index = 1;
    problem.x[3][0].value = 1;
    problem.x[3][1].index = 2;
    problem.x[3][1].value = 1;
    problem.x[3][2].index = -1;

    }

    for(int i=0; i<4; i++)
    {
        cout << problem.y[i] << endl;
    }

    svm_model * model = svm_train(&problem, &params);
    svm_save_model("mymodel.svm", model);

    for(int i=0; i<4; i++)
    {
        double d = svm_predict(model, problem.x[i]);

        cout << "Prediction " << d << endl;
    }
    /* We should free the memory at this point. 
       But this example is large enough already */ 
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top