This class acts as a front end for the LIBSVM library (http://www.csie.ntu.edu.tw/~cjlin/libsvm/). It implements a Support Vector Machine (SVM) classifier, a powerful classifier that works well on a wide range of classification problems, particularly on more complex problems that other classifiers (such as the KNN, GMM or ANBC algorithms) might not be able to solve.
using namespace std;
int main (int argc, const char * argv[])
{
if( argc != 2 ){
cout << "Error: failed to parse data filename from command line. You should run this example with one argument pointing to the data filename!\n";
return EXIT_FAILURE;
}
const string filename = argv[1];
SVM svm(SVM::LINEAR_KERNEL);
if( !trainingData.
load( filename ) ){
cout << "Failed to load training data: " << filename << endl;
return EXIT_FAILURE;
}
if( !svm.
train( trainingData ) ){
cout << "Failed to train classifier!\n";
return EXIT_FAILURE;
}
if( !svm.
save(
"SVMModel.grt") ){
cout << "Failed to save the classifier model!\n";
return EXIT_FAILURE;
}
if( !svm.
load(
"SVMModel.grt") ){
cout << "Failed to load the classifier model!\n";
return EXIT_FAILURE;
}
double accuracy = 0;
UINT classLabel = testData[i].getClassLabel();
bool predictSuccess = svm.
predict( inputVector );
if( !predictSuccess ){
cout << "Failed to perform prediction for test sampel: " << i <<"\n";
return EXIT_FAILURE;
}
if( classLabel == predictedClassLabel ) accuracy++;
cout << "TestSample: " << i << " ClassLabel: " << classLabel << " PredictedClassLabel: " << predictedClassLabel << endl;
}
cout <<
"Test Accuracy: " << accuracy/double(testData.
getNumSamples())*100.0 <<
"%" << endl;
return EXIT_SUCCESS;
}