Giter Site home page Giter Site logo

chen0040 / js-svm Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 3.0 40 KB

Package provides javascript implementation of support vector machines

License: MIT License

JavaScript 77.32% HTML 22.68%
linear-svm support-vector-machines kernel-svm kernels binary-classification multi-class-classification

js-svm's Introduction

js-svm

Package provides javascript implementation of linear SVM and SVM with gaussian kernel

Build Status Coverage Status

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 = require('js-svm');
var iris = require('js-datasets-iris');

var svm = new jssvm.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 otherwise
   if(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 = new jssvm.BinarySvmClassifier({
   alpha: 0.01, // learning rate
   iterations: 1000, // maximum iterations
   C: 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 = new jssvm.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 species
   if(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 = new jssvm.MultiClassSvmClassifier({
   alpha: 0.01, // learning rate
   iterations: 1000, // maximum iterations
   C: 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 = new jssvm.BinarySvmClassifier({
   ...,
   kernel: 'gaussian'
});

....

var svm = new jssvm.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

The demo code in HTML can be found in the following files within the package:

js-svm's People

Contributors

chen0040 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.