Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong prediction #3

Open
eveningkid opened this issue Apr 7, 2019 · 3 comments
Open

Wrong prediction #3

eveningkid opened this issue Apr 7, 2019 · 3 comments

Comments

@eveningkid
Copy link

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
Copy link
Member

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
Copy link
Author

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
Copy link

LimaBD commented Sep 7, 2024

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:

svm/src/svm.js

Lines 204 to 207 in 392a1d0

SVM.prototype.predictOne = function (p) {
var margin = this.marginOne(p);
return margin > 0 ? 1 : -1;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants