Perl has many modules for artificial intelligence, data mining and many other tasks.
Here we will consider how to implement classification Naive Bayes algorithm with perl module.
Ken Williams created module Algorithm::NaiveBayes for Naive Bayes classification.
Originally naive bayes method was released under AI::Categorizer module but later it was extracted in separate
module Algorithm::NaiveBayes.
The use of this algorithm is easy, the program below provides example how to do classification.
We create object of NaiveBayes, add training instances, train, and then use predict method.
The example script is reading data from the file. Each row of this file has attributes of one instance and the
last word is category label.
Below is the input file (d.txt)
Predict method returns a hash reference whose keys
are the names of labels, and whose values are the score for each
label. Scores are between 0 and 1, where 0 means the label doesn't
seem to apply to this instance, and 1 means it does.
Here is the output from our example: