.. index:: single: knn .. _knn/0: .. rst-class:: right **object** ``knn`` ======= k-Nearest Neighbors classifier with multiple distance metrics and weighting options. Learns from a dataset object implementing the ``dataset_protocol`` protocol and returns a classifier term that can be used for prediction and exported as predicate clauses. | **Availability:** | ``logtalk_load(knn(loader))`` | **Author:** Paulo Moura | **Version:** 1:0:0 | **Date:** 2026-02-20 | **Compilation flags:** | ``static, context_switching_calls`` | **Implements:** | ``public`` :ref:`classifier_protocol ` | **Imports:** | ``public`` :ref:`options ` | **Uses:** | :ref:`format ` | :ref:`list ` | :ref:`pairs ` | :ref:`type ` | **Remarks:** - Algorithm: k-NN is a lazy learning algorithm that classifies instances based on the majority class among the k nearest training instances. - Distance metrics: Supports Euclidean, Manhattan, Chebyshev, and Minkowski distance metrics. - Weighting schemes: Supports uniform, distance-based, and Gaussian weighting of neighbors. - Feature types: Automatically handles numeric and categorical features. - Classifier representation: The learned classifier is represented (by default) as a ``knn_classifier(AttributeNames, FeatureTypes, Instances)`` where ``Instances`` contains the training data. | **Inherited public predicates:** |  :ref:`options_protocol/0::check_option/1`  :ref:`options_protocol/0::check_options/1`  :ref:`classifier_protocol/0::classifier_to_clauses/4`  :ref:`classifier_protocol/0::classifier_to_file/4`  :ref:`options_protocol/0::default_option/1`  :ref:`options_protocol/0::default_options/1`  :ref:`classifier_protocol/0::learn/2`  :ref:`options_protocol/0::option/2`  :ref:`options_protocol/0::option/3`  :ref:`classifier_protocol/0::predict/3`  :ref:`classifier_protocol/0::print_classifier/1`  :ref:`options_protocol/0::valid_option/1`  :ref:`options_protocol/0::valid_options/1`   .. contents:: :local: :backlinks: top Public predicates ----------------- .. index:: predict/4 .. _knn/0::predict/4: ``predict/4`` ^^^^^^^^^^^^^ Predicts the class label for a new instance using the learned classifier and the given options. The instance is a list of ``Attribute-Value`` pairs. | **Compilation flags:** | ``static`` | **Template:** | ``predict(Classifier,Instance,Class,Options)`` | **Mode and number of proofs:** | ``predict(+compound,+list,-atom,+list(compound))`` - ``one`` ------------ .. index:: predict_probabilities/3 .. _knn/0::predict_probabilities/3: ``predict_probabilities/3`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Predicts class probabilities for a new instance using the learned classifier and default options. Returns a list of ``Class-Probability`` pairs. The instance is a list of ``Attribute-Value`` pairs. | **Compilation flags:** | ``static`` | **Template:** | ``predict_probabilities(Classifier,Instance,Probabilities)`` | **Mode and number of proofs:** | ``predict_probabilities(+compound,+list,-list)`` - ``one`` ------------ .. index:: predict_probabilities/4 .. _knn/0::predict_probabilities/4: ``predict_probabilities/4`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Predicts class probabilities for a new instance using the learned classifier and the given options. Returns a list of ``Class-Probability`` pairs. The instance is a list of ``Attribute-Value`` pairs. | **Compilation flags:** | ``static`` | **Template:** | ``predict_probabilities(Classifier,Instance,Probabilities,Options)`` | **Mode and number of proofs:** | ``predict_probabilities(+compound,+list,-list,+list(compound))`` - ``one`` ------------ Protected predicates -------------------- (no local declarations; see entity ancestors if any) Private predicates ------------------ (no local declarations; see entity ancestors if any) Operators --------- (none) .. seealso:: :ref:`dataset_protocol `, :ref:`c45 `, :ref:`isolation_forest `, :ref:`naive_bayes `, :ref:`nearest_centroid `, :ref:`random_forest `, :ref:`ada_boost `