AI::Ca...er::KNN(3pm)
๐ AI::Ca...er::KNN โ wait, it's AI::Categorizer::Learner::KNN ๐
okay bestie so this is the User Contributed Perl Documentation page for AI::Categorizer::Learner::KNN, aka the K Nearest Neighbour algorithm doing its thing for AI::Categorizer. yes the title is truncated, no we're not gonna let that stop us. sit down, hydrate, let's go ๐ซถ
โจ NAME โจ
AI::Categorizer::Learner::KNN โ the K Nearest Neighbour Algorithm for AI::Categorizer. that's it. that's the name. iconic.
๐ธ SYNOPSIS ๐ธ
here's the whole vibe, verbatim, do not touch the code bestie:
use AI::Categorizer::Learner::KNN;
# Here $k is an AI::Categorizer::KnowledgeSet object
my $nb = new AI::Categorizer::Learner::KNN(...parameters...);
$nb->train(knowledge_set => $k);
$nb->save_state('filename');
... time passes ...
$l = AI::Categorizer::Learner->restore_state('filename');
my $c = new AI::Categorizer::Collection::Files( path => ... );
while (my $document = $c->next) {
my $hypothesis = $l->categorize($document);
print "Best assigned category: ", $hypothesis->best_category, "\n";
print "All assigned categories: ", join(', ', $hypothesis->categories), "\n";
}
and yes $nb is the variable name even though this is KNN and not Naive Bayes. we don't question the ancestors. we respect the ancestors. ๐
๐ฆ DESCRIPTION ๐ฆ
so this module is a whole implementation of the k-Nearest-Neighbor decision-making algorithm, aimed at the task of document categorization (as defined by the AI::Categorizer module). the entire interface is spelled out in AI::Categorizer, so go read that for the full picture. this page is the KNN-specific tea. ๐
๐ METHODS ๐
this class inherits from the AI::Categorizer::Learner class, so all of its methods are available to you unless it's explicitly mentioned here. inherited methods, we love to see it. ๐
โจ new() โจ
creates a fresh new KNN Learner and hands it back to you. on top of the parameters that the AI::Categorizer::Learner class already accepts, the KNN subclass accepts these extra ones:
thresholdโ sets the score threshold for category membership. the default is currently0.1. want more categories per document? set the threshold lower. want fewer? set it higher. it's a lowkey effective way to trade off between precision and recall. balance queen. โ๏ธk_valueโ sets the"k"value (as in k-Nearest-Neighbor, yes the name is right there) to the given integer. this tells it how many of each document's nearest neighbors get considered when assigning categories. default is5. ๐
โจ threshold() โจ
returns the current threshold value. and if you pass it an optional numeric argument, you can set the threshold too. getter and setter, main character energy fr fr. ๐
โจ train(knowledge_set => $k) โจ
trains the categorizer. this is the prep step so it can actually categorize documents later. the knowledge_set parameter has to provide an object of the class AI::Categorizer::KnowledgeSet (or a subclass of it), populated with lots of documents and categories. for the whole how-do-i-make-one-of-those situation, check AI::Categorizer::KnowledgeSet. ๐
โจ categorize($document) โจ
returns an AI::Categorizer::Hypothesis object representing the categorizer's "best guess" about which categories the given document should be assigned to. want the details on how to actually use that object? AI::Categorizer::Hypothesis has you covered. ๐ฎ
โจ save_state($path) โจ
saves the categorizer so you can use it later. heads up bestie, this method is inherited from AI::Categorizer::Storable. it's not even KNN's own method, it's just vibing here. ๐พ
๐ซถ AUTHOR ๐ซถ
originally written by David Bell (<dave@student.usyd.edu.au>), October 2002. ๐
then it was added to AI::Categorizer in November 2002, modified, and maintained by Ken Williams (<ken@mathforum.org>). legends only. ๐
๐ COPYRIGHT ๐
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. slay, open source. ๐
๐ SEE ALSO ๐
AI::Categorizer(3)"A re-examination of text categorization methods"by Yiming Yang โ <http://www.cs.cmu.edu/~yiming/publications.html> โ yes it's a link, yes it's in the original, yes you should read it if you're into the theory. academic baddie behaviour. ๐
ok that's the whole page bestie, every flag, every default, every method, nothing skipped ๐
and that's a wrap! this was perl v5.42.0, dated 2025-07-25, straight out of the User Contributed Perl Documentation collection. go categorize something, you've got this. periodt. ๐
โจ
๐ฆ where this page comes from
- fedora 43: perl-AI-Categorizer 0.09-38.fc43 ยท GPL-1.0-or-later OR Artistic-1.0-Perl
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms