Workflow🔗
A k-NN classifier is created from labeled feature vectors and can subsequently be used to classify unknown samples. Unlike parametric classifiers, k-NN stores the training samples and determines the class of an unknown sample based on its nearest neighbors.
The following section introduces the general workflow for k-NN classification.
General workflow🔗
Most k-NN classification tasks follow the same workflow:
- Create the classifier.
- Add training samples.
- Train the classifier.
- Classify unknown samples.
- Evaluate the result.
Create the classifier🔗
This step prepares the classifier and defines the dimension of the feature vectors used for training and classification.
The classifier is created with
The returned classifier handle is required for all following steps.
Add training samples🔗
Training samples are provided as feature vectors together with their class labels.
Training samples can be added with
The feature vector contains the numeric features that describe the sample. Each training sample must be assigned to a class using its class ID.
Several training samples are added by successively calling add_sample_class_knn with different feature vectors and class IDs.
The quality and representativeness of the training samples have a direct influence on the classification result. The training data should therefore cover the relevant variation within each class.
Train the classifier🔗
After all training samples have been added, the classifier is trained. During this step, the internal representation of the training samples is prepared for an efficient search of the nearest neighbors.
Training is performed with
Depending on the selected configuration, the training step can also normalize the feature vectors and prepare the internal search structure used during classification.
Further information on parameter optimization can be found in the Optimization chapter.
Classify unknown samples🔗
Unknown objects are represented by feature vectors and classified based on their nearest neighbors in the training data.
Classification is performed with
During classification, the k nearest neighbors of the unknown sample are determined. The selected classification method then uses these neighbors to determine the classification result.
Depending on the selected method, the operator can return the best-rated classes, information about their distances, or the nearest neighbors themselves.
Evaluate the result🔗
After training and classification, the classifier performance should be evaluated using representative test data that was not used during training.
Evaluating the classifier helps to assess the classification performance and compare different parameter settings.
In particular, the evaluation can be used to determine whether the selected value of k and the training data provide sufficient classification performance.
Further recommendations on classifier evaluation and parameter optimization can be found in the Optimization chapter.