The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Algorithm::SVM - Perl bindings for the libsvm Support Vector Machine library.


  use Algorithm::SVM;

  # Load the model stored in the file 'sample.model'
  $svm = new Algorithm::SVM(Model => 'sample.model');

  # Classify a dataset.
  $ds1 = new Algorithm::SVM::DataSet(Label => 1,
                                     Data  => [0.12, 0.25, 0.33, 0.98]);
  $res = $svm->predict($ds);

  # Train a new SVM on some new datasets.

  # Change some of the SVM parameters.
  # Retrain the SVM with the new parameters.

  # Perform cross validation on the training set.
  $accuracy = $svm->validate(5);

  # Save the model to a file.

  # Load a saved model from a file.

  # Retreive the number of classes.
  $num = $svm->getNRClass();

  # Retreive labels for dataset classes
  (@labels) = $svm->getLabels();

  # Probabilty for regression models, see below for details
  $prob = $svm->getSVRProbability();


Algorithm::SVM implements a Support Vector Machine for Perl. Support Vector Machines provide a method for creating classifcation functions from a set of labeled training data, from which predictions can be made for subsequent data sets.


  # Load an existing SVM.
  $svm = new Algorithm::SVM(Model  => 'sample.model');

  # Create a new SVM with the specified parameters.
  $svm = new Algorithm::SVM(Type   => 'C-SVC',
                            Kernel => 'radial',
                            Gamma  => 64,
                            C      => 8);

An Algorithm::SVM object can be created in one of two ways - an existing SVM can be loaded from a file, or a new SVM can be created an trained on a dataset.

An existing SVM is loaded from a file using the Model named parameter. The model file should be of the format produced by the svm-train program (distributed with the libsvm library) or from the $svm->save() method.

New SVM's can be created using the following parameters:

  Type    - The type of SVM that should be created.  Possible values are:
            'C-SVC', 'nu-SVC', 'one-class', 'epsilon-SVR' and 'nu-SVR'.
            Default os 'C-SVC'.

  Kernel  - The type of kernel to be used in the SVM.  Possible values
            are: 'linear', 'polynomial', 'radial' and 'sigmoid'.
            Default is 'radial'.

  Degree  - Sets the degree in the kernel function.  Default is 3.

  Gamma   - Sets the gamme in the kernel function.  Default is 1/k,
            where k is the number of training sets.

  Coef0   - Sets the Coef0 in the kernel function.  Default is 0.

  Nu      - Sets the nu parameter for nu-SVC SVM's, one-class SVM's
            and nu-SVR SVM's.  Default is 0.5.

  Epsilon - Sets the epsilon in the loss function of epsilon-SVR's.
            Default is 0.1.

For a more detailed explanation of what the above parameters actually do, refer to the documentation distributed with libsvm.




The Algorithm::SVM object provides accessor methods for the various SVM parameters. When a value is provided to the method, the object will attempt to set the corresponding SVM parameter. If no value is provided, the current value will be returned. See the constructor documentation for a description of appropriate values.

The retrain method should be called if any of the parameters are modified from their initial values so as to rebuild the model with the new values. Note that you can only retrain an SVM if you've previously trained the SVM on a dataset. (ie. You can't currently retrain a model loaded with the load method.) The method will return a true value if the retraining was successful and a false value otherwise.

  $res = $svm->predict($ds);

The predict method is used to classify a set of data according to the loaded model. The method accepts a single parameter, which should be an Algorithm::SVM::DataSet object. Returns a floating point number corresponding to the predicted value.

  $res = $svm->predict_value($ds);

The predict_value method works similar to predict, but returns a floating point value corresponding to the output of the trained SVM. For a linear kernel, this can be used to reconstruct the weights for each attribute as follows: the bias of the linear function is returned when calling predict_value on an empty dataset (all zeros), and by setting each variable in turn to one and all others to zero, you get one value per attribute which corresponds to bias + weight_i. By subtracting the bias, the final linear model is obtained as sum of (weight_i * attr_i) plus bias. The sign of this value corresponds to the binary prediction.


Saves the currently loaded model to the specified filename. Returns a false value on failure, and truth value on success.


Loads a model from the specified filename. Returns a false value on failure, and truth value on success.


Trains the SVM on a set of Algorithm::SVM::DataSet objects. @tset should be an array of Algorithm::SVM::DataSet objects.

  $accuracy = $svm->validate(5);

Performs cross validation on the training set. If an argument is provided, the set is partioned into n subsets, and validated against one another. Returns a floating point number representing the accuracy of the validation.

  $num = $svm->getNRClass();

For a classification model, this function gives the number of classes. For a regression or a one-class model, 2 is returned.

  (@labels) = $svm->getLabels();

For a classification model, this function returns the name of the labels in an array. For regression and one-class models undef is returned.

  $prob = $svm->getSVRProbability();

For a regression model with probability information, this function outputs a value sigma > 0. For test data, we consider the probability model: target value = predicted value + z, z: Laplace distribution e^(-|z|/sigma)/2sigma)

If the model is not for svr or does not contain required information, undef is returned.


Matthew Laird <> Alexander K. Seewald <>


Algorithm::SVM::DataSet and the libsvm homepage:


Thanks go out to Fiona Brinkman and the other members of the Simon Fraser University Brinkman Laboratory for providing me the opportunity to develop this module. Additional thanks go to Chih-Jen Lin, one of the libsvm authors, for being particularly helpful during the development process.

As well to Dr. Alexander K. Seewald of Seewald Solutions for many bug fixes, new test cases, and lowering the memory footprint by a factor of 20. Thank you very much!