SVM

Support Vector Machines (SVM) are a set of supervised learning algorithms used for classification and regression. DolphinDB offers an SVM plugin that allows users to train and predict SVM models directly on DolphinDB objects. This plugin is implemented based on libsvm, encapsulating common SVM algorithms.

Installation (with installPlugin)

Required server version: DolphinDB 2.00.10 or higher.

Supported OS: Windows x86-64 and Linux x86-64.

Installation Steps:

(1) Use listRemotePlugins to check plugin information in the plugin repository.

Note: For plugins not included in the provided list, you can install through precompiled binaries or compile from source. These files can be accessed from our GitHub repository by switching to the appropriate version branch.

login("admin", "123456")
listRemotePlugins(, "http://plugins.dolphindb.com/plugins/")

(2) Invoke installPlugin for plugin installation.

installPlugin("SVM")

(3) Use loadPlugin to load the plugin before using the plugin methods.

loadPlugin("SVM")

Method References

fit

Syntax

fit(Y, X, [para=None])

Details

The method trains SVM models and returns an SVM object.

Parameters

  • Y: An INT or DOUBLE vector indicating the target values.
  • X: A DOUBLE matrix, table or vector indicating input training data.
    • Matrix: Each column is a sample, with elements as feature values.
    • Table: Each row is a sample, and all columns must be DOUBLE type.
    • Vector: Automatically divided into samples based on Y's length.
  • para (optional): A dictionary with STRING keys and ANY values indicating SVM training parameters. It includes the following key values:
    • "type": SVM type. Values can be "NuSVC", "NuSVR", "OneClass", "SVC", "SVR".
    • "kernel": The kernel function type. Values can be "linear", "poly", "rbf", "sigmoid", or "precomputed".
    • "degree": Degree of the kernel function. The value is INT.
    • "gamma": Gamma parameter of the kernel function. The value can be "scale" or DOUBLE.
    • "coef0": Coef0 parameter of the kernel. The value is DOUBLE and the default is 0.
    • "C": Cost parameter of C-SVC, epsilon-SVR, and nu-SVR. The value is DOUBLE and the default is 1.
    • "epsilon": Epsilon parameter of epsilon-SVR. The value is DOUBLE and the default is 0.1.
    • "shrinking": A boolean indicating whether to use shrinking heuristics. The default is true.
    • "cache_size": The size of the kernel cache in MB. The value is DOUBLE and the default is 100.
    • "verbose": A boolean indicating whether verbose output is enabled. The default is true.
    • "nu": Upper bound on the fraction of margin errors and a lower bound of the fraction of support vectors. The range is (0,1] and the default value is 0.5.

predict

Syntax

predict(SVMobject, X)

Details The method performs classification or regression using an SVM model on the provided test data. It returns a vector containing the predicted sample labels or regression values.

Parameters

  • SVMobject: An SVM object.
  • X: A DOUBLE matrix, table, or vector containing the input test data.

score

Syntax

score(SVMobject, Y, X)

Details The method calculates the accuracy of an existing SVM model using provided test data and labels, returning statistical metrics. The SVM model is represented by an SVM object. For classification models, it returns the prediction accuracy. For regression models, it returns MSE (Mean Squared Error) and R² (Coefficient of Determination).

Parameters

  • SVMobject: An SVM object.
  • Y: A vector of actual target values.
  • X: A matrix, table, or vector indicating the input test data matrix.

saveModel

Syntax

saveModel(SVMobject, location)

Details The method saves a trained SVM model to a file and returns a boolean value indicating whether the model was successfully saved.

Parameters

  • SVMobject: An SVM object.
  • location: A STRING scalar indicating the file path.

loadModel

Syntax

loadModel(location)

Details The method loads an SVM model from a file into memory and returns an SVM object.

Parameters

  • location: A STRING scalar indicating the file path.

Usage Examples

SVM Classification Model

Train the model:

path="/path/to/PluginSVM.txt";
modelPath="/path/to/mymodel"
loadPlugin(path)
X = matrix(-1.0 -1.0,-2.0 -1.0, 1.0 1.0, 2.0 1.0)
Y = 1.0 1.0 2.0 2.0
clf = svm::fit(Y, X)

Predict with the model:

svm::predict(clf, X)
// output: [1,1,2,2]

Evaluate the model:

svm::score(clf, X);

// output: 1

Save the model:

svm::saveModel(clf, modelPath)

SVM Regression Model

Train the model:

path="/path/to/PluginSVM.txt";
modelPath="/path/to/mymodel";
loadPlugin(path);
X = table(1 3 5 7 11 16 23 as X)
Y = 0.1 4.2 5.6 8.8 22.1 35.6 77.2
regr = svm::fit(Y, X, {type: "SVR"})

Evaluate the model:

svm::score(regr, Y, X);

// output: MSE->797.772
// output: R2->0.582937