NAME
Algorithm::LibLinear - A Perl binding for LIBLINEAR, a library for classification/regression using linear SVM and logistic regression.
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
...
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.47, released on July 9, 2023.
METHODS
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:
- bias
-
Bias term to be added to prediction result (i.e.,
-B
option for LIBLINEAR'strain
command.).This parameter makes sense only when its value is positive.
- cost
-
Penalty cost for misclassification (
-c
.) - epsilon
-
Termination criterion (
-e
.)Default value of this parameter depends on the value of
solver
. - loss_sensitivity
-
Epsilon in loss function of SVR (
-p
.) - nu
-
Nu parameter of one-class SVM (
-n
.) - regularize_bias
-
Whether to regularize the bias term (
-R
, negated.) - solver
-
Kind of solver (
-s
.)For classification:
- 'L2R_LR' - L2-regularized logistic regression
- 'L2R_L2LOSS_SVC_DUAL' - L2-regularized L2-loss SVC (dual problem)
- 'L2R_L2LOSS_SVC' - L2-regularized L2-loss SVC (primal problem)
- 'L2R_L1LOSS_SVC_DUAL' - L2-regularized L1-loss SVC (dual problem)
- 'MCSVM_CS' - Crammer-Singer multi-class SVM
- 'L1R_L2LOSS_SVC' - L1-regularized L2-loss SVC
- 'L1R_LR' - L1-regularized logistic regression (primal problem)
- 'L1R_LR_DUAL' - L1-regularized logistic regression (dual problem)
For regression:
- 'L2R_L2LOSS_SVR' - L2-regularized L2-loss SVR (primal problem)
- 'L2R_L2LOSS_SVR_DUAL' - L2-regularized L2-loss SVR (dual problem)
- 'L2R_L1LOSS_SVR_DUAL' - L2-regularized L1-loss SVR (dual problem)
For outlier detection:
- weights
-
Weights to adjust the cost parameter of different classes (
-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.
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.
find_cost_parameter(data_set => $data_set, num_folds => $num_folds [, initial => -1.0] [, update => 0])
Deprecated. Use find_parameters
instead.
Shorthand alias for find_parameters
only works on cost
parameter. Notice that loss_sensitivity
is affected too when update
is set.
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 initial_cost
or initial_loss_sensitivity
is a negative, the value is automatically calculated. Works only for 3 solvers: 'L2R_LR'
, 'L2R_L2LOSS_SVC'
and 'L2R_L2LOSS_SVR'
. Error will be thrown for otherwise.
When update
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 cost
, found loss_sensitivity
(only if solver is 'L2R_L2LOSS_SVR'
) and mean accuracy of cross validation with the found parameters.
train(data_set => $data_set)
Executes training and returns a trained Algorithm::LibLinear::Model instance. data_set
is same as the cross_validation
's.
AUTHOR
Koichi SATO <sekia@cpan.org>
SEE ALSO
Algorithm::LibLinear::FeatureScaling
Algorithm::SVM - A Perl binding to LIBSVM.
LICENSE
Algorithm::LibLinear
Copyright (c) 2013-2023 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.
LIBLINEAR
Copyright (c) 2007-2023 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.