SVM
Extends:
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:
// 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 |
Number of errors per iteration. |
|
public |
optionsUser: * |
|
public |
training: {"X": *, "y": *} |
Method Summary
Public Methods | ||
public |
createClassifier(): * |
|
public |
Retrieve the indices of the support vector samples. |
|
public |
train(X: *, y: *) |
Inherited Summary
From class Estimator | ||
public abstract |
Make a prediction for a data set. |
|
public abstract |
Train the supervised learning algorithm on a dataset. |
From class OneVsAllClassifier | ||
public |
classifiers: * |
|
public abstract |
createClassifier(classIndex: number): BinaryClassifier Create a binary classifier for one of the classes. |
|
public |
createClassifiers(y: Array<number>) Create all binary classifiers. |
|
public |
getClasses(): Array<number> 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:
Name | Type | Attribute | Description |
optionsUser | Object | User-defined options for SVM. Options are passed to created BinarySVM objects. See BinarySVM.constructor() for more details |
|
optionsUser | Object |
|
User-defined options for SVM |
optionsUser.C | number |
|
Regularization (i.e. penalty for slack variables) |
optionsUser.kernel | Object |
|
Kernel. Defaults to the linear kernel |
optionsUser.convergenceNumPasses | number |
|
Number of passes without alphas changing to treat the algorithm as converged |
optionsUser.numericalTolerance | number |
|
Numerical tolerance for a value in the to be equal to another SMO algorithm to be equal to another value |
optionsUser.useKernelCache | boolean |
|
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 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#createClassifierReturn:
* |
See:
- OneVsAll#createClassifier
public getSupportVectors(): Array<number> source
Retrieve the indices of the support vector samples.
public train(X: *, y: *) source
Train the supervised learning algorithm on a dataset.
Override:
Estimator#trainParams:
Name | Type | Attribute | Description |
X | * | ||
y | * |