
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Package implements linear svm and kernel svm that supports binary and mult-class classification
Package provides javascript implementation of linear SVM and SVM with gaussian kernel
npm install js-svm
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
});
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
});
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'
});
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:
FAQs
Package implements linear svm and kernel svm that supports binary and mult-class classification
The npm package js-svm receives a total of 51 weekly downloads. As such, js-svm popularity was classified as not popular.
We found that js-svm demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.