mljs / svm

Support Vector Machine in Javascript
MIT License
37 stars 7 forks source link

Wrong prediction #3

Open eveningkid opened 5 years ago

eveningkid commented 5 years ago

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!

stropitek commented 5 years ago

Hello!

I don't think I can find time to dig into this.

But there is a build of libsvm in asm which is a subset of javascript so it is actually plain javascript. You should be able to use it for your project.

eveningkid commented 5 years ago

Right, I did try this as well but turned out that it would fail once running in React Native...just thought I would switch back to a simpler version.

Anyways, thanks for your reply.

LimaBD commented 1 week ago

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!

This is not a issue,model.predictOne is designed to return 1 or -1 always, and model.predict depends on this method.

You can see the model.predictOne implementation here:

https://github.com/mljs/svm/blob/392a1d09b33faa3be43dbb119114642cba3a9c64/src/svm.js#L204-L207