Saturday, 24 August 2013

Classification using LibSVM

Classification using LibSVM

I am using LibSVM to carry out some multi-class classifications. I trained
the model using the MATLAB interface of LibSVM. I then saved this model in
a format that would be recognized in C. I now want to classify using
svm_predict in C. I am having trouble being able to reproduce the results
that I saw in MATLAB. In fact I get the same class output irrespective of
what test vector I feed in (even a vector of zeros) I think the issue is
with the way I am loading the test vector x into the svm_node structure.
Below is the code snippet. Do let me know if this is correct way or if I
am missing something.
struct svm_model *libsvm_model = svm_load_model('mymodel.svm');
struct svm_node x[2001]; // this is for one feature vector of size 2000x1
int index = 1;
int i = 0;
for (i = 0; i < features.size(); i++) {
x[i].index = index;
x[i].value = features.at(i);
index = index + 1;
}
x[i+1].index = -1;
x[i+1].value = '?';
double result = svm_predict(libsvm_model, x);

No comments:

Post a Comment