Make predictions using LIBSVM on Android
In machine learning, support vector machines are supervised learning models with associated learning algorithms that analyze data used for classification and regression analysis. Given a set of training examples, each marked for belonging to one of two categories, an SVM training algorithm builds a model that assigns new examples into one category or the other, making it a non-probabilistic binary linear classifier. [Wikipedia]
LIBSVM is an integrated software for support vector classification, (C-SVC, nu-SVC), regression (epsilon-SVR, nu-SVR) and distribution estimation (one-class SVM). It supports multi-class classification. [official page]
cordova plugin add cordova-plugin-classifier
-
Train the model on your computer. You can find more information about the process here.
-
Rename the model file to
svm.model
and copy it to the SD Card of the phone -
Make sure that your app has permissions to access the SD Card
Then you can use the plugin in your Javascript code like this
document.addEventListener("deviceready", function () {
var featuresForPrediction = "1:-1.43 2:-1.15 3:1.08 4:-1.13 5:-1.99";
predict(featuresForPrediction, success, error);
function success(result) {
console.log(result);
}
function error(err) {
console.log("some error has occured");
}
}, false);
var features = [-1.43, -1.15, 1.08, -1.13, -1.99];
console.log(formatLIBSVM(features)); // 1:-1.43 2:-1.15 3:1.08 4:-1.13 5:-1.99
function formatLIBSVM(features) {
var featuresLIBSVM = "";
for (var i = 0; i < features.length; i++) {
featuresLIBSVM = featuresLIBSVM + (i+1).toString() + ":" + features[i] + " ";
}
return featuresLIBSVM;
}