The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

SYNOPSIS

  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.
  $svm->train(@tset);

  # Change some of the SVM parameters.
  $svm->gamma(64);
  $svm->C(8);
  # Retrain the SVM with the new parameters.
  $svm->retrain();

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

  # Save the model to a file.
  $svm->save('new-sample.model');

  # Load a saved model from a file.
  $svm->load('new-sample.model');

DESCRIPTION

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.

CONSTRUCTOR

  # 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.

METHODS

  $svm->degree($degree);
  $svm->gamma($gamma);
  $svm->coef0($coef0);
  $svm->C($C);
  $svm->nu($nu);
  $svm->epsilon($epsilon);
  $svm->kernel_type($ktype);
  $svm->svm_type($svmtype);

  $svm->retrain();

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.

  $svm->save($filename);

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

  $svm->load($filename);

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

  $svm->train(@tset);

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.

AUTHOR

Cory Spencer <cspencer@sfu.ca>

SEE ALSO

Algorithm::SVM::DataSet and the libsvm homepage: http://www.csie.ntu.edu.tw/~cjlin/libsvm/

ACKNOWLEDGEMENTS

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.