NAME

AI::NaiveBayes1 - Bayesian prediction of categories

SYNOPSIS

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";
}

DESCRIPTION

This module implements the classic "Naive Bayes" machine learning algorithm.

METHODS

new()

Creates a new AI::NaiveBayes1 object and returns it. At the moment there are no parameters that affect anything.

add_instance( attributes => HASH, label => STRING|ARRAY )

Adds a training instance to the categorizer.

train()

Calculates the probabilities that will be necessary for categorization using the predict() method.

predict( attributes => HASH )

Use this method to predict the label of an unknown instance. The attributes should be of the same format as you passed to add_instance(). predict() returns a hash reference whose keys are the names of labels, and whose values are corresponding probabilities.

labels

Returns a list of all the labels the object knows about (in no particular order), or the number of labels if called in a scalar context.

THEORY

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...

HISTORY

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.

AUTHOR

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/.

SEE ALSO

Algorithms::NaiveBayes, perl.