1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
package Algorithm::NaiveBayes::Model::Discrete;
use strict;
use base qw(Algorithm::NaiveBayes);
use Algorithm::NaiveBayes::Util qw(rescale);
sub do_add_instance {
my ($self, $attributes, $labels, $data) = @_;
foreach my $label ( @$labels ) {
my $mylabel = $data->{labels}{$label} ||= {};
$mylabel->{count}++;
while (my ($attr, $value) = each %$attributes) {
$mylabel->{attrs}{$attr}{$value}++;
}
}
}
sub do_train {
my ($self, $training_data) = @_;
my $m = {};
my $instances = $self->instances;
my $labels = $training_data->{labels};
my $probs = $m->{probs} = {};
# Calculate the log-probabilities for each category
foreach my $label ($self->labels) {
$m->{prior_probs}{$label} = log($labels->{$label}{count} / $instances);
my $denominator = log($labels->{$label}{count});
while (my ($attribute, $values) = each %{ $labels->{$label}{attrs} }) {
while (my ($value, $count) = each %$values) {
$probs->{$attribute}{$label}{$value} = log($count) - $denominator;
}
}
}
return $m;
}
sub do_predict {
my ($self, $m, $newattrs) = @_;
# Note that we're using the log(prob) here. That's why we add instead of multiply.
my %scores = %{$m->{prior_probs}};
while (my ($feature, $value) = each %$newattrs) {
next unless exists $m->{probs}{$feature}; # Ignore totally unseen features
while (my ($label, $values) = each %{$m->{probs}{$feature}}) {
$scores{$label} += ($values->{$value} || 0);
}
}
rescale \%scores;
return \%scores;
}
1;
|