1Algorithm::SVM(3) User Contributed Perl Documentation Algorithm::SVM(3)
2
3
4
6 Algorithm::SVM - Perl bindings for the libsvm Support Vector Machine
7 library.
8
10 use Algorithm::SVM;
11
12 # Load the model stored in the file 'sample.model'
13 $svm = new Algorithm::SVM(Model => 'sample.model');
14
15 # Classify a dataset.
16 $ds1 = new Algorithm::SVM::DataSet(Label => 1,
17 Data => [0.12, 0.25, 0.33, 0.98]);
18 $res = $svm->predict($ds);
19
20 # Train a new SVM on some new datasets.
21 $svm->train(@tset);
22
23 # Change some of the SVM parameters.
24 $svm->gamma(64);
25 $svm->C(8);
26 # Retrain the SVM with the new parameters.
27 $svm->retrain();
28
29 # Perform cross validation on the training set.
30 $accuracy = $svm->validate(5);
31
32 # Save the model to a file.
33 $svm->save('new-sample.model');
34
35 # Load a saved model from a file.
36 $svm->load('new-sample.model');
37
38 # Retreive the number of classes.
39 $num = $svm->getNRClass();
40
41 # Retreive labels for dataset classes
42 (@labels) = $svm->getLabels();
43
44 # Probabilty for regression models, see below for details
45 $prob = $svm->getSVRProbability();
46
48 Algorithm::SVM implements a Support Vector Machine for Perl. Support
49 Vector Machines provide a method for creating classifcation functions
50 from a set of labeled training data, from which predictions can be made
51 for subsequent data sets.
52
54 # Load an existing SVM.
55 $svm = new Algorithm::SVM(Model => 'sample.model');
56
57 # Create a new SVM with the specified parameters.
58 $svm = new Algorithm::SVM(Type => 'C-SVC',
59 Kernel => 'radial',
60 Gamma => 64,
61 C => 8);
62
63 An Algorithm::SVM object can be created in one of two ways - an
64 existing SVM can be loaded from a file, or a new SVM can be created an
65 trained on a dataset.
66
67 An existing SVM is loaded from a file using the Model named parameter.
68 The model file should be of the format produced by the svm-train
69 program (distributed with the libsvm library) or from the $svm->save()
70 method.
71
72 New SVM's can be created using the following parameters:
73
74 Type - The type of SVM that should be created. Possible values are:
75 'C-SVC', 'nu-SVC', 'one-class', 'epsilon-SVR' and 'nu-SVR'.
76 Default os 'C-SVC'.
77
78 Kernel - The type of kernel to be used in the SVM. Possible values
79 are: 'linear', 'polynomial', 'radial' and 'sigmoid'.
80 Default is 'radial'.
81
82 Degree - Sets the degree in the kernel function. Default is 3.
83
84 Gamma - Sets the gamme in the kernel function. Default is 1/k,
85 where k is the number of training sets.
86
87 Coef0 - Sets the Coef0 in the kernel function. Default is 0.
88
89 Nu - Sets the nu parameter for nu-SVC SVM's, one-class SVM's
90 and nu-SVR SVM's. Default is 0.5.
91
92 Epsilon - Sets the epsilon in the loss function of epsilon-SVR's.
93 Default is 0.1.
94
95 For a more detailed explanation of what the above parameters actually
96 do, refer to the documentation distributed with libsvm.
97
99 $svm->degree($degree);
100 $svm->gamma($gamma);
101 $svm->coef0($coef0);
102 $svm->C($C);
103 $svm->nu($nu);
104 $svm->epsilon($epsilon);
105 $svm->kernel_type($ktype);
106 $svm->svm_type($svmtype);
107
108 $svm->retrain();
109
110 The Algorithm::SVM object provides accessor methods for the various SVM
111 parameters. When a value is provided to the method, the object will
112 attempt to set the corresponding SVM parameter. If no value is
113 provided, the current value will be returned. See the constructor
114 documentation for a description of appropriate values.
115
116 The retrain method should be called if any of the parameters are
117 modified from their initial values so as to rebuild the model with the
118 new values. Note that you can only retrain an SVM if you've previously
119 trained the SVM on a dataset. (ie. You can't currently retrain a model
120 loaded with the load method.) The method will return a true value if
121 the retraining was successful and a false value otherwise.
122
123 $res = $svm->predict($ds);
124
125 The predict method is used to classify a set of data according to the
126 loaded model. The method accepts a single parameter, which should be
127 an Algorithm::SVM::DataSet object. Returns a floating point number
128 corresponding to the predicted value.
129
130 $res = $svm->predict_value($ds);
131
132 The predict_value method works similar to predict, but returns a
133 floating point value corresponding to the output of the trained SVM.
134 For a linear kernel, this can be used to reconstruct the weights for
135 each attribute as follows: the bias of the linear function is returned
136 when calling predict_value on an empty dataset (all zeros), and by
137 setting each variable in turn to one and all others to zero, you get
138 one value per attribute which corresponds to bias + weight_i. By
139 subtracting the bias, the final linear model is obtained as sum of
140 (weight_i * attr_i) plus bias. The sign of this value corresponds to
141 the binary prediction.
142
143 $svm->save($filename);
144
145 Saves the currently loaded model to the specified filename. Returns a
146 false value on failure, and truth value on success.
147
148 $svm->load($filename);
149
150 Loads a model from the specified filename. Returns a false value on
151 failure, and truth value on success.
152
153 $svm->train(@tset);
154
155 Trains the SVM on a set of Algorithm::SVM::DataSet objects. @tset
156 should be an array of Algorithm::SVM::DataSet objects.
157
158 $accuracy = $svm->validate(5);
159
160 Performs cross validation on the training set. If an argument is
161 provided, the set is partioned into n subsets, and validated against
162 one another. Returns a floating point number representing the accuracy
163 of the validation.
164
165 $num = $svm->getNRClass();
166
167 For a classification model, this function gives the number of classes.
168 For a regression or a one-class model, 2 is returned.
169
170 (@labels) = $svm->getLabels();
171
172 For a classification model, this function returns the name of the
173 labels in an array. For regression and one-class models undef is
174 returned.
175
176 $prob = $svm->getSVRProbability();
177
178 For a regression model with probability information, this function
179 outputs a value sigma > 0. For test data, we consider the probability
180 model: target value = predicted value + z, z: Laplace distribution
181 e^(-|z|/sigma)/2sigma)
182
183 If the model is not for svr or does not contain required information,
184 undef is returned.
185
187 Matthew Laird <matt@brinkman.mbb.sfu.ca> Alexander K. Seewald
188 <alex@seewald.at>
189
191 Algorithm::SVM::DataSet and the libsvm homepage:
192 http://www.csie.ntu.edu.tw/~cjlin/libsvm/
193
195 Thanks go out to Fiona Brinkman and the other members of the Simon
196 Fraser University Brinkman Laboratory for providing me the opportunity
197 to develop this module. Additional thanks go to Chih-Jen Lin, one of
198 the libsvm authors, for being particularly helpful during the
199 development process.
200
201 As well to Dr. Alexander K. Seewald of Seewald Solutions for many bug
202 fixes, new test cases, and lowering the memory footprint by a factor of
203 20. Thank you very much!
204
205
206
207perl v5.30.0 2019-07-26 Algorithm::SVM(3)