AI-NaiveBayes
view release on metacpan or search on metacpan
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
Appendix: How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to humanity, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.
To do so, attach the following notices to the program. It is safest to
attach them to the start of each source file to most effectively convey
the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 19yy <name of author>
},
labels => ['vampire']
},
);
# Classify a feature vector
my $result = $classifier->classify({bar => 3, blurp => 2});
# $result is now a AI::NaiveBayes::Classification object
my $best_category = $result->best_category;
=head1 DESCRIPTION
This module implements the classic "Naive Bayes" machine learning
algorithm. This is a low level class that accepts only pre-computed feature-vectors
as input, see L<AI::Classifier::Text> for a text classifier that uses
this class.
Creation of C<AI::NaiveBayes> classifier object out of training
data is done by L<AI::NaiveBayes::Learner>. For quick start
lib/AI/NaiveBayes.pm view on Meta::CPAN
},
labels => ['vampire']
},
);
# Classify a feature vector
my $result = $classifier->classify({bar => 3, blurp => 2});
# $result is now a AI::NaiveBayes::Classification object
my $best_category = $result->best_category;
=head1 DESCRIPTION
This module implements the classic "Naive Bayes" machine learning
algorithm. This is a low level class that accepts only pre-computed feature-vectors
as input, see L<AI::Classifier::Text> for a text classifier that uses
this class.
Creation of C<AI::NaiveBayes> classifier object out of training
data is done by L<AI::NaiveBayes::Learner>. For quick start
lib/AI/NaiveBayes/Classification.pm view on Meta::CPAN
package AI::NaiveBayes::Classification;
$AI::NaiveBayes::Classification::VERSION = '0.04';
use strict;
use warnings;
use 5.010;
use Moose;
has features => (is => 'ro', isa => 'HashRef[HashRef]', required => 1);
has label_sums => (is => 'ro', isa => 'HashRef', required => 1);
has best_category => (is => 'ro', isa => 'Str', lazy_build => 1);
sub _build_best_category {
my $self = shift;
my $sc = $self->label_sums;
my ($best_cat, $best_score) = each %$sc;
while (my ($key, $val) = each %$sc) {
($best_cat, $best_score) = ($key, $val) if $val > $best_score;
}
return $best_cat;
}
sub find_predictors{
my $self = shift;
my $best_cat = $self->best_category;
my $features = $self->features;
my @predictors;
for my $feature ( keys %$features ) {
for my $cat ( keys %{ $features->{$feature } } ){
next if $cat eq $best_cat;
push @predictors, [ $feature, $features->{$feature}{$best_cat} - $features->{$feature}{$cat} ];
}
}
@predictors = sort { abs( $b->[1] ) <=> abs( $a->[1] ) } @predictors;
return $best_cat, @predictors;
}
__PACKAGE__->meta->make_immutable;
1;
=pod
=encoding UTF-8
lib/AI/NaiveBayes/Classification.pm view on Meta::CPAN
AI::NaiveBayes::Classification - The result of a bayesian classification
=head1 VERSION
version 0.04
=head1 SYNOPSIS
my $result = $classifier->classify({bar => 3, blurp => 2});
# $result is an AI::NaiveBayes::Classification object
say $result->best_category;
my $predictors = $result->find_predictors;
=head1 DESCRIPTION
AI::NaiveBayes::Classification represents the result of a bayesian classification,
produced by AI::NaiveBayes classifier.
=head1 METHODS
=over 4
=item C<best_category()>
Returns a string being a label that suits given document the best.
=item C<find_predictors()>
This method returns the C<best_category()>, as well as the list of all the predictors
along with their influence on the best category selected. So the second value
returned is a list of array references, where each one contains a string being a
single feature and a number describing its influence on the result. So the
second part of the result may look like this:
(
[ 'activities', 1.2511540632952 ],
[ 'over', -1.0269523272981 ],
[ 'provide', 0.8280157033269 ],
[ 'natural', 0.7361042359385 ],
[ 'against', -0.6923354975173 ],
t/02-predict.t view on Meta::CPAN
$s = $classifier->classify( _hash(qw(i see that many vampires may have eaten my beautiful daughter's blood)) );
$h = $s->label_sums;
ok $h;
ok $h->{farming} < 0.5;
ok $h->{vampire} > 0.5;
# Find predictors
my $p = $classifier->classify( _hash( qw(i would like to begin farming sheep)) );
my( $best_cat, @predictors ) = $p->find_predictors();
is( $best_cat, 'farming', 'Best category' );
is( scalar @predictors, 2, 'farming and sheep - two predictors' );
is( $predictors[0][0], 'farming', 'Farming is the best predictor' );
# Prior probs
$lr = AI::NaiveBayes::Learner->new();
# Populate
$lr->add_example( attributes => _hash(qw(sheep very valuable farming)),
labels => ['farming'] );
$lr->add_example( attributes => _hash(qw(farming requires many kinds animals)),
labels => ['farming'] );
$lr->add_example( attributes => _hash(qw(good soil)),
( run in 0.635 second using v1.01-cache-2.11-cpan-4e96b696675 )