int main() { srand(time(0)); SVMClassifier svm; //Do a simple test of loading a pre-made model, testing 2 vectors, and saving it double testVectorPos[] = {.7,1,1,-.32,-.105,-1,1,-.4198,-1,-.2258,1,-1}; double testVectorNeg[] = {.58,-1,.3333,-.6,1,-1,1,.358779,-1,-.484,-1,1}; unsigned int testVectorSize = 12; svm.loadModel("svm.model"); double probability; probability = svm.classifyVector(testVectorPos,testVectorSize); cout << "Test VectorPos Probability is " << probability << endl; probability = svm.classifyVector(testVectorNeg,testVectorSize); cout << "Test VectorNeg Probability is " << probability << endl; svm.saveModel("svmNew1.model"); //Create a new model that exists of a simple cluster in the second quadrant of a 2d grid being positive, and the rest being negative (x,y) testVectorSize = 2; unsigned int numSamples = 1000; double **trainVec = new double*[numSamples]; bool *trainPolarities = new bool[numSamples]; for (unsigned int vecIter = 0; vecIter < numSamples; vecIter++) { trainVec[vecIter] = new double[2]; //Pos if (vecIter <= (numSamples - 1)/2) { trainVec[vecIter][0] = -1*rand()/(double)RAND_MAX; trainVec[vecIter][1] = rand()/(double)RAND_MAX; trainPolarities[vecIter] = true; } //Neg else { trainVec[vecIter][0] = rand()/(double)RAND_MAX; trainVec[vecIter][1] = -1*rand()/(double)RAND_MAX; trainPolarities[vecIter] = false; } } svm.trainModel(trainVec, trainPolarities, numSamples, testVectorSize); svm.saveModel("svmNew2.model"); for (unsigned int testVecIter = 0; testVecIter < numSamples; testVecIter++) { probability = svm.classifyVector(trainVec[testVecIter],testVectorSize); cout << "Test Probability is " << probability << endl; } delete [] trainPolarities; for (unsigned int vecIter = 0; vecIter < numSamples; vecIter++) delete [] trainVec[vecIter]; delete [] trainVec; return 0; }
// run from build folder of the project (see path below) int main (int argc, char** argv) { Eigen::MatrixXf X; Eigen::MatrixXf y; eigen_extensions::loadASCII("../test/svmtestx.eig.txt", &X); eigen_extensions::loadASCII("../test/svmtesty.eig.txt", &y); // Classify cout << X.topRows(5) << endl; cout << y.topRows(5) << endl; vector<int> yhat; SVMClassifier svm; svm.train(X, y); svm.test(X, yhat); Eigen::MatrixXf w; float b; svm.getw(w, b); Eigen::MatrixXf margin= ((X * w).array() + b).matrix(); // ahh eigen... // Evaluate accuracy and print results int match=0; for(int i=0;i<yhat.size();i++) { if(yhat[i]==(int)y(i)) match++; else printf("WRONG! "); printf("y= %d, yhat= %d margin= %f\n", (int)y(i), yhat[i], margin(i)); } printf("MATLAB cross-check: last margin should be around -3.107\n"); printf("Accuracy= %f. From MATLAB cross-check, expect this to be around 0.945\n", 1.0*match/yhat.size()); // Save the model svm.saveModel("temp.svmmodel"); // Test loading model in new instance of SVMClassifier SVMClassifier svm2; svm2.loadModel("temp.svmmodel"); yhat.clear(); svm2.test(X, yhat); match=0; for(int i=0;i<yhat.size();i++) if(yhat[i]==(int)y(i)) match++; printf("Accuracy= %f from loaded model. Should be 0.945 again.\n", 1.0*match/yhat.size()); printf("you may want to rm the temporary file temp.svmmodel\n"); }