The sample code below show how to use SVM binary classifier on the iris datsets to classify whether a data row belong to species Iris-virginica:
var jssvm =require('js-svm');
var iris =require('js-datasets-iris');
var svm =newjssvm.BinarySvmClassifier();
iris.shuffle();
var trainingDataSize =Math.round(iris.rowCount*0.9);
var trainingData = [];
var testingData = [];
for(var i=0; i <iris.rowCount ; ++i) {
var row = [];
row.push(iris.data[i][0]); // sepalLength;row.push(iris.data[i][1]); // sepalWidth;row.push(iris.data[i][2]); // petalLength;row.push(iris.data[i][3]); // petalWidth;row.push(iris.data[i][4] =="Iris-virginica"?1.0:0.0); // output which is 1 if species is Iris-virginica; 0 otherwiseif(i < trainingDataSize){
trainingData.push(row);
} else {
testingData.push(row);
}
}
var result =svm.fit(trainingData);
console.log(result);
for(var i=0; i <testingData.length; ++i){
var predicted =svm.transform(testingData[i]);
console.log("actual: "+ testingData[i][4] +" predicted: "+ predicted);
}
To configure the BinarySvmClassifier, use the following code when it is created:
var svm =newjssvm.BinarySvmClassifier({
alpha:0.01, // learning rate
iterations:1000, // maximum iterationsC:5.0, // panelty term
trace:false// debug tracing
});
Multi-Class Classification using One-vs-All Logistic Regression
The sample code below illustrates how to run the multi-class classifier on the iris datasets to classifiy the species of each data row:
var jssvm =require('js-svm');
var iris =require('js-datasets-iris');
var classifier =newjssvm.MultiClassSvmClassifier();
iris.shuffle();
var trainingDataSize =Math.round(iris.rowCount*0.9);
var trainingData = [];
var testingData = [];
for(var i=0; i <iris.rowCount ; ++i) {
var row = [];
row.push(iris.data[i][0]); // sepalLength;row.push(iris.data[i][1]); // sepalWidth;row.push(iris.data[i][2]); // petalLength;row.push(iris.data[i][3]); // petalWidth;row.push(iris.data[i][4]); // output is speciesif(i < trainingDataSize){
trainingData.push(row);
} else {
testingData.push(row);
}
}
var result =classifier.fit(trainingData);
console.log(result);
for(var i=0; i <testingData.length; ++i){
var predicted =classifier.transform(testingData[i]);
console.log("svm prediction testing: actual: "+ testingData[i][4] +" predicted: "+ predicted);
}
To configure the MultiClassSvmClassifier, use the following code when it is created:
var classifier =newjssvm.MultiClassSvmClassifier({
alpha:0.01, // learning rate
iterations:1000, // maximum iterationsC:5.0// panelty term
sigma:1.0// the standard deviation for the gaussian kernel
});
Switch between linear and guassian kernel
By default the kernel used by the binary and multi-class classifier is "linear" which can be printed by:
console.log(classifier.kernel);
To switch to use gaussian kernel, put the property 'kernel: "gaussian"' in the config data when the classifier is created:
var svm =newjssvm.BinarySvmClassifier({
...,
kernel:'gaussian'
});
....
var svm =newjssvm.MultiClassSvmClassifier({
...,
kernel:'gaussian'
});
Usage In HTML
Include the "node_modules/js-svm/build/jssvm.min.js" (or "node_modules/js-svm/src/jssvm.js") in your HTML <script> tag
I used to look for a libsvm written in pure Java, but the solutions found are not very satisfactory, and ended up making one myself. I have recently release the implementation and make it available in github.
The code above create a data frame which has the following columns
livch1 (input): value = 1 if the "livch" column of the CSV contains value 1 ; 0 otherwise
livch2 (input): value = 1 if the "livch" column of the CSV contains value 2 ; 0 otherwise
livch3 (input): value = 1 if the "livch" column of the CSV contains value 3+ ; 0 otherwise
age (input): value = numeric value in the "age" column of the CSV
age^2 (input): value = square of numeric value in the "age" column of the CSV
urban (input): value = 1 if the "urban" column of the CSV has value "Y" ; 0 otherwise
use (output): value = 1 if the "use" column of the CSV has value "Y" ; 0 otherwise
Currently csv files and the libsvm format are supported for creating data frame, to load a libsvm-format file, call "DataQuery.libsvm()" instead of "DataQuery.csv(..)".
In the future more option will be added for the supported format
One-class SVM
Below is the code to create and train a one-class SVM:
Below is the code to perform multi-class classification:
algorithm.classify(data_point)
Data Format
The data format default is the DataFrame class, which can be used to load csv and libsvm format text file. Please refers to the unit test cases on how they can be used.
Sample codes
Sample code for OneClassSVM:
Below is a sample code example of the one-class SVM for the example below here: