Algorithm-SVM
view release on metacpan or search on metacpan
lib/Algorithm/SVM.pm view on Meta::CPAN
Loads a model from the specified filename. Returns a false value on failure,
and truth value on success.
$svm->train(@tset);
Trains the SVM on a set of Algorithm::SVM::DataSet objects. @tset should
be an array of Algorithm::SVM::DataSet objects.
$accuracy = $svm->validate(5);
Performs cross validation on the training set. If an argument is provided,
the set is partioned into n subsets, and validated against one another.
Returns a floating point number representing the accuracy of the validation.
$num = $svm->getNRClass();
For a classification model, this function gives the number of classes.
For a regression or a one-class model, 2 is returned.
(@labels) = $svm->getLabels();
For a classification model, this function returns the name of the labels
in an array. For regression and one-class models undef is returned.
$prob = $svm->getSVRProbability();
For a regression model with probability information, this function
outputs a value sigma > 0. For test data, we consider the probability
model: target value = predicted value + z, z: Laplace distribution
e^(-|z|/sigma)/2sigma)
If the model is not for svr or does not contain required information,
undef is returned.
=head1 MAINTAINER
Matthew Laird <matt@brinkman.mbb.sfu.ca>
Alexander K. Seewald <alex@seewald.at>
=head1 SEE ALSO
Algorithm::SVM::DataSet and the libsvm homepage:
http://www.csie.ntu.edu.tw/~cjlin/libsvm/
=head1 ACKNOWLEDGEMENTS
Thanks go out to Fiona Brinkman and the other members of the Simon Fraser
University Brinkman Laboratory for providing me the opportunity to develop
this module. Additional thanks go to Chih-Jen Lin, one of the libsvm authors,
for being particularly helpful during the development process.
As well to Dr. Alexander K. Seewald of Seewald Solutions for many bug fixes,
new test cases, and lowering the memory footprint by a factor of 20. Thank
you very much!
=cut
sub new {
my ($class, %args) = @_;
my $self = bless({ }, $class);
# Ensure we have a valid SVM type.
$args{Type} = 'C-SVC' if(! exists($args{Type}));
my $svmtype = $SVM_TYPES{$args{Type}};
croak("Invalid SVM type: $args{Type}") if(! defined($svmtype));
# Ensure we have a valid kernel type.
$args{Kernel} = 'radial' if(! exists($args{Kernel}));
my $kernel = $KERNEL_TYPES{$args{Kernel}};
croak("Invalid SVM kernel type: $args{Kernel}") if(! defined($svmtype));
# Set some defaults.
my $degree = exists($args{Degree}) ? $args{Degree} + 0 : 3;
my $gamma = exists($args{Gamma}) ? $args{Gamma} + 0 : 0;
my $coef0 = exists($args{Coef0}) ? $args{Coef0} + 0 : 0;
my $c = exists($args{C}) ? $args{C} + 0 : 1;
my $nu = exists($args{Nu}) ? $args{Nu} + 0 : 0.5;
my $epsilon = exists($args{Epsilon}) ? $args{Epsilon} + 0 : 0.1;
$self->{svm} = _new_svm($svmtype, $kernel, $degree, $gamma, $coef0,
$c, $nu, $epsilon);
# Load the model if one was specified.
if(my $model = $args{Model}) {
croak("Model file not found or bad permissions: $model")
if((! -r $model) || (! -f $model));
# Load the model.
$self->load($model);
# Ensure that the model loaded correctly.
croak("Error loading model file: $model") if(! $self->{svm});
}
return $self;
}
sub predict {
my ($self, $x) = @_;
# Check if we got a dataset object.
croak("Not an Algorithm::DataSet") if(ref($x) ne "Algorithm::SVM::DataSet");
return _predict($self->{svm}, $x);
}
sub predict_value {
my ($self, $x) = @_;
# Check if we got a dataset object.
croak("Not an Algorithm::DataSet") if(ref($x) ne "Algorithm::SVM::DataSet");
return _predict_value($self->{svm}, $x);
}
sub save {
my ($self, $file) = @_;
croak("Can't save model because no filename provided") if(! $file);
( run in 0.954 second using v1.01-cache-2.11-cpan-39bf76dae61 )