- Type
- Supervised learning algorithm
- Task
- Classification and regression
- Key idea
- Majority vote among the k closest training points
- Formalised
- 1967 (Cover and Hart)
- Related
- Cosine Similarity, Vector Database, FAISS, Semantic Search
- Type
- Supervised learning algorithm
- Task
- Classification and regression
- Key idea
- Majority vote among the k closest training points
- Formalised
- 1967 (Cover and Hart)
- Related
- Cosine Similarity, Vector Database, FAISS, Semantic Search
Background
The method dates to at least 1951 in fixed-k form and was formalised in a 1967 paper by Thomas Cover and Peter Hart, which proved an important theoretical result: as the number of training examples grows, the error rate of the 1-nearest neighbour classifier converges to twice the optimal Bayes error, making kNN surprisingly strong given enough data.[1] It became a standard baseline in pattern recognition and remains a teaching algorithm because it requires almost no assumptions about the underlying distribution.
Key concepts
Three design choices determine kNN's behaviour:
- Distance metric. Predictions depend on how closeness is measured. Euclidean distance suits dense numeric features; Manhattan distance suits grid-like data; and cosine similarity — comparing vector direction rather than magnitude — dominates text and embedding-based retrieval.[2]
- Choice of k. A small k (often 1) makes the model sensitive to noise and produces jagged decision boundaries; a large k smooths boundaries but can blur distinct classes. k is typically tuned by cross-validation.
- Feature scaling. Because distance is computed over raw features, variables with large ranges dominate unless inputs are normalised or standardised.
Known weaknesses include the curse of dimensionality — in very high dimensions, distance concentration makes "nearest" barely closer than "farthest", degrading accuracy — plus sensitivity to irrelevant features and to class imbalance in dense neighbourhoods.[1]
Applications
kNN underpins recommender systems ("users who bought this also bought"), document and image retrieval, outlier detection in fraud and cybersecurity, and imputation of missing values. Most importantly for modern AI, vector search is kNN: retrieval-augmented generation (RAG) finds relevant context by running nearest-neighbour queries over embedding vectors, and semantic-search and similarity features in AI products are kNN with cosine distance at scale.[3]
>See Also
🇲🇾 Relevance to Malaysia: kNN is part of the standard machine-learning curriculum in Malaysian universities and data-science bootcamps, and is frequently the first algorithm taught in HRD Corp and MDEC-backed AI training programmes because its transparency suits regulatory expectations. Under Malaysia's AI Governance Framework and PDPA, models used in credit, hiring or enforcement decisions must be explainable — kNN's "these are the similar cases" reasoning is far easier to audit than a deep network's internals. Practically, Malaysian e-commerce and fintech teams building RAG assistants or product-recommendation engines rely on nearest-neighbour search over embeddings, so understanding its scaling limits (index choice, dimensionality, feature normalisation) is directly relevant local engineering knowledge.[4][5]
References
- ↑Cover, T. and Hart, P. (1967). Nearest neighbor pattern classification. IEEE Transactions on Information Theory. https://ieeexplore.ieee.org/document/1053964
- ↑scikit-learn. Nearest Neighbors. https://scikit-learn.org/stable/modules/neighbors.html
- ↑Khandelwal, U. et al. (2020). Generalization through Memorization: Nearest Neighbor Language Models. https://arxiv.org/abs/1911.00172
- ↑Malaysia Digital Economy Corporation (MDEC). Malaysia's AI Governance Framework. https://www.mdec.my/
- ↑National AI Office Malaysia. Official website. https://ai.gov.my/