Algorithm::LibLinear::Model
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_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 ...
This class represents a classifier or an estimated function generated as a return value of Algorithm::LibLinear's train method.
train
If you have model files generated by LIBLINEAR's train command or this class's save method, you can load them.
save
load
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.
Algorithm::LibLinaear->train
Algorithm::LibLinear
Class method. Load a LIBLINEAR's model file and returns an instance of this class.
Returns value of the bias term corresponding to the $index-th class.
$index
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.
get_decfun_bias()
Returns an ArrayRef of class labels, each of them could be returned by predict and predict_values.
predict
predict_values
Returns value of the coefficient of classifier matrix. i.e., W($feature_index, $label_index) (see bias method description above.)
$feature_index
$label_index
bias
Be careful that both indices are 1-based just same as bias.
Returns true if the model is trained for logistic regression, false otherwise.
Returns true if the model is trained for support vector regression (SVR), false otherwise.
The number of class labels.
The number of features contained in training set.
In case of classification, returns predicted class label.
In case of regression, returns value of estimated function given feature.
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.)
not $classifier->is_probability_model
Returns an ArrayRef of decision values of each class (higher is better).
Writes the model out as a LIBLINEAR model file.
To install Algorithm::LibLinear, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Algorithm::LibLinear
CPAN shell
perl -MCPAN -e shell install Algorithm::LibLinear
For more information on module installation, please visit the detailed CPAN module installation guide.