=head1 NAME Algorithm::LibLinear - A Perl binding for LIBLINEAR, a library for classification/regression using linear SVM and logistic regression. =head1 SYNOPSIS use Algorithm::LibLinear; # Constructs a model for L2-regularized L2 loss support vector classification. my $learner = Algorithm::LibLinear->new( cost => 1, epsilon => 0.01, solver => 'L2R_L2LOSS_SVC', weights => [ +{ label => 1, weight => 1, }, +{ label => -1, weight => 1, }, ], ); # Loads a training data set from DATA filehandle. my $data_set = Algorithm::LibLinear::DataSet->load(fh => \*DATA); # Updates training parameter. $learner->find_parameters(data_set => $data_set, num_folds => 5, update => 1); # Executes cross validation. my $accuracy = $learner->cross_validation(data_set => $data_set, num_folds => 5); # Executes training. my $classifier = $learner->train(data_set => $data_set); # Determines which (+1 or -1) is the class for the given feature to belong. my $class_label = $classifier->predict(feature => +{ 1 => 0.38, 2 => -0.5, ... }); __DATA__ +1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.419847 9:-1 10:-0.225806 12:1 13:-1 -1 1:0.583333 2:-1 3:0.333333 4:-0.603774 5:1 6:-1 7:1 8:0.358779 9:-1 10:-0.483871 12:-1 13:1 +1 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1 -1 1:0.458333 2:1 3:1 4:-0.358491 5:-0.374429 6:-1 7:-1 8:-0.480916 9:1 10:-0.935484 12:-0.333333 13:1 -1 1:0.875 2:-1 3:-0.333333 4:-0.509434 5:-0.347032 6:-1 7:1 8:-0.236641 9:1 10:-0.935484 11:-1 12:-0.333333 13:-1 ... =head1 DESCRIPTION Algorithm::LibLinear is an XS module that provides features of LIBLINEAR, a fast C library for classification and regression. Current version is based on LIBLINEAR 2.42, released on November 1, 2020. =head1 METHODS =head2 new([bias => -1.0] [, cost => 1] [, epsilon => 0.1] [, loss_sensitivity => 0.1] [, nu => 0.5] [, regularize_bias => 1] [, solver => 'L2R_L2LOSS_SVC_DUAL'] [, weights => []]) Constructor. You can set several named parameters: =over 4 =item bias Bias term to be added to prediction result (i.e., C<-B> option for LIBLINEAR's C command.). This parameter makes sense only when its value is positive. =item cost Penalty cost for misclassification (C<-c>.) =item epsilon Termination criterion (C<-e>.) Default value of this parameter depends on the value of C. =item loss_sensitivity Epsilon in loss function of SVR (C<-p>.) =item nu Nu parameter of one-class SVM (C<-n>.) =item regularize_bias Whether to regularize the bias term (C<-R>, negated.) =item solver Kind of solver (C<-s>.) For classification: =over 4 =item 'L2R_LR' - L2-regularized logistic regression =item 'L2R_L2LOSS_SVC_DUAL' - L2-regularized L2-loss SVC (dual problem) =item 'L2R_L2LOSS_SVC' - L2-regularized L2-loss SVC (primal problem) =item 'L2R_L1LOSS_SVC_DUAL' - L2-regularized L1-loss SVC (dual problem) =item 'MCSVM_CS' - Crammer-Singer multi-class SVM =item 'L1R_L2LOSS_SVC' - L1-regularized L2-loss SVC =item 'L1R_LR' - L1-regularized logistic regression (primal problem) =item 'L1R_LR_DUAL' - L1-regularized logistic regression (dual problem) =back For regression: =over 4 =item 'L2R_L2LOSS_SVR' - L2-regularized L2-loss SVR (primal problem) =item 'L2R_L2LOSS_SVR_DUAL' - L2-regularized L2-loss SVR (dual problem) =item 'L2R_L1LOSS_SVR_DUAL' - L2-regularized L1-loss SVR (dual problem) =back For outlier detection: =over 4 =item 'ONECLASS_SVM' - One-class SVM =back =item weights Weights adjust the cost parameter of different classes (C<-wi>.) For example, my $learner = Algorithm::LibLinear->new( weights => [ +{ label => 1, weight => 0.5 }, +{ label => 2, weight => 1 }, +{ label => 3, weight => 0.5 }, ], ); is giving a doubling weight for class 2. This means that samples belonging to class 2 have stronger effect than other samples belonging class 1 or 3 on learning. This option is useful when the number of training samples of each class is not balanced. =back =head2 cross_validation(data_set => $data_set, num_folds => $num_folds) Evaluates training parameter using N-fold cross validation method. Given data set will be split into N parts. N-1 of them will be used as a training set and the rest 1 part will be used as a test set. The evaluation iterates N times using each different part as a test set. Then average accuracy is returned as result. =head2 find_cost_parameter(data_set => $data_set, num_folds => $num_folds [, initial => -1.0] [, update => 0]) Deprecated. Use C instead. Shorthand alias for C only works on C parameter. Notice that C is affected too when C is set. =head2 find_parameters(data_set => $data_set, num_folds => $num_folds [, initial_cost => -1.0] [, initial_loss_sensitivity => -1.0] [, update => 0]) Finds the best parameters by N-fold cross validation. If C or C is a negative, the value is automatically calculated. Works only for 3 solvers: C<'L2R_LR'>, C<'L2R_L2LOSS_SVC'> and C<'L2R_L2LOSS_SVR'>. Error will be thrown for otherwise. When C is set true, the instance is updated to use the found parameters. This behaviour is disabled by default. Return value is an ArrayRef containing 3 values: found C, found C (only if solver is C<'L2R_L2LOSS_SVR'>) and mean accuracy of cross validation with the found parameters. =head2 train(data_set => $data_set) Executes training and returns a trained L instance. C is same as the C's. =head1 AUTHOR Koichi SATO Esekia@cpan.orgE =head1 SEE ALSO L L L L L - A Perl binding to LIBSVM. =head1 LICENSE =head2 Algorithm::LibLinear Copyright (c) 2013-2020 Koichi SATO. All rights reserved. The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =head2 LIBLINEAR Copyright (c) 2007-2020 The LIBLINEAR Project. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither name of copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.