← Back to all posts
Artificial Intelligence, Machine Learning, and Security Research Department2 min read

kNN Algorithm

An overview of the k-nearest neighbors (kNN) algorithm, which classifies a sample according to the categories of its nearest neighbors in the feature space.

Read in Chinese→
  1. Overview of the kNN Algorithm

The core idea of the kNN algorithm is that if the majority of a sample’s k nearest neighbors in the feature space belong to a particular class, then the sample also belongs to that class and shares the characteristics of samples in that class. When making a classification decision, this method relies solely on the class of the nearest sample or samples to determine the class of the sample to be classified.

  1. Introduction to the kNN Algorithm

The simplest and most basic classifier records the classes corresponding to all training data. When the attributes of a test object exactly match those of a training object, the test object can be classified accordingly. However, it is unlikely that every test object will have an exactly matching training object. Another issue is that a test object may match multiple training objects simultaneously, potentially causing it to be assigned to multiple classes. kNN was developed to address these problems.

kNN performs classification by measuring the distances between different feature values. Its basic idea is that if the majority of the k most similar samples (i.e., the nearest samples in the feature space) belong to a particular class, then the sample also belongs to that class. K is usually an integer no greater than 20. In the kNN algorithm, the selected neighbors are objects that have already been correctly classified. When making a classification decision, this method relies solely on the class of the nearest sample or samples to determine the class of the sample to be classified.

在这里插入图片描述

在这里插入图片描述