The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Algorithm::LibLinear::Model

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;
$classifier->is_probability_model;
say $classifier->num_classes;  # == @labels
say $classifier->num_features;  # == $data_set->size
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 
...

DESCRIPTION

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

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

METHOD

Note that the constructor of this class is not a part of public API. You can get a instance via <Algorithm::LibLinaear-train>>. i.e., Algorithm::LibLinear is a factory class.

load(filename => $path)

Class method. Load a LIBLINEAR's model file and returns an instance of this class.

class_labels

Returns an ArrayRef of class labels, each of them could be returned by predict and predict_values.

is_probability_model

Returns true if the model is trained as a classifier based on logistic regression, else otherwise.

num_classes

The number of class labels.

num_features

The number of features contained in training set.

predict(feature => $hashref)

In case of classification, returns predicted class label.

In case of regression, returns value of estimated function given feature.

predict_probabilities(feature => $hashref)

Returns an ArrayRef of probabilities of the feature belonging to corresponding class.

This method raises an error if the model is not a classifier based on logistic regression (i.e., <not $classifier-is_probability_model>>.)

predict_values(feature => $hashref)

Returns an ArrayRef of decision values of each class (higher is better).

save(filename => $path)

Writes the model out as a LIBLINEAR model file.