gitmyhub

svmjs

JavaScript ★ 709 updated 8y ago

Support Vector Machine in Javascript (SMO algorithm, supports arbitrary kernels) + GUI demo

svmjs is a lightweight JavaScript library for training a Support Vector Machine classifier entirely in the browser or Node.js, letting you build interactive machine-learning demos without a Python backend.

JavaScriptsetup: easycomplexity 3/5

What This Does

svmjs lets you train a machine learning classifier entirely in JavaScript—right in your browser or Node.js server. A classifier learns to sort data into two categories (like "spam" or "not spam") by studying labeled examples. Once trained, it can predict which category new data belongs to. The practical benefit is that you can build interactive demos or tools that teach machine learning concepts without needing a Python backend or specialized ML infrastructure.

How It Works

The library uses an algorithm called SMO (Sequential Minimal Optimization) to train what's called a Support Vector Machine. In plain terms: you give it a list of examples with correct answers, and it finds a mathematical boundary that separates one group from another. What makes this implementation special is that it supports "kernels"—different ways of measuring similarity between data points. A linear kernel is the simplest and fastest; an RBF kernel is more flexible and can handle curved boundaries, but slower. You can even write your own kernel function if you want something custom.

Who Would Use This

Anyone building an interactive machine learning demo or educational tool would benefit. For example, you could create a web app where users draw points on a canvas, label them as two categories, and instantly see the decision boundary that an SVM learns. Data scientists prototyping ideas in a Jupyter notebook could use it to experiment without leaving JavaScript. It's also useful for anyone who wants to include ML classification directly in a frontend without calling an external API.

What's Notable

This is intentionally lightweight and space-efficient—it won't crash even with moderately large datasets. The tradeoff is speed: the algorithm is somewhat naive about choosing which calculations to optimize next, so complex problems might need a lot of iterations. The author recommends starting with a linear kernel first, then trying RBF if that doesn't work well, and tuning a parameter called C to balance how strictly the model fits your training data. The library also lets you save and load trained models as JSON, so you can train once and deploy the model elsewhere.

Where it fits