Algorithm-SVMLight
view release on metacpan or search on metacpan
lib/Algorithm/SVMLight.pm view on Meta::CPAN
}
sub _escape {
local $_ = shift;
s/([\\'])/\\$1/g;
s/\n/\\n/g;
s/\r/\\r/g;
return "'$_'";
}
1;
__END__
=head1 NAME
Algorithm::SVMLight - Perl interface to SVMLight Machine-Learning Package
=head1 SYNOPSIS
use Algorithm::SVMLight;
my $s = new Algorithm::SVMLight;
$s->add_instance
(attributes => {foo => 1, bar => 1, baz => 3},
label => 1);
$s->add_instance
(attributes => {foo => 2, blurp => 1},
label => -1);
... repeat for several more instances, then:
$s->train;
# Find results for unseen instances
my $result = $s->predict
(attributes => {bar => 3, blurp => 2});
=head1 DESCRIPTION
This module implements a perl interface to Thorsten Joachims' SVMLight
package:
=over 4
SVMLight is an implementation of Vapnik's Support Vector Machine
[Vapnik, 1995] for the problem of pattern recognition, for the problem
of regression, and for the problem of learning a ranking function. The
optimization algorithms used in SVMlight are described in [Joachims,
2002a ]. [Joachims, 1999a]. The algorithm has scalable memory
requirements and can handle problems with many thousands of support
vectors efficiently.
-- http://svmlight.joachims.org/
=back
Support Vector Machines in general, and SVMLight specifically,
represent some of the best-performing Machine Learning approaches in
domains such as text categorization, image recognition, bioinformatics
string processing, and others.
For efficiency reasons, the underlying SVMLight engine indexes features by integers, not
strings. Since features are commonly thought of by name (e.g. the
words in a document, or mnemonic representations of engineered
features), we provide in C<Algorithm::SVMLight> a simple mechanism for
mapping back and forth between feature names (strings) and feature
indices (integers). If you want to use this mechanism, use the
C<add_instance()> and C<predict()> methods. If not, use the
C<add_instance_i()> (or C<read_instances()>) and C<predict_i()>
methods.
=head1 INSTALLATION
For installation instructions, please see the F<README> file included
with this distribution.
=head1 METHODS
=over 4
=item new(...)
Creates a new C<Algorithm::SVMLight> object and returns it. Any named
arguments that correspond to SVM parameters will cause their
corresponding C<set_I<***>()> method to be invoked:
$s = Algorithm::SVMLight->new(
type => 2, # Regression model
biased_hyperplane => 0, # Nonbiased
kernel_type => 3, # Sigmoid
);
See the C<set_I<***>(...)> method for a list of such parameters.
=item set_I<***>(...)
The following parameters can be set by using methods with their
corresponding names - for instance, the C<maxiter> parameter can be
set by using C<set_maxiter($x)>, where C<$x> is the new desired value.
Learning parameters:
type
svm_c
eps
svm_costratio
transduction_posratio
biased_hyperplane
sharedslack
svm_maxqpsize
svm_newvarsinqp
kernel_cache_size
epsilon_crit
epsilon_shrink
svm_iter_to_shrink
maxiter
remove_inconsistent
skip_final_opt_check
compute_loo
rho
xa_depth
predfile
alphafile
Kernel parameters:
kernel_type
poly_degree
rbf_gamma
coef_lin
coef_const
custom
For an explanation of these parameters, you may be interested in
looking at the F<svm_common.h> file in the SVMLight distribution.
It would be a good idea if you only set these parameters via arguments
to C<new()> (see above) or right after calling C<new()>, since I don't
think the underlying C code expects them to change in the middle of a
process.
=item add_instance(label => $x, attributes => \%y)
Adds a training instance to the set of instances which will be used to
train the model. An C<attributes> parameter specifies a hash of
attribute-value pairs for the instance, and a C<label> parameter
specifies the label. The label must be a number, and typically it
should be C<1> for positive training instances and C<-1> for negative
training instances. The keys of the C<attributes> hash should be
strings, and the values should be numbers (the values of each attribute).
All training instances share the same attribute-space; if an attribute
is unspecified for a certain instance, it is equivalent to specifying
a value of zero. Typically you can save a lot of memory (and
potentially training time) by omitting zero-valued attributes.
Each training instance may have a "cost factor" assigned to it,
indicating the relative cost of misclassification of the instance.
The default is a cost of 1.0; to assign a different cost, pass a
C<cost_factor> parameter with the desired value.
When using a ranking SVM, you may also pass a C<query_id> parameter,
whose integer value will identify the group of instances in which this
instance belongs for ranking purposes.
Finally, a C<slack_id> parameter may also be passed and it will become
the C<slackid> member of the underlying C<DOC> C struct, used in an
"OPTIMIZATION" SVM (C<type==4>).
=item add_instance_i($label, $name, \@indices, \@values, $query_id=0, $slack_id=0, $cost_factor=1.0)
This is just like C<add_instance()>, but bypasses all the
string-to-integer mapping of feature names. Use this method when you
already have your features represented as integers. The C<$label>
parameter must be a number (typically C<1> or C<-1>), and the
C<@indices> and C<@values> arrays must be parallel arrays of indices
and their corresponding values. Furthermore, the indices must be
positive integers and given in strictly increasing order.
If you like C<add_instance_i()>, I've got a C<predict_i()> I bet
you'll just love.
=item read_instances($file)
An alternative to calling C<add_instance_i()> for each instance is to
organize a collection of training data into SVMLight's standard
"example_file" format, then call this C<read_instances()> method to
import the data. Under the hood, this calls SVMLight's
C<read_documents()> C function. When it's convenient for you to
organize the data in this manner, you may see speed improvements.
=item ranking_callback(\&function)
When using a ranking SVM, it is possible to customize the cost of
ranking each pair of instances incorrectly by supplying a custom Perl
callback function.
For two instances C<i> and C<j>, the custom function will receive four
arguments: the C<rankvalue> of instance C<i> and C<j>, and the
C<costfactor> of instance C<i> and C<j>. It should return a real
( run in 1.603 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )