Skip to content

Commit 1ce41ba

Browse files
authored
Create knn-vs-ann.md
1 parent 3fb42ff commit 1ce41ba

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: kNN vs ANN
3+
description: Explanation and comparison of kNN and ANN algorithms.
4+
author: wmwxwa
5+
ms.author: wangwilliam
6+
ms.service: cosmos-db
7+
ms.topic: conceptual
8+
ms.date: 07/01/2024
9+
---
10+
11+
# kNN vs ANN
12+
13+
Two popular vector search algorithms are k-Nearest Neighbors (kNN) and Approximate Nearest Neighbors (ANN, not to be confused with Artificial Neural Network). kNN is precise but computationally intensive, making it less suitable for large datasets. ANN, on the other hand, offers a balance between accuracy and efficiency, making it better suited for large-scale applications.
14+
15+
## How kNN works
16+
17+
1. Vectorization: Each data point in the dataset is represented as a vector in a multi-dimensional space.
18+
2. Distance Calculation: To classify a new data point (query point), the algorithm calculates the distance between the query point and all other points in the dataset using a [distance function](distance-function.md).
19+
3. Finding Neighbors: The algorithm identifies the k closest data points (neighbors) to the query point based on the calculated distances. The value of k (the number of neighbors) is crucial. A small k can be sensitive to noise, while a large k can smooth out details.
20+
4. Making Predictions:
21+
- Classification: For classification tasks, kNN assigns the class label to the query point that is most common among the k neighbors. Essentially, it performs a "majority vote."
22+
- Regression: For regression tasks, kNN predicts the value for the query point as the average (or sometimes weighted average) of the values of the k neighbors.
23+
24+
## How ANN works
25+
26+
1. Vectorization: Each data point in the dataset is represented as a vector in a multi-dimensional space.
27+
2. Indexing and Data Structures: ANN algorithms use advanced data structures (e.g., KD-trees, locality-sensitive hashing, or graph-based methods) to index the data points, allowing for faster searches.
28+
3. Distance Calculation: Instead of calculating the exact distance to every point, ANN algorithms use heuristics to quickly identify regions of the space that are likely to contain the nearest neighbors.
29+
4. Finding Neighbors: The algorithm identifies a set of data points that are likely to be close to the query point. These neighbors are not guaranteed to be the exact closest points but are close enough for practical purposes.
30+
4. Making Predictions:
31+
- Classification: For classification tasks, ANN assigns the class label to the query point that is most common among the identified neighbors, similar to kNN.
32+
- Regression: For regression tasks, ANN predicts the value for the query point as the average (or weighted average) of the values of the identified neighbors.

0 commit comments

Comments
 (0)