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;
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
for my $label (1 .. $classifier->num_classes) {
print 'Coeffs: ';
print join(' ', map {
$classifier->coefficient($_, $label);
} 1 .. $classifier->num_features);
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
...
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.
bias([$index])
Returns value of the bias term corresponding to the $index
-th class. In case of one-class SVM (i.e., when is_oneclass_model
is true,) the $index
is ignored.
Recall that a trained model can be represented as a function f(x) = W^t x + b, where W is a F x C matrix, b is a C-sized vector and C and F are the numbers of classes and features, respectively. This method returns b($index
) in this notation.
Note that <$index> is 1-based, unlike LIBLINEAR's get_decfun_bias()
function.
class_labels
Returns an ArrayRef of class labels, each of them could be returned by predict
and predict_values
.
coefficient($feature_index, $label_index)
Returns value of the coefficient of classifier matrix. i.e., W($feature_index
, $label_index
) (see bias
method description above.)
Be careful that both indices are 1-based just same as bias
.
is_oneclass_model
Returns true if the model is trained for one-class SVM, false otherwise.
is_probability_model
Returns true if the model is trained for logistic regression, false otherwise.
is_regression_model
Returns true if the model is trained for support vector regression (SVR), false 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 will raise 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.