AI::Ca...eBayes(3pm)

๐Ÿ“ฆ
version 0.09 ยท 2 versions
, in fedora 43

โœจ AI::Ca...eBayes(3pm) โœจ

okay bestie, come sit with me ๐Ÿ’– this is the manual page for AI::Ca...eBayes(3pm), straight out of the User Contributed Perl Documentation, and we're gonna get you fluent in it. no cap.


๐ŸŒธ NAME ๐ŸŒธ

AI::Categorizer::Learner::NaiveBayes โ€” the Naive Bayes Algorithm For AI::Categorizer. that's the whole vibe. it does the math so you don't have to ๐Ÿง โœจ


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

like, here's the shape of it, literally copy-paste coded:

  use AI::Categorizer::Learner::NaiveBayes;

  # Here $k is an AI::Categorizer::KnowledgeSet object

  my $nb = new AI::Categorizer::Learner::NaiveBayes(...parameters...);
  $nb->train(knowledge_set => $k);
  $nb->save_state('filename');

  ... time passes ...

  $nb = AI::Categorizer::Learner::NaiveBayes->restore_state('filename');
  my $c = new AI::Categorizer::Collection::Files( path => ... );
  while (my $document = $c->next) {
    my $hypothesis = $nb->categorize($document);
    print "Best assigned category: ", $hypothesis->best_category, "\n";
    print "All assigned categories: ", join(', ', $hypothesis->categories), "\n";
  }

see how it trains, saves, time passes (the most relatable line in any manpage honestly), then restores and starts categorizing? that's the whole lifecycle, periodt ๐Ÿ’…


๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

so this is an implementation of the Naive Bayes decision-making algorithm, pointed at the task of document categorization โ€” as defined by the AI::Categorizer module, okay? if you want the complete description of the interface, go read AI::Categorizer, it's the main character here and this module is like its very competent bestie ๐Ÿซถ

also important: this module is now a wrapper around the stand-alone Algorithm::NaiveBayes module. the discussion of Bayes' Theorem got moved into that module's documentation. so if you're like "wait where's the theorem" โ€” it's over there, go look ๐Ÿงš


๐Ÿ‘‘ METHODS ๐Ÿ‘‘

this class inherits from the AI::Categorizer::Learner class, so literally all of its methods are available to you unless we explicitly say otherwise down here.

โœจ new() โœจ

Creates a new Naive Bayes Learner and hands it back to you. on top of the parameters the AI::Categorizer::Learner class already accepts, the Naive Bayes subclass is like "and also these":

  • threshold โ€” sets the score threshold for category membership. the default is currently 0.3. set the threshold lower to assign more categories per document, set it higher to assign fewer. this is a genuinely effective way to trade off between precision and recall, iykyk ๐ŸŽ€

โœจ threshold() โœจ

Returns the current threshold value. hand it an optional numeric argument and you may set the threshold. that's it, that's the method ๐Ÿ’…

โœจ train(knowledge_set => $k) โœจ

Trains the categorizer. this is the glow-up phase โ€” it prepares the thing for later use in categorizing documents. the knowledge_set parameter must provide an object of the class AI::Categorizer::KnowledgeSet (or a subclass of it), and it needs to be populated with lots of documents and categories. check AI::Categorizer::KnowledgeSet for the details on how to build one of those ๐ŸŒˆ

โœจ categorize($document) โœจ

Returns an AI::Categorizer::Hypothesis object representing the categorizer's "best guess" about which categories the given document should be assigned to. yes, it's a guess, it's doing its best ๐Ÿ˜ญ โ€” see AI::Categorizer::Hypothesis for more details on how to actually use that object.

โœจ save_state($path) โœจ

Saves the categorizer for later use. this method is inherited from AI::Categorizer::Storable. free method, no notes ๐Ÿ’–


๐Ÿงฎ CALCULATIONS ๐Ÿงฎ

okay so the various probabilities used in the above calculations are found directly from the training documents. example time, pay attention bestie:

if there are 5000 total tokens (words) in the "sports" training documents and 200 of them are the word "curling", then P(curling|sports) = 200/5000 = 0.04 .

and if there are 10,000 total tokens in the training corpus and 5,000 of them are in documents belonging to the category "sports", then P(sports) = 5,000/10,000 = 0.5> .

not me leaving that stray > in there โ€” it's in the original, and we keep every detail, so it stays ๐Ÿ’€

because the probabilities involved are often very small and we multiply a whole bunch of them together, the result is often a tiny tiny number. that could pose problems of floating-point underflow (yikes), so instead of working with the actual probabilities we work with the logarithms of the probabilities. bonus: this also speeds up various calculations in the categorize() method. two birds, one elegant log ๐ŸŒŸ


๐Ÿ”ฅ TO DO ๐Ÿ”ฅ

More work on the confidence scores โ€” right now the winning category tends to dominate the scores overwhelmingly, when the scores should probably be more evenly distributed.

(we're aware. it's on the list. the winning category is giving main character energy a little too hard and we're working on it ๐Ÿ™ˆ)


๐Ÿซถ AUTHOR ๐Ÿซถ

Ken Williams, ken@forum.swarthmore.edu


Copyright 2000-2003 Ken Williams. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

  • AI::Categorizer(3), Algorithm::NaiveBayes(3)
  • "A re-examination of text categorization methods" by Yiming Yang <http://www.cs.cmu.edu/~yiming/publications.html>
  • "On the Optimality of the Simple Bayesian Classifier under Zero-One Loss" by Pedro Domingos "/www.cs.washington.edu/homes/pedrod/mlj97.ps.gz"" in "http:
  • A simple but complete example of Bayes' Theorem from Dr. Math "/www.mathforum.com/dr.math/problems/battisfore.03.22.99.html"" in "http:

and that's the whole page, bestie ๐ŸŽ€ you ate. this was perl v5.42.0, dated 2025-07-25, and it's the AI::Ca...eBayes(3pm) page from the User Contributed Perl Documentation โ€” go categorize something iconic ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from