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
npm install ml-svm
// Instantiate the svm classifiervarSVM=require('ml-svm');varoptions={C: 0.01,tol: 10e-4,maxPasses: 10,maxIterations: 10000,kernel: 'rbf',kernelOptions: {sigma: 0.5}};varsvm=newSVM(options);// Train the classifier - we give him an xorvarfeatures=[[0,0],[0,1],[1,1],[1,0]];varlabels=[1,-1,1,-1];svm.train(features,labels);// Let's see how narrow the margin isvarmargins=svm.margin(features);// Let's see if it is separable by testing on the training datasvm.predict(features);// [1, -1, 1, -1]// I want to see what my support vectors arevarsupportVectors=svm.supportVectors();// Now we want to save the model for later usevarmodel=svm.toJSON();/// ... later, you can make predictions without retraining the modelvarimportedSvm=SVM.load(model);importedSvm.predict(features);// [1, -1, 1, -1]