| 1 | package AI::NaiveBayes; | ||||||||||||||||||||
| 2 | 3 | $AI::NaiveBayes::VERSION = '0.02'; | |||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use AI::NaiveBayes::Classification; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use AI::NaiveBayes::Learner; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use MooseX::Storage; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 3 | ▶ | use List::Util qw(max); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 3 | with Storage(format => 'Storable', io => 'File'); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 3 | has model => (is => 'ro', isa => 'HashRef[HashRef]', required => 1); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | sub train { | ||||||||||||||||||||
| 18 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
train: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 19 | 1 | my $learner = AI::NaiveBayes::Learner->new(); | |||||||||||||||||||
| 20 | 1 | for my $example ( @_ ){ | |||||||||||||||||||
| 21 | 2 | $learner->add_example( %$example ); | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | 1 | return $learner->classifier; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | sub classify { | ||||||||||||||||||||
| 28 | 4 | ▶ | my ($self, $newattrs) = @_; | ||||||||||||||||||
|
Subroutine
classify: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 29 | 4 | ▶ | $newattrs or die "Missing parameter for classify()"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 4 | my $m = $self->model; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # Note that we're using the log(prob) here. That's why we add instead of multiply. | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 4 | my %scores = %{$m->{prior_probs}}; | |||||||||||||||||||
| 36 | 4 | my %features; | |||||||||||||||||||
| 37 | 4 | ▶ | while (my ($feature, $value) = each %$newattrs) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 30 | ▶ | next unless exists $m->{attributes}{$feature}; # Ignore totally unseen features | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 9 | ▶ | while (my ($label, $attributes) = each %{$m->{probs}}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 18 | ▶ | my $score = ($attributes->{$feature} || $m->{smoother}{$label})*$value; # P($feature|$label)**$value | ||||||||||||||||||
|
Condition66%2/3
$attributes->{$feature} || $m->{'smoother'}{$label}
MC/DC0%0/2
$attributes->{$feature} || $m->{'smoother'}{$label}
$attributes->{$feature}
$m->{'smoother'}{$label}
Truth table66%2/3
A || B
A= $attributes->{$feature}B= $m->{'smoother'}{$label}
| |||||||||||||||||||||
| 41 | 18 | $scores{$label} += $score; | |||||||||||||||||||
| 42 | 18 | $features{$feature}{$label} = $score; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 4 | rescale(\%scores); | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 4 | return AI::NaiveBayes::Classification->new( label_sums => \%scores, features => \%features ); | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | sub rescale { | ||||||||||||||||||||
| 52 | 4 | ▶ | my ($scores) = @_; | ||||||||||||||||||
|
Subroutine
rescale: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # Scale everything back to a reasonable area in logspace (near zero), un-loggify, and normalize | ||||||||||||||||||||
| 55 | 4 | my $total = 0; | |||||||||||||||||||
| 56 | 4 | my $max = max(values %$scores); | |||||||||||||||||||
| 57 | 4 | foreach (values %$scores) { | |||||||||||||||||||
| 58 | 8 | $_ = exp($_ - $max); | |||||||||||||||||||
| 59 | 8 | $total += $_**2; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 4 | $total = sqrt($total); | |||||||||||||||||||
| 62 | 4 | foreach (values %$scores) { | |||||||||||||||||||
| 63 | 8 | $_ /= $total; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 3 | 1; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =pod | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =encoding UTF-8 | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =head1 NAME | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | AI::NaiveBayes - A Bayesian classifier | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =head1 VERSION | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | version 0.02 | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head1 SYNOPSIS | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # AI::NaiveBayes objects are created by AI::NaiveBayes::Learner | ||||||||||||||||||||
| 87 | # but for quick start you can use the 'train' class method | ||||||||||||||||||||
| 88 | # that is a shortcut using default AI::NaiveBayes::Learner settings | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | my $classifier = AI::NaiveBayes->train( | ||||||||||||||||||||
| 91 | { | ||||||||||||||||||||
| 92 | attributes => _hash(qw(sheep very valuable farming)), | ||||||||||||||||||||
| 93 | labels => ['farming'] | ||||||||||||||||||||
| 94 | }, | ||||||||||||||||||||
| 95 | { | ||||||||||||||||||||
| 96 | attributes => _hash(qw(vampires cannot see their images mirrors)), | ||||||||||||||||||||
| 97 | labels => ['vampire'] | ||||||||||||||||||||
| 98 | }, | ||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | # Classify a feature vector | ||||||||||||||||||||
| 102 | my $result = $classifier->classify({bar => 3, blurp => 2}); | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | # $result is now a AI::NaiveBayes::Classification object | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | my $best_category = $result->best_category; | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | This module implements the classic "Naive Bayes" machine learning | ||||||||||||||||||||
| 111 | algorithm. This is a low level class that accepts only pre-computed feature-vectors | ||||||||||||||||||||
| 112 | as input, see L<AI::Classifier::Text> for a text classifier that uses | ||||||||||||||||||||
| 113 | this class. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | Creation of C<AI::NaiveBayes> classifier object out of training | ||||||||||||||||||||
| 116 | data is done by L<AI::NaiveBayes::Learner>. For quick start | ||||||||||||||||||||
| 117 | you can use the limited C<train> class method that trains the | ||||||||||||||||||||
| 118 | classifier in a default way. | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | The classifier object is immutable. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | It is a well-studied probabilistic algorithm often used in | ||||||||||||||||||||
| 123 | automatic text categorization. Compared to other algorithms (kNN, | ||||||||||||||||||||
| 124 | SVM, Decision Trees), it's pretty fast and reasonably competitive in | ||||||||||||||||||||
| 125 | the quality of its results. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | A paper by Fabrizio Sebastiani provides a really good introduction to | ||||||||||||||||||||
| 128 | text categorization: | ||||||||||||||||||||
| 129 | L<http://faure.iei.pi.cnr.it/~fabrizio/Publications/ACMCS02.pdf> | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head1 METHODS | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =over 4 | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =item new( model => $model ) | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | Internal. See L<AI::NaiveBayes::Learner> to learn how to create a C<AI::NaiveBayes> | ||||||||||||||||||||
| 138 | classifier from training data. | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =item train( LIST of HASHREFS ) | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Shortcut for creating a trained classifier using L<AI::NaiveBayes::Learner> default | ||||||||||||||||||||
| 143 | settings. | ||||||||||||||||||||
| 144 | Arguments are passed to the C<add_example> method of the L<AI::NaiveBayes::Learner> | ||||||||||||||||||||
| 145 | object one by one. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =item classify( HASHREF ) | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Classifies a feature-vector of the form: | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | { feature1 => weight1, feature2 => weight2, ... } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | The result is a C<AI::NaiveBayes::Classification> object. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =item rescale | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | Internal | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =back | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =over 4 | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =item model | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | Internal | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =back | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =head1 THEORY | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Bayes' Theorem is a way of inverting a conditional probability. It | ||||||||||||||||||||
| 174 | states: | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | P(y|x) P(x) | ||||||||||||||||||||
| 177 | P(x|y) = ------------- | ||||||||||||||||||||
| 178 | P(y) | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | The notation C<P(x|y)> means "the probability of C<x> given C<y>." See also | ||||||||||||||||||||
| 181 | L<"http://mathforum.org/dr.math/problems/battisfore.03.22.99.html"> | ||||||||||||||||||||
| 182 | for a simple but complete example of Bayes' Theorem. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | In this case, we want to know the probability of a given category given a | ||||||||||||||||||||
| 185 | certain string of words in a document, so we have: | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | P(words | cat) P(cat) | ||||||||||||||||||||
| 188 | P(cat | words) = -------------------- | ||||||||||||||||||||
| 189 | P(words) | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | We have applied Bayes' Theorem because C<P(cat | words)> is a difficult | ||||||||||||||||||||
| 192 | quantity to compute directly, but C<P(words | cat)> and C<P(cat)> are accessible | ||||||||||||||||||||
| 193 | (see below). | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | The greater the expression above, the greater the probability that the given | ||||||||||||||||||||
| 196 | document belongs to the given category. So we want to find the maximum | ||||||||||||||||||||
| 197 | value. We write this as | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | P(words | cat) P(cat) | ||||||||||||||||||||
| 200 | Best category = ArgMax ----------------------- | ||||||||||||||||||||
| 201 | cat in cats P(words) | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | Since C<P(words)> doesn't change over the range of categories, we can get rid | ||||||||||||||||||||
| 204 | of it. That's good, because we didn't want to have to compute these values | ||||||||||||||||||||
| 205 | anyway. So our new formula is: | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | Best category = ArgMax P(words | cat) P(cat) | ||||||||||||||||||||
| 208 | cat in cats | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | Finally, we note that if C<w1, w2, ... wn> are the words in the document, | ||||||||||||||||||||
| 211 | then this expression is equivalent to: | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | Best category = ArgMax P(w1|cat)*P(w2|cat)*...*P(wn|cat)*P(cat) | ||||||||||||||||||||
| 214 | cat in cats | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | That's the formula I use in my document categorization code. The last | ||||||||||||||||||||
| 217 | step is the only non-rigorous one in the derivation, and this is the | ||||||||||||||||||||
| 218 | "naive" part of the Naive Bayes technique. It assumes that the | ||||||||||||||||||||
| 219 | probability of each word appearing in a document is unaffected by the | ||||||||||||||||||||
| 220 | presence or absence of each other word in the document. We assume | ||||||||||||||||||||
| 221 | this even though we know this isn't true: for example, the word | ||||||||||||||||||||
| 222 | "iodized" is far more likely to appear in a document that contains the | ||||||||||||||||||||
| 223 | word "salt" than it is to appear in a document that contains the word | ||||||||||||||||||||
| 224 | "subroutine". Luckily, as it turns out, making this assumption even | ||||||||||||||||||||
| 225 | when it isn't true may have little effect on our results, as the | ||||||||||||||||||||
| 226 | following paper by Pedro Domingos argues: | ||||||||||||||||||||
| 227 | L<"http://www.cs.washington.edu/homes/pedrod/mlj97.ps.gz"> | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | =head1 SEE ALSO | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | Algorithm::NaiveBayes (3), AI::Classifier::Text(3) | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =head1 BASED ON | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | Much of the code and description is from L<Algorithm::NaiveBayes>. | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =head1 AUTHORS | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =over 4 | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | =item * | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | Zbigniew Lukasiak <zlukasiak@opera.com> | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =item * | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | Tadeusz Sośnierz <tsosnierz@opera.com> | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | =item * | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | Ken Williams <ken@mathforum.org> | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | =back | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | This software is copyright (c) 2012 by Opera Software ASA. | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 260 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | =cut | ||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | __END__ | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | # ABSTRACT: A Bayesian classifier | ||||||||||||||||||||
| 268 | |||||||||||||||||||||