Algorithm-LibLinear

 view release on metacpan or  search on metacpan

lib/Algorithm/LibLinear.pm  view on Meta::CPAN

        Carp::croak('Operation is unsupported with this solver: ', $solver);
    }
    $self->training_parameter->find_parameters(
        $data_set->as_problem(bias => $self->bias),
        $num_folds,
        $initial_cost,
        $initial_loss_sensitivity,
        $update,
    );
}

sub is_regression_solver { $_[0]->training_parameter->is_regression_solver }

sub loss_sensitivity { $_[0]->training_parameter->loss_sensitivity }

sub training_parameter { $_[0]->{training_parameter} }

sub train {
    args
        my $self => $InstanceOfPackage,
        my $data_set => InstanceOf['Algorithm::LibLinear::DataSet'];

    my $raw_model = Algorithm::LibLinear::Model::Raw->train(
        $data_set->as_problem(bias => $self->bias),
        $self->training_parameter,
    );
    Algorithm::LibLinear::Model->new(raw_model => $raw_model);
}

sub weights {
    args
        my $self => $InstanceOfPackage;

    my $labels = $self->training_parameter->weight_labels;
    my $weights = $self->training_parameter->weights;
    [ map {
        +{ label => $labels->[$_], weight => $weights->[$_], }
    } 0 .. $#$labels ];
}

1;
__END__

=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.47, released on July 9, 2023.

=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<train> 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<solver>.

=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)

lib/Algorithm/LibLinear.pm  view on Meta::CPAN


Executes training and returns a trained L<Algorithm::LibLinear::Model> instance.
C<data_set> is same as the C<cross_validation>'s.

=head1 AUTHOR

Koichi SATO E<lt>sekia@cpan.orgE<gt>

=head1 SEE ALSO

L<Algorithm::LibLinear::DataSet>

L<Algorithm::LibLinear::FeatureScaling>

L<Algorithm::LibLinear::Model>

L<LIBLINEAR Homepage|http://www.csie.ntu.edu.tw/~cjlin/liblinear/>

L<Algorithm::SVM> - A Perl binding to LIBSVM.

=head1 LICENSE

=head2 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, modi...

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 HOL...

=head2 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.

=cut



( run in 2.116 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )