Algorithm-AdaBoost
view release on metacpan or search on metacpan
lib/Algorithm/AdaBoost.pm view on Meta::CPAN
sub final_classifier {
args my $self;
Carp::croak 'The classifier is not trained' unless $self->trained;
return $self->{final_classifier};
}
sub train {
args
my $self,
my $num_iterations => 'Int',
my $training_set => +{ isa => 'ArrayRef', optional => 1 },
my $weak_classifier_generator => +{ isa => 'CodeRef', optional => 1 };
$training_set //= $self->training_set
// Carp::croak('Given no training set.');
$weak_classifier_generator //= $self->weak_classifier_generator
// Carp::croak('Given no weak classifier generator.');
my $num_training_set = @$training_set;
# Initial distribution is uniform.
my $distribution = [ (1 / $num_training_set) x $num_training_set ];
my ($weak_classifier, $weight);
my @weak_classifiers;
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, +{
lib/Algorithm/AdaBoost.pm view on Meta::CPAN
# 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
lib/Algorithm/AdaBoost.pm view on Meta::CPAN
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).
=back
and 2 optional parameters:
=over 2
=item training_set
=item weak_classifier_generator
t/01_basic.t view on Meta::CPAN
my $learner = new_ok 'Algorithm::AdaBoost' => [
training_set => \@training_set,
weak_classifier_generator => \&generate_weak_classifier,
];
ok(
+(not $learner->trained),
'|trained| should be false before the learner |train|-ed.'
);
$learner->train(num_iterations => 1000);
ok($learner->trained, 'A classifier is constructed successfully.');
my $classifier = $learner->final_classifier;
my $correct = 0;
for my $test_data (@test_set) {
my $answer = $classifier->classify($test_data->{feature}) < 0 ? -1 : 1;
++$correct if $answer == $test_data->{label};
}
my $accuracy = $correct / @test_set;
( run in 0.742 second using v1.01-cache-2.11-cpan-71847e10f99 )