# 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
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 SVMs 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 SVMs, one-class SVMs
and nu-SVR SVMs. Default is 0.5.
Epsilon - Sets the epsilon in the loss function of epsilon-SVRs.
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 youve previously trained the
SVM on a dataset. (ie. You cant 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
If the model is not for svr or does not contain required information,
undef is returned.