Algorithm-LibLinear

 view release on metacpan or  search on metacpan

README.pod  view on Meta::CPAN

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

README.pod  view on Meta::CPAN

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

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

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

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

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

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

        } @feature;
        push @data_set, +{ feature => \%feature, label => $label, };
    }
    return \@data_set;
}

sub size { 0 + @{ $_[0]->as_arrayref } }

1;

__DATA__

=head1 NAME

Algorithm::LibLinear::DataSet

=head1 SYNOPSIS

  use Algorithm::LibLinear::DataSet;
  
  my $data_set = Algorithm::LibLinear::DataSet->new(data_set => [
    +{ feature => +{ 1 => 0.708333, 2 => 1, 3 => 1, ... }, label => 1, },
    +{ feature => +{ 1 => 0.583333, 2 => -1, 3 => 0.333333, ... }, label => -1, },
    +{ feature => +{ 1 => 0.166667, 2 => 1, 3 => -0.333333, ... }, label => 1, },
    ...
  ]);
  my $data_set = Algorithm::LibLinear::DataSet->load(fh => \*DATA);
  my $data_set = Algorithm::LibLinear::DataSet->load(filename => 'liblinear_file');
  my $data_set = Algorithm::LibLinear::DataSet->load(string => "+1 1:0.70833 ...");
  
  say $data_set->size;
  say $data_set->as_string;  # '+1 1:0.70833 2:1 3:1 ...'
  
  __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 
  ...

=head1 DESCRIPTION

This class represents set of labeled feature vectors used for learning.

=head1 METHODS

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

    +{
        feature => $self->scale_feature($labeled_data->{feature}),
        label => $labeled_data->{label},
    };
}

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

1;

__DATA__

=head1 NAME

Algorithm::LibLinear::FeatureScaling

=head1 SYNOPSIS

  use Algorithm::LibLinear::DataSet;
  use Algorithm::LibLinear::FeatureScaling;
  

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

sub save {
    args
        my $self => $InstanceOfPackage,
        my $filename => Str;

    $self->raw_model->save($filename);
}

1;

__DATA__

=head1 NAME

Algorithm::LibLinear::Model

=head1 SYNOPSIS

  use Algorithm::LibLinear;
  
  my $data_set = Algorithm::LibLinear::DataSet->load(fh => \*DATA);
  my $classifier = Algorithm::LibLinear->new->train(data_set => $data_set);
  my $classifier = Algorithm::LibLinear::Model->load(filename => 'trained.model');
  
  my @labels = $classifier->class_labels;
  if ($classifier->is_oneclass_model) { ... }
  if ($classifier->is_probability_model) { ... }
  if ($classifier->is_regression_model) { ... }
  say $classifier->num_classes;  # == @labels
  say $classifier->num_features;  # == $data_set->size
  

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

      print "\t";
      print 'Bias: ', $classifier->bias($label);
      print "\n";
  }
  
  my $class_label = $classifier->predict(feature => +{ 1 => 1, 2 => 1, ... });
  my @probabilities = $classifier->predict_probability(feature => +{ 1 => 1, 2 => 1, ... });
  my @values = $classifier->predict_values(feature => +{ 1 => 1, 2 => 1, ... });
  $classifier->save(filenmae => 'trained.model');
  
  __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 
  ...

=head1 DESCRIPTION

This class represents a classifier or an estimated function generated as a return value of L<Algorithm::LibLinear>'s C<train> method. 

If you have model files generated by LIBLINEAR's C<train> command or this class's C<save> method, you can C<load> them.

t/Algorithm/LibLinear.t  view on Meta::CPAN

use strict;
use warnings;
use File::Temp;
use Test::Exception;
use Test::LeakTrace;
use Test::More;

BEGIN { use_ok 'Algorithm::LibLinear' }

my $input_data_set = do { local $/; <DATA> };

{
    my $learner = new_ok 'Algorithm::LibLinear' => [
        cost => 1,
        epsilon => 0.01,
        solver => 'L2R_L2LOSS_SVC',
        weights => [
            +{ label => 1, weight => 1, },
            +{ label => -1, weight => 1, },
        ],

t/Algorithm/LibLinear.t  view on Meta::CPAN

    );
    my $data_set =
        Algorithm::LibLinear::DataSet->load(string => $input_data_set);
    my $classifier = $learner->train(data_set => $data_set);
    my $labels = $classifier->class_labels;
};

done_testing;

# This input data is copied from heart_scale file in LIBLINEAR distribution.
__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 
-1 1:0.5 2:1 3:1 4:-0.509434 5:-0.767123 6:-1 7:-1 8:0.0534351 9:-1 10:-0.870968 11:-1 12:-1 13:1 
+1 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5 
+1 1:0.25 2:1 3:1 4:-0.698113 5:-0.484018 6:-1 7:1 8:0.0839695 9:1 10:-0.612903 12:-0.333333 13:1 
+1 1:0.291667 2:1 3:1 4:-0.132075 5:-0.237443 6:-1 7:1 8:0.51145 9:-1 10:-0.612903 12:0.333333 13:1 
+1 1:0.416667 2:-1 3:1 4:0.0566038 5:0.283105 6:-1 7:1 8:0.267176 9:-1 10:0.290323 12:1 13:1 

t/Algorithm/LibLinear/DataSet.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;

BEGIN { use_ok 'Algorithm::LibLinear::DataSet' }

my $source = do { local $/; <DATA> };
my $data_set = Algorithm::LibLinear::DataSet->load(string => $source);

is $data_set->size, 5;
is $data_set->as_string, $source;

done_testing;

__DATA__
1 2:0.1 3:0.2
1 2:0.1 3:0.3 4:-1.2
-1 1:0.4
-1 1:0.1 4:1.4 5:0.5
-1 1:-0.1 2:-0.2 3:0.1 4:1.1 5:0.1

t/Algorithm/LibLinear/FeatureScaling.t  view on Meta::CPAN

use strict;
use warnings;
use Algorithm::LibLinear::DataSet;
use Algorithm::LibLinear::FeatureScaling;
use Test::Exception;
use Test::More;

my $data_set = Algorithm::LibLinear::DataSet->load(fh => \*DATA);
my $scale = new_ok 'Algorithm::LibLinear::FeatureScaling' => [
    data_set => $data_set,
];
ok my $scaled_data_set = $scale->scale(data_set => $data_set);
is $scaled_data_set->size, $data_set->size;

{
    my $scaled_within_range = 1;
  VALUE_RANGE_CHECK:
    for my $scaled_data (@{ $scaled_data_set->as_arrayref }) {

t/Algorithm/LibLinear/FeatureScaling.t  view on Meta::CPAN

        [
            +{ feature => +{ 1 => 1, 2 => -1, 3 => 1 }, label => 1 },
            +{ feature => +{ 1 => -1, 2 => 1, 3 => -1 }, label => 1 },
            +{ feature => +{ 1 => -1, 3 => -1 }, label => 1 },
        ],
    );
}

done_testing;

__DATA__
+1  1:1.0  2:2.0  3:1.41
+1  1:2.0  2:2.0  3:1.73  4:-1.0  5:1.0
+1  1:3.0  2:2.0  3:2.00  4:-2.0
+1  1:4.0  2:2.0  3:2.23  4:-3.0  5:1.0

xt/01_podspell.t  view on Meta::CPAN

use Test::More;
eval q{ use Test::Spelling };
plan skip_all => "Test::Spelling is not installed." if $@;
add_stopwords(map { split /[\s\:\-]/ } <DATA>);
$ENV{LANG} = 'C';
all_pod_files_spelling_ok('lib');
__DATA__
Koichi SATO
L1
L2
LIBLINEAR
LIBSVM
MERCHANTABILITY
NONINFRINGEMENT
Redistributions
SVC
SVM



( run in 0.598 second using v1.01-cache-2.11-cpan-140bd7fdf52 )