Home Reference Source
public class | source

SVM

Extends:

EstimatorClassifierOneVsAllClassifier → SVM

Support Vector Machine (SVM) classification model for 2 or more classes. The model is a one-vs-all classifier and uses the BinarySVM classifier as its base model. For training individual models, a simplified version of John Platt's SMO algorithm is used.

Support Vector Machines (SVM)

Support Vector Machines train a classifier by finding the decision boundary between the classes that maximizes the margin between the boundary and the data points on either side of it. It is a very intuitive approach to classification. The soft-margin SVM is a modification of this approach where samples are allowed to be misclasiffied, but at some cost. Furthermore, SVMs can implement the "kernel trick", where an implicit feature transformation is applied.

This is all implemented in the SVM implementation in JSMLT. For more information about Support Vector Machines, you can start by checking out the Wikipedia page on SVMs.

Examples

Example 1: Training a multiclass SVM on a well-known three-class dataset, the Iris dataset.

See:

Example:

Example 1. SVM training on a multiclass classification task.
// Import JSMLT
var jsmlt = require('@jsmlt/jsmlt');

// Load the iris dataset. When loading is completed, process the data and run the classifier
jsmlt.Datasets.loadIris((X, y_raw) => {
  // Encode the labels (which are strings) into integers
  var labelencoder = new jsmlt.Preprocessing.LabelEncoder();
  var y = labelencoder.encode(y_raw);

  // Split the data into a training set and a test set
  [X_train, y_train, X_test, y_test] = jsmlt.ModelSelection.trainTestSplit([X, y]);

  // Create and train classifier
  var clf = new jsmlt.Supervised.SVM.SVM({
    kernel: new jsmlt.Kernel.Gaussian(1),
  });
  clf.train(X_train, y_train);

  // Make predictions on test data
  var predictions = clf.predict(X_test);

  // Evaluate and output the classifier's accuracy
  var accuracy = jsmlt.Validation.Metrics.accuracy(predictions, y_test);
  console.log(`Accuracy: ${accuracy}`);
});

Constructor Summary

Public Constructor
public

constructor(optionsUser: Object, optionsUser: Object)

Constructor.

Member Summary

Public Members
public

numErrors: Array<mixed>

Number of errors per iteration.

public
public

training: {"X": *, "y": *}

Method Summary

Public Methods
public
public

Retrieve the indices of the support vector samples.

public

train(X: *, y: *)

Inherited Summary

From class Estimator
public abstract

predict(X: Array<Array<number>>): Array<mixed>

Make a prediction for a data set.

public abstract

train(X: Array<Array<number>>, y: Array<mixed>)

Train the supervised learning algorithm on a dataset.

From class OneVsAllClassifier
public
public abstract

createClassifier(classIndex: number): BinaryClassifier

Create a binary classifier for one of the classes.

public

Create all binary classifiers.

public

Get the class labels corresponding with each internal class label.

public

Retrieve the individual binary one-vs-all classifiers.

public

predict(X: *): *

public

predictProba(X: Array.Array<number>): Array.Array<number>

Make a probabilistic prediction for a data set.

public

trainBatch(X: Array<Array<number>>, y: Array<mixed>)

Train all binary classifiers one-by-one

public

Train all binary classifiers iteration by iteration, i.e.

Public Constructors

public constructor(optionsUser: Object, optionsUser: Object) source

Constructor. Initialize class members and store user-defined options.

Params:

NameTypeAttributeDescription
optionsUser Object

User-defined options for SVM. Options are passed to created BinarySVM objects. See BinarySVM.constructor() for more details

optionsUser Object
  • optional

User-defined options for SVM

optionsUser.C number
  • optional
  • default: 100

Regularization (i.e. penalty for slack variables)

optionsUser.kernel Object
  • optional

Kernel. Defaults to the linear kernel

optionsUser.convergenceNumPasses number
  • optional
  • default: 10

Number of passes without alphas changing to treat the algorithm as converged

optionsUser.numericalTolerance number
  • optional
  • default: 1e-6

Numerical tolerance for a value in the to be equal to another SMO algorithm to be equal to another value

optionsUser.useKernelCache boolean
  • optional
  • default: true

Whether to cache calculated kernel values for training sample pairs. Enabling this option (which is the default) generally improves the performance in terms of speed at the cost of memory

See:

Public Members

public numErrors: Array<mixed> source

Number of errors per iteration. Only used if accuracy tracking is enabled

public optionsUser: * source

public training: {"X": *, "y": *} source

Public Methods

public createClassifier(): * source

Create a binary classifier for one of the classes.

Override:

OneVsAllClassifier#createClassifier

Return:

*

See:

  • OneVsAll#createClassifier

public getSupportVectors(): Array<number> source

Retrieve the indices of the support vector samples.

Return:

Array<number>

List of sample indices of the support vectors

public train(X: *, y: *) source

Train the supervised learning algorithm on a dataset.

Override:

Estimator#train

Params:

NameTypeAttributeDescription
X *
y *

See: