.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Algorithm::SVM 3pm" .TH Algorithm::SVM 3pm "2020-11-08" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Algorithm::SVM \- Perl bindings for the libsvm Support Vector Machine library. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Algorithm::SVM; \& \& # Load the model stored in the file \*(Aqsample.model\*(Aq \& $svm = new Algorithm::SVM(Model => \*(Aqsample.model\*(Aq); \& \& # 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(\*(Aqnew\-sample.model\*(Aq); \& \& # Load a saved model from a file. \& $svm\->load(\*(Aqnew\-sample.model\*(Aq); \& \& # 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(); .Ve .SH "DESCRIPTION" .IX Header "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. .SH "CONSTRUCTOR" .IX Header "CONSTRUCTOR" .Vb 2 \& # Load an existing SVM. \& $svm = new Algorithm::SVM(Model => \*(Aqsample.model\*(Aq); \& \& # Create a new SVM with the specified parameters. \& $svm = new Algorithm::SVM(Type => \*(AqC\-SVC\*(Aq, \& Kernel => \*(Aqradial\*(Aq, \& Gamma => 64, \& C => 8); .Ve .PP An Algorithm::SVM object can be created in one of two ways \- an existing \&\s-1SVM\s0 can be loaded from a file, or a new \s-1SVM\s0 can be created an trained on a dataset. .PP An existing \s-1SVM\s0 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 \f(CW$svm\fR\->\fBsave()\fR method. .PP New \s-1SVM\s0's can be created using the following parameters: .PP .Vb 3 \& Type \- The type of SVM that should be created. Possible values are: \& \*(AqC\-SVC\*(Aq, \*(Aqnu\-SVC\*(Aq, \*(Aqone\-class\*(Aq, \*(Aqepsilon\-SVR\*(Aq and \*(Aqnu\-SVR\*(Aq. \& Default os \*(AqC\-SVC\*(Aq. \& \& Kernel \- The type of kernel to be used in the SVM. Possible values \& are: \*(Aqlinear\*(Aq, \*(Aqpolynomial\*(Aq, \*(Aqradial\*(Aq and \*(Aqsigmoid\*(Aq. \& Default is \*(Aqradial\*(Aq. \& \& 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\*(Aqs, one\-class SVM\*(Aqs \& and nu\-SVR SVM\*(Aqs. Default is 0.5. \& \& Epsilon \- Sets the epsilon in the loss function of epsilon\-SVR\*(Aqs. \& Default is 0.1. .Ve .PP For a more detailed explanation of what the above parameters actually do, refer to the documentation distributed with libsvm. .SH "METHODS" .IX Header "METHODS" .Vb 8 \& $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(); .Ve .PP The Algorithm::SVM object provides accessor methods for the various \s-1SVM\s0 parameters. When a value is provided to the method, the object will attempt to set the corresponding \s-1SVM\s0 parameter. If no value is provided, the current value will be returned. See the constructor documentation for a description of appropriate values. .PP 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 \s-1SVM\s0 if you've previously trained the \&\s-1SVM\s0 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. .PP .Vb 1 \& $res = $svm\->predict($ds); .Ve .PP 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. .PP .Vb 1 \& $res = $svm\->predict_value($ds); .Ve .PP The predict_value method works similar to predict, but returns a floating point value corresponding to the output of the trained \&\s-1SVM.\s0 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. .PP .Vb 1 \& $svm\->save($filename); .Ve .PP Saves the currently loaded model to the specified filename. Returns a false value on failure, and truth value on success. .PP .Vb 1 \& $svm\->load($filename); .Ve .PP Loads a model from the specified filename. Returns a false value on failure, and truth value on success. .PP .Vb 1 \& $svm\->train(@tset); .Ve .PP Trains the \s-1SVM\s0 on a set of Algorithm::SVM::DataSet objects. \f(CW@tset\fR should be an array of Algorithm::SVM::DataSet objects. .PP .Vb 1 \& $accuracy = $svm\->validate(5); .Ve .PP 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. .PP .Vb 1 \& $num = $svm\->getNRClass(); .Ve .PP For a classification model, this function gives the number of classes. For a regression or a one-class model, 2 is returned. .PP .Vb 1 \& (@labels) = $svm\->getLabels(); .Ve .PP For a classification model, this function returns the name of the labels in an array. For regression and one-class models undef is returned. .PP .Vb 1 \& $prob = $svm\->getSVRProbability(); .Ve .PP 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) .PP If the model is not for svr or does not contain required information, undef is returned. .SH "MAINTAINER" .IX Header "MAINTAINER" Matthew Laird Alexander K. Seewald .SH "SEE ALSO" .IX Header "SEE ALSO" Algorithm::SVM::DataSet and the libsvm homepage: http://www.csie.ntu.edu.tw/~cjlin/libsvm/ .SH "ACKNOWLEDGEMENTS" .IX Header "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. .PP 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!