js-svm
Package provides javascript implementation of linear SVM and SVM with gaussian kernel
Features
- Support for binary classification
- Support for multi-class classification
Install
npm install js-svm
Usage
SVM Binary Classifier
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 = ;var iris = ; var svm = ; iris; var trainingDataSize = Math;var trainingData = ;var testingData = ;forvar i=0; i < irisrowCount ; ++i var row = ; row; // sepalLength; row; // sepalWidth; row; // petalLength; row; // petalWidth; row; // output which is 1 if species is Iris-virginica; 0 otherwise ifi < trainingDataSize trainingData; else testingData; var result = svm; console; forvar i=0; i < testingDatalength; ++i var predicted = svm; console;
To configure the BinarySvmClassifier, use the following code when it is created:
var svm = alpha: 001 // learning rate iterations: 1000 // maximum iterations C: 50 // 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 = ;var iris = ; var classifier = ; iris; var trainingDataSize = Math;var trainingData = ;var testingData = ;forvar i=0; i < irisrowCount ; ++i var row = ; row; // sepalLength; row; // sepalWidth; row; // petalLength; row; // petalWidth; row; // output is species ifi < trainingDataSize trainingData; else testingData; var result = classifier; console; forvar i=0; i < testingDatalength; ++i var predicted = classifier; console;
To configure the MultiClassSvmClassifier, use the following code when it is created:
var classifier = alpha: 001 // learning rate iterations: 1000 // maximum iterations C: 50 // panelty term sigma: 10 // 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;
To switch to use gaussian kernel, put the property 'kernel: "gaussian"' in the config data when the classifier is created:
var svm = ... kernel: 'gaussian'; ... var svm = ... 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
The demo code in HTML can be found in the following files within the package: