🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › k-nearest-neighbours
K-Nearest Neighbor(KNN) Algorithm - GeeksforGeeks
In the KNN algorithm k is just a number that tells the algorithm how many nearby points or neighbors to look at when it makes a decision. Example: Imagine you're deciding which fruit it is based on its shape and size.
Published: May 2, 2026
🌐
freeCodeCamp
freecodecamp.org › news › k-nearest-neighbors-algorithm-classifiers-and-model-example
KNN Algorithm – K-Nearest Neighbors Classifiers and Model Example
January 25, 2023 - Since the value of K is 3, the algorithm will only consider the 3 nearest neighbors to the green point (new entry). This is represented in the graph above. Out of the 3 nearest neighbors in the diagram above, the majority class is red so the new entry will be assigned to that class. The last data entry has been classified as red. In the last section, we saw an example the K-NN algorithm using diagrams.
Discussions

What applications is K-nearest neighbors good for?
Do you mean k-means clustering or k-nearest neighbors? I ask because the other responses at the time of writing seemed to be talking about k-means Well, k-nearest neighbors is what you said, so... Suppose I want to predict whether or not a person is a lawyer, suppose I know feature measurements X, Y, and Z about this person, and suppose I have a data set including measurements X, Y, and Z of many people, and whether or not they are lawyers. I then compute distances from my given person's data (X,Y,Z) to each of the people's data in the data set. I then choose the k nearest people, and run a voting scheme (for example, if he majority of them are lawyers then I predict that the given person is a lawyer). So basically, classification and regression. k-means, on the other hand, is an unsupervised learning technique used to cluster similar data. More on reddit.com
🌐 r/datascience
8
6
April 9, 2016
How does the KNN classifier actually work?
Yes, your understanding is correct. Let’s make a toy problem to help answer your second two questions. Say I have two labeled mushrooms with features. Edible mushroom, X_e has features [1,0,1] and non-edible mushroom X_n = [6, 5, 11]. Now i give you a mystery mushroom X_i and tell you its features are [1,1,2]. Do you think it’s more likely to be edible or not edible? We just trained a simple KNN classifier with k=1. But eyeballing isn’t a very systematic way of playing this game, especially at scale; so we need some way to formalize our intuition that X_i is closer to X_e than X_n. Euclidean distance is one way of doing that- in our toy example, sqrt(sum((X_i - X_e)2 )) = sqrt(12 + 12 ) = sqrt(2) and sqrt(sum((X_i - X_n)2 )) = sqrt(200). So we can say, at least in euclidean space, X_i is closer (or less far away) to X_e than to X_n. In KNN X_e « votes » its class, which in this case is « edible ». For larger numbers of k, other closest neighbors also have a vote, which is why chosen ‘k’ is almost always odd. The same process can be repeated irrespective of number of features (whether 3 or 20), but in higher dimensions, the things get counterintuitive (curse of dimensionality), so KNN eventually struggles when data dimensionality is too high. More on reddit.com
🌐 r/MLQuestions
8
4
December 4, 2023
What are some real life use cases where DBSCAN outperforms KNN?
We used DBSCAN in a fraud detection pipeline that performer extremely well. However due to extreme RAM requirements of DBSCAN we had to first run a clustering algorithm down over our data points. We ran the clustering algorithm with 50.000 clusters. The cluster centers was then picked as the new data points. We could then run dbscan on the cluster centers. Each cluster center that was deemed an outlier had all corresponding (original) data points set as outliers as well. Due to randomness of the clustering algorithm we ran the algorithm 15 times. An original data point had to be marked an outlier for 50% of the runs in order to be classified as outlier. More on reddit.com
🌐 r/datascience
14
2
April 6, 2023
Why is K-Nearest neighbor considered to be a type of machine learning?
Because it is a method for learning to predict new cases from training data. K is a meta-parameter controlling model complexity. Prediction are learned from the training data at test time. More on reddit.com
🌐 r/learnmachinelearning
32
27
February 14, 2020
🌐
W3Schools
w3schools.com › python › python_ml_knn.asp
Python Machine Learning - K-nearest neighbors (KNN)
It is based on the idea that the observations closest to a given data point are the most "similar" observations in a data set, and we can therefore classify unforeseen points based on the values of the closest existing points. By choosing K, the user can select the number of nearby observations to use in the algorithm. Here, we will show you how to implement the KNN algorithm for classification, and show how different values of K affect the results.
🌐
IBM
ibm.com › think › topics › knn
What is the k-nearest neighbors (KNN) algorithm
November 17, 2025 - Finance: It has also been used in a variety of finance and economic use cases. For example, one paper shows how using KNN on credit data can help banks assess risk of a loan to an organization or individual.
🌐
PubMed Central
pmc.ncbi.nlm.nih.gov › articles › PMC4916348
Introduction to machine learning: k-nearest neighbors - PMC
In this example we choose four nearest kinds of food, they are apple, green bean, lettuce, and corn. Because the vegetable wins the most votes, sweet potato is assigned to the class of vegetable. You can see that the key concept of kNN is easy to understand. ... Illustration of how k-nearest ...
🌐
Arize
arize.com › courses › deep dive on knn: understanding and implementing the k-nearest neighbors algorithm
Deep Dive on KNN: Understanding and Implementing the K-Nearest Neighbors Algorithm - ML Course
1 week ago - For example, an RGB image with dimensions 100×100 pixels can be represented as a feature vector of length 30,000 (100x100x3), where each element of the vector corresponds to the pixel value of the corresponding RGB channel.
🌐
Revoledu
people.revoledu.com › kardi › tutorial › KNN › KNN_Numerical-example.html
K Nearest Neighbors Tutorial: KNN Numerical Example (hand computation)
Here is step by step on how to compute K-nearest neighbors KNN algorithm: Determine parameter K = number of nearest neighbors · Calculate the distance between the query-instance and all the training samples · Sort the distance and determine nearest neighbors based on the K-th minimum distance ... Use simple majority of the category of nearest neighbors as the prediction value of the query instance · We will use again the previous example to calculate KNN by hand computation.
🌐
Medium
medium.com › data-science › k-nearest-neighbor-classifier-explained-a-visual-guide-with-code-examples-for-beginners-a3d85cad00e1
K Nearest Neighbor Classifier, Explained: A Visual Guide with Code Examples for Beginners
November 30, 2024 - The KNN algorithm assumes that similar things exist in close proximity, making it intuitive and easy to understand. ... Nearest Neighbor methods is one of the simplest algorithms in machine learning. Throughout this article, we’ll use this simple artificial golf dataset (inspired by [1]) as an example...
Find elsewhere
🌐
scikit-learn
scikit-learn.org › stable › modules › neighbors.html
1.6. Nearest Neighbors — scikit-learn 1.9.1 documentation
One example is kernel density estimation, discussed in the density estimation section. NearestNeighbors implements unsupervised nearest neighbors learning. It acts as a uniform interface to three different nearest neighbors algorithms: BallTree, ...
🌐
Medium
medium.com › swlh › k-nearest-neighbor-ca2593d7a3c4
K-Nearest Neighbor. A complete explanation of K-NN | by Antony Christopher | The Startup | Medium
February 3, 2021 - So for this identification, we ... measure. Our KNN model will find the similar features of the new data set to the cats and dogs images and based on the most similar features it will put it in either cat or dog category...
🌐
ScienceDirect
sciencedirect.com › topics › computer-science › k-nearest-neighbors-algorithm
k-Nearest Neighbors Algorithm - an overview | ScienceDirect Topics
The K-Nearest Neighbors (KNN) algorithm is a supervised learning classifier that employs proximity for classifications or predictions about the grouping of a data point [54]. Although it can be applied to classification or regression issues, it is commonly employed as a classification method since it relies on the concept that similar points can be discovered near one another [55]. It is also known as a lazy learner algorithm. The KNNs have been common in brain cancer segmentation, and the results of studies had different accuracy rates. For example, Havaei et al.
🌐
Wikipedia
en.wikipedia.org › wiki › K-nearest_neighbors_algorithm
k-nearest neighbors algorithm - Wikipedia
August 27, 2026 - "Output-sensitive algorithms for computing nearest-neighbor decision boundaries". Discrete and Computational Geometry. 33 (4): 593–604. doi:10.1007/s00454-004-1152-0. ↑ Hart, Peter E. (1968). "The Condensed Nearest Neighbor Rule". IEEE Transactions on Information Theory. 18: 515–516. doi:10.1109/TIT.1968.1054155. 1 2 Mirkes, Evgeny M.; KNN ...
🌐
Applied AI Blog
appliedaicourse.com › home › machine learning › k-nearest neighbor(knn) algorithm in machine learning
K-Nearest Neighbor(KNN) Algorithm in Machine Learning
October 18, 2024 - For instance, if a data point has five nearest neighbors, and three of them belong to class A while two belong to class B, the algorithm will classify the point as class A. In regression, KNN predicts continuous values by averaging the values ...
🌐
Analytics Vidhya
analyticsvidhya.com › home › knn algorithm: introduction to k-nearest neighbors algorithm for regression
KNN algorithm: Introduction to K-Nearest Neighbors Algorithm for Regression
December 27, 2025 - It predicts new data points based on their similarity to points in the training set. In our example, ID11 has height and age similar to ID1 and ID5, so its weight would be predicted accordingly.
🌐
DataCamp
datacamp.com › tutorial › k-nearest-neighbors-knn-classification-with-r-tutorial
K-Nearest Neighbors (KNN) Classification with R Tutorial | DataCamp
June 14, 2023 - K-Nearest Neighbors (KNN) is a supervised machine learning model that can be used for both regression and classification tasks. The algorithm is non-parametric, which means that it doesn't make any assumption about the underlying distribution of the data.
🌐
Towards Data Science
towardsdatascience.com › home › data science › comprehensive guide to approximate nearest neighbors algorithms
Comprehensive Guide To Approximate Nearest Neighbors Algorithms | Towards Data Science
July 28, 2021 - def build(self): self.index = nmslib.init(method='hnsw', space='cosinesimil') self.index.addDataPointBatch(self.vectors) self.index.createIndex({'post': 2}) def query(self, vector, k=10): indices = self.index.knnQuery(vector, k=k) return ...
🌐
DataCamp
datacamp.com › tutorial › k-nearest-neighbor-classification-scikit-learn
K-Nearest Neighbors (KNN) Classification with scikit-learn | DataCamp
February 20, 2023 - In the example below, if we choose to measure three points, we can say the three nearest points are pears, so I’m 100% sure this is a pear. If we choose to measure the four nearest points, three are pears while one is a grape, so we would say we are 75% sure this is a pear. We’ll cover how to find the best value for k and the different ways to measure distance later in this article. To further illustrate the kNN algorithm, let's work on a case study you may find while working as a data scientist.
🌐
Unstop
unstop.com › home › blog › knn algorithm - explained with example
KNN Algorithm - Explained With Example
January 6, 2025 - If two of the three neighbors are apples and one is an orange, the algorithm assigns the label "apple" to the new fruit, as the majority of its neighbors are apples. This process highlights the intuitive nature of KNN, where the classification ...
🌐
Scaler
scaler.com › home › topics › machine-learning › k-nearest neighbor (knn) algorithm in machine learning
K-Nearest Neighbor (KNN) Algorithm in Machine Learning - Scaler Topics
February 17, 2023 - KNN is one of the simplest forms ... new data points based on the similarity measure of the earlier stored data points. For example, if we have a dataset of tomatoes and bananas....
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › mathematical-explanation-of-k-nearest-neighbour
Mathematical explanation of K-Nearest Neighbour - GeeksforGeeks
July 23, 2025 - So according to KNN algorithm classifying based on Majority Vote, Angelina will be in the class of people who like cricket. This example illustrates the working of KNN and how it classifies data based on the majority class of its nearest neighbors.