NAME

AI::NaiveBayes1 - Bayesian prediction of categories

SYNOPSIS

use AI::NaiveBayes1;
my $nb = AI::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";
}

# export the model into a string
my $string = $nb->export_to_YAML();

# create the same model from the string
my $nb1 = AI::NaiveBayes1->import_from_YAML($string);

# write the model to a file (shorter than model->string->file)
$nb->export_to_YAML_file('t/tmp1');

# read the model from a file (shorter than file->string->model)
my $nb2 = AI::NaiveBayes1->import_from_YAML_file('t/tmp1');

DESCRIPTION

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

METHODS

Constructor Methods

new()

Creates a new AI::NaiveBayes1 object and returns it.

import_from_YAML( $string )

Creates a new AI::NaiveBayes1 object from a string where it is represented in YAML. Requires YAML module.

import_from_YAML_file( $file_name )

Creates a new AI::NaiveBayes1 object from a file where it is represented in YAML. Requires YAML module.

Methods

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

Adds a training instance to the categorizer.

add_instances( attributes => HASH, label => STRING|ARRAY, cases => NUMBER )

Adds a number of identical instances to the categorizer.

export_to_YAML()

Returns a YAML string representation of an AI::NaiveBayes1 object. Requires YAML module.

export_to_YAML_file( $file_name )

Writes a YAML string representation of an AI::NaiveBayes1 object to a file. Requires YAML module.

Returns a string, human-friendly representation of the model. The model is supposed to be trained before calling this method.

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... (You can read more about it in many books.)

HISTORY

Algorithms::NaiveBayes by Ken Williams was not what I needed so I wrote this one. Algorithms::NaiveBayes is oriented towards text categorization, it includes smoothing, and log probabilities. This module is a generic, basic Naive Bayes algorithm.

THANKS

I'd like to thank Tom Dyson and Dan Von Kohorn for bug reports, support, and comments; and to CPAN-testers (jlatour, Jost.Krieger) for their bug reports.

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.