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` classifier_protocol
Imports:
`public` options
Uses:
format
list
pairs
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:
check_option/1 check_options/1 classifier_to_clauses/4 classifier_to_file/4 default_option/1 default_options/1 learn/2 option/2 option/3 predict/3 print_classifier/1 valid_option/1 valid_options/1

## Public predicates

### `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`

### `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`

### `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)

See also

dataset_protocol, c45, isolation_forest, naive_bayes, nearest_centroid, random_forest, ada_boost