machine learning - LibSvm add features using the JAVA api -
i have text , want train adding feature using java api. looking @ examples main class build training set svm_problem. appear svm_node represents feature (the index feature , value weight of feature).
what have done have map (just simplify problem) keeps association between feature , index. each of weight> example create new node :
svm_node currentnode = new svm_node(); int index = feature.getindexinmap(); double value = feature.getweight(); currentnode.index = index; currentnode.value = value; is intuition correct? svm_problem.y refers to? refer index of label? svm_problem.l length of 2 vectors?
your intuition close, svm_node pattern not feature. variable svm_problem.y array contains labels of each pattern , svm_problem.l size of training set.
also, beware svm_parameter.nr_weight weight of each label (useful if have unbalanced training set) if not going use must set value zero.
let me show 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, ¶ms); 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; } /* should free memory @ point. example large enough */ }
Comments
Post a Comment