AI::Ca...er::KNN(3pm)

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

๐Ÿ’– 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 currently 0.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 is 5. ๐Ÿ‘‘

โœจ 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 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 ๐ŸŒŸ


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