Home Reference Source
import {BinarySVM} from '@jsmlt/jsmlt/src/supervised/svm/svm.js'
public class | source

BinarySVM

Extends:

EstimatorClassifier → BinarySVM

SVM learner for binary classification problem.

Constructor Summary

Public Constructor
public

constructor(optionsUser: Object)

Constructor.

Member Summary

Public Members
public

C: *

public

alphas: *

public
public
public
public

kernel: *

public
public
public
public
public

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

public

Method Summary

Public Methods
public

Apply the kernel to two data points.

public

Calculate the bounds on \alpha_j to make sure it can be clipped to the [0,C] box and that it can be chosen to satisfy the linear equality constraint stemming from the fact that the sum of all products y_i * a_i should equal 0.

public

Get the signed value of the class index.

public

Get the class index corresponding to a sign.

public

Retrieve the indices of the support vector samples.

public

predict(features: Array<Array<mixed>>, optionsUser: Object): Array<mixed>

Make a prediction for a data set.

public

Calculate the margin (distance to the decision boundary) for a single sample.

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.

Public Constructors

public constructor(optionsUser: Object) source

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

Params:

NameTypeAttributeDescription
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
  • default: null

Kernel. Defaults to the linear kernel

optionsUser.convergenceNumPasses number
  • optional
  • default: 20

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

Public Members

public C: * source

public alphas: * source

public b: number source

public convergenceNumPasses: * source

public isTraining: boolean source

public kernel: * source

public kernelCache: * source

public kernelCacheStatus: * source

public numericalTolerance: * source

public supportVectors: * source

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

public useKernelCache: * source

Public Methods

public applyKernel(x: Array<number> | number, y: Array<number> | number): number source

Apply the kernel to two data points. Accepts both feature arrays and training data point indices for x and y. If x and y are integers, attempts to fetch the kernel result for the corresponding training data points from cache, and computes and stores the result in cache if it isn't found

Params:

NameTypeAttributeDescription
x Array<number> | number

Feature vector or data point index for first data point. Arrays are treated as feature vectors, integers as training data point indices

y Array<number> | number

Feature vector or data point index for second data point. Arrays are treated as feature vectors, integers as training data point indices

Return:

number

Kernel result

public calculateAlphaBounds(i: number, j: number): Array<number> source

Calculate the bounds on \alpha_j to make sure it can be clipped to the [0,C] box and that it can be chosen to satisfy the linear equality constraint stemming from the fact that the sum of all products y_i * a_i should equal 0.

Params:

NameTypeAttributeDescription
i number

Index of \alpha_i

j number

Index of \alpha_j

Return:

Array<number>

Two-dimensional array containing the lower and upper bound

public getClassIndexSign(classIndex: number): number source

Get the signed value of the class index. Returns -1 for class index 0, 1 for class index 1.

Params:

NameTypeAttributeDescription
classIndex number

Class index

Return:

number

Sign corresponding to class index

public getSignClassIndex(sign: number): number source

Get the class index corresponding to a sign.

Params:

NameTypeAttributeDescription
sign number

Sign

Return:

number

Class index corresponding to sign

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 predict(features: Array<Array<mixed>>, optionsUser: Object): Array<mixed> source

Make a prediction for a data set.

Override:

Estimator#predict

Params:

NameTypeAttributeDescription
features Array<Array<mixed>>

Features for each data point. Each array element should be an array containing the features of the data point

optionsUser Object
  • optional

Options for prediction

optionsUser.output string
  • optional
  • default: 'classLabels'

Output for predictions. Either "classLabels" (default, output predicted class label), "raw" or "normalized" (both output margin (distance to decision boundary) for each sample)

Return:

Array<mixed>

Predictions. Output dependent on options.output, defaults to class labels

public sampleMargin(sample: Array<number> | number): number source

Calculate the margin (distance to the decision boundary) for a single sample.

Params:

NameTypeAttributeDescription
sample Array<number> | number

Sample features array or training sample index

Return:

number

Distance to decision boundary

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

Train the supervised learning algorithm on a dataset.

Override:

Estimator#train

Params:

NameTypeAttributeDescription
X *
y *

See: