Algorithm-AdaBoost
view release on metacpan or search on metacpan
lib/Algorithm/AdaBoost.pm view on Meta::CPAN
while ($num_iterations--) {
# Construct a weak classifier which classifies data on the distribution.
$weak_classifier = $weak_classifier_generator->(
distribution => $distribution,
training_set => $training_set,
);
$weight = $self->calculate_classifier_weight(
classifier => $weak_classifier,
distribution => $distribution,
);
push @weak_classifiers, +{
classifier => $weak_classifier,
weight => $weight,
};
} continue {
$distribution = $self->construct_hardest_distribution(
classifier => $weak_classifier,
previous_distribution => $distribution,
training_set => $training_set,
weight => $weight,
);
}
return $self->{final_classifier} = Algorithm::AdaBoost::Classifier->new(
weak_classifiers => \@weak_classifiers,
);
}
sub trained { exists shift->{final_classifier} }
sub training_set { shift->{training_set} }
sub weak_classifier_generator { shift->{weak_classifier_generator} }
1;
__END__
=head1 NAME
Algorithm::AdaBoost - AdaBoost learning algorithm
=head1 SYNOPSIS
use Algorithm::AdaBoost;
# Training phase.
my $learner = Alogrithm::AdaBoost->new(
training_set => [
+{ feature => [...], label => 1, },
+{ feature => [...], label => -1, },
+{ feature => [...], label => -1, },
...
],
weak_classifier_generator => \&my_poor_learning_algorithm,
);
$learner->train(num_iterations => 1_000);
# Now you have a boost-ed classifier (Algorithm::AdaBoost::Classifier).
my $classifier = $learner->final_classifier;
given ($classifier->classify([...])) {
when ($_ > 0) { say 'The data belongs to class 1.' }
when ($_ < 0) { say 'The data belongs to class 2.' }
default { warn 'The data cannot be classified.' }
}
=head1 DESCRIPTION
AdaBoost is a machine learning algorithm proposed by Freund and Schapire.
Using an arbitrary binary classification algorithm, The algorithm can construct a more accurate classifier (i.e. it is a meta-algorithm).
=head1 METHODS
=head2 new
Constructor. You can specify 2 optional attributes:
=over 2
=item training_set
An ArrayRef which is used as a training data set.
Each item is a HashRef having 2 keys: C<feature> and C<label>. C<feature> is a arbitrary input that classifier accepts and C<label> is a expected output label (C<+1> or C<-1>).
=item weak_classifier_generator
A CodeRef which is expected to generate a binary classifier function.
When the function is called, 2 named parameters are specified like this:
my $classifier = $generator->(
distribution => [...],
training_set => [...],
);
C<distribution> is an ArrayRef which each item is a probability of corresponding item in C<training_set>. i.e. C<distribution> is P(X = t_i) where t_i is i-th item in C<training_set>.
The generated classifier is expected to be a CodeRef which takes 1 argument (value of C<feature>) and return C<+1> or C<-1> as a output label.
=back
Either of both can be overriden temporarily with parameters for C<train>.
=head2 classify
Shorthand for C<< $learner->final_classifier->classify >>.
=head2 final_classifier
Returns the last constructed classifier.
=head2 train
Constructs a stronger classifier from given training set and weak learning algorithm.
This method takes 1 mandatory parameter:
=over 2
=item num_iterations
Specifies how many training iterations to be excuted (i.e., how many weak classifiers to be generated).
( run in 0.305 second using v1.01-cache-2.11-cpan-483215c6ad5 )