Algorithm::SVM(3pm)
π Algorithm::SVM β the Perl bindings for the libsvm Support Vector Machine library π
okay bestie, gather round, because this is <pink>Algorithm::SVM</pink>, a page from the User Contributed Perl Documentation section of the manual, and it's about to become your whole personality. π¦
Perl bindings for the libsvm Support Vector Machine library. That's the vibe. π
β¨ NAME β¨
Algorithm::SVM β Perl bindings for the libsvm Support Vector Machine library.
(yeah that's the name section and it's literally just the name, we love a low-effort icon π)
πΈ SYNOPSIS πΈ
Here's the whole thing in action, no edits, exactly as it ships:
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');
# Retrieve the number of classes.
$num = $svm->getNRClass();
# Retrieve labels for dataset classes
(@labels) = $svm->getLabels();
# Probabilty for regression models, see below for details
$prob = $svm->getSVRProbability();
no cap, that's the whole workflow right there. load it, classify it, train it, tune it, retrain it, validate it, save it, load it again, then go fishing for class counts, labels and regression probabilities. main character energy from start to finish. π
π§ DESCRIPTION π§
Algorithm::SVM implements a Support Vector Machine for Perl. Support Vector Machines give you a method for creating classification functions from a set of labeled training data β and once you've got those functions, you can make predictions for subsequent data sets. literally it learns from the labeled stuff and then guesses on the new stuff. iconic behavior, honestly. π
π CONSTRUCTOR π
Two ways in. Pick your fighter:
# 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 and trained on a dataset.
An existing SVM gets loaded from a file using the Model named parameter. That model file should be in the format produced by the svm-train program (which ships with the libsvm library), or the format that comes out of the $svm->save() method. πΎ
New SVM's can be created using these parameters right here:
Typeβ the type of SVM that should be created. Possible values are:'C-SVC','nu-SVC','one-class','epsilon-SVR'and'nu-SVR'. Default is'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 is3.Gammaβ sets the gamme in the kernel function. Default is1/k, where k is the number of training sets.Coef0β sets theCoef0in the kernel function. Default is0.Nuβ sets the nu parameter for nu-SVC SVM's, one-class SVM's and nu-SVR SVM's. Default is0.5.Epsilonβ sets the epsilon in the loss function of epsilon-SVR's. Default is0.1.
For a more detailed explanation of what the above parameters actually do, refer to the documentation distributed with libsvm. bestie, the source is right there, go read it π«Ά
π METHODS π
Here's your accessor lineup:
$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. (yes, getter AND setter, one method, doing double duty, we stan a multitasker β¨)
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: 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.
(that's a whole little protocol, bestie β empty dataset for the bias, one-hot each variable for bias + weight_i, subtract, done. forensic. π)
$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. feed it the whole array, it'll eat. π
$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. π (the Laplace distribution said "I'll be there, but with conditions")
π«Ά MAINTAINER π«Ά
Matthew Laird <matt@brinkman.mbb.sfu.ca> and Alexander K. Seewald <alex@seewald.at> β the ones holding this whole thing down. π
π₯ SEE ALSO π₯
Algorithm::SVM::DataSet and the libsvm homepage: http://www.csie.ntu.edu.tw/~cjlin/libsvm/ β go click around, iykyk. π
π 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.
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! π₯ (a factor of 20, bestie. that's not a tweak, that's a glow-up.)
and that's the page!! no notes, we ate, periodt. π β¨
π cute sign-off time: this one's from perl v5.42.3, dated 2026-08-19. so go forth, train your SVM, tune your gamma, retrain responsibly, and remember β you can't retrain a loaded model, and that's not a personal failing, that's just the rules. π¦ππ
π¦ where this page comes from
- ubuntu stonking: libalgorithm-svm-perl 0.13-5build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms