|
AI::NaiveBayes1 - Bayesian prediction of categories |
AI::NaiveBayes1 - Bayesian prediction of categories
use Algorithm::NaiveBayes1; my $nb = Algorithm::NaiveBayes1->new;
$nb->add_instances(attributes=>{model=>'H',place=>'B'},label=>'repairs=Y',cases=>30);
$nb->add_instances(attributes=>{model=>'H',place=>'B'},label=>'repairs=N',cases=>10);
$nb->add_instances(attributes=>{model=>'H',place=>'N'},label=>'repairs=Y',cases=>18);
$nb->add_instances(attributes=>{model=>'H',place=>'N'},label=>'repairs=N',cases=>16);
$nb->add_instances(attributes=>{model=>'T',place=>'B'},label=>'repairs=Y',cases=>22);
$nb->add_instances(attributes=>{model=>'T',place=>'B'},label=>'repairs=N',cases=>14);
$nb->add_instances(attributes=>{model=>'T',place=>'N'},label=>'repairs=Y',cases=> 6);
$nb->add_instances(attributes=>{model=>'T',place=>'N'},label=>'repairs=N',cases=>84);
$nb->train;
print "Model:\n" . $nb->print_model;
# Find results for unseen instances
my $result = $nb->predict
(attributes => {model=>'T', place=>'N'});
foreach my $k (keys(%{ $result })) {
print "for label $k P = " . $result->{$k} . "\n";
}
This module implements the classic ``Naive Bayes'' machine learning algorithm.
new()AI::NaiveBayes1 object and returns it. At the
moment there are no parameters that affect anything.
train()predict() method.
add_instance(). predict() returns a hash reference whose keys
are the names of labels, and whose values are corresponding
probabilities.
Bayes' Theorem is a way of inverting a conditional probability. It states:
P(y|x) P(x)
P(x|y) = -------------
P(y)
and so on...
Algorithms::NaiveBayes by Ken Williams was not what I needed so I wrote this one. Algorithms::NaiveBayse is oriented towards text categorization, it includes smooting, and log probabilities. This module is a generic, basic Naive Bayes algorithm.
Copyright 2003 Vlado Keselj www.cs.dal.ca/~vlado
This script is provided ``as is'' without expressed or implied warranty. This is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The latest version can be found at http://www.cs.dal.ca/~vlado/srcperl/.
Algorithms::NaiveBayes, the perl manpage.
|
AI::NaiveBayes1 - Bayesian prediction of categories |