Giter Club home page Giter Club logo

svm's Introduction

ml-svm

NPM version build status David deps npm download

Support Vector Machines in Javascript

⚠️ ⚠️ This is a simplified implementation of SVM, primarily meant for students to understand the algorithm. For real world applications, please check out libsvm-js ⚠️ ⚠️

Implementation of this simplified Sequential Minimization Optimization algorithm

Installation

npm install ml-svm

API

API documentation

Example

// Instantiate the svm classifier
var SVM = require('ml-svm');

var options = {
  C: 0.01,
  tol: 10e-4,
  maxPasses: 10,
  maxIterations: 10000,
  kernel: 'rbf',
  kernelOptions: {
    sigma: 0.5
  }
};

var svm = new SVM(options);

// Train the classifier - we give him an xor
var features = [[0,0],[0,1],[1,1],[1,0]];
var labels = [1, -1, 1, -1];
svm.train(features, labels);

// Let's see how narrow the margin is
var margins = svm.margin(features);

// Let's see if it is separable by testing on the training data
svm.predict(features); // [1, -1, 1, -1]

// I want to see what my support vectors are
var supportVectors = svm.supportVectors();
 
// Now we want to save the model for later use
var model = svm.toJSON();

/// ... later, you can make predictions without retraining the model
var importedSvm = SVM.load(model);
importedSvm.predict(features); // [1, -1, 1, -1] 

Authors

License

MIT

svm's People

Contributors

greenkeeperio-bot avatar maasencioh avatar stropitek avatar targos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

svm's Issues

Wrong prediction

Hello,

I am trying to make a simple classification using SVM with the following snippet:

const SVM = require('ml-svm');

const X = [[-1, -1], [-2, -1], [1, 1], [2, 1]];
const y = [1, 1, 2, 2];

const model = new SVM({
  C: 1.0,
  tol: 0.0001,
});

model.train(X, y);
model.predict([[-0.8, -1]]);
// => sometimes -1 (wrong), sometimes 1 (correct)

I don't understand why it sometimes predict -1, which is not even an available class...
It happens randomly so I thought it may be related to whether or not the algorithm converged?

I really hope to use this library as it runs in React Native using plain JS (which libsvm doesn't)! :(

Do you know what could be the problem?

Thank you!

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.