K-Nearest Neighbors (KNN)

KNN Concept Mathematically

  • Tell me your friends, I'll tell your nature.
  • KNN Algorithm Steps:
    1. Calculate distance from test point to all training points.
    2. Select the k nearest neighbors.
    3. Predict the class based on the majority class of the neighbors.
Calculation Example for (2, 3, 1) with k=3:
Feature AFeature BFeature CClassDistance to Test Point
121Red√((2-1)² + (3-2)² + (1-1)²) = 1.41
211Red√((2-2)² + (3-1)² + (1-1)²) = 2
341Blue√((2-3)² + (3-4)² + (1-1)²) = 1.41
561Blue√((2-5)² + (3-6)² + (1-1)²) = 4.24
3 Nearest Neighbors: Red, Red, Blue => Prediction: Red

KNN Concept Visually: Step - 1

KNN Step 1
KNN Step 1: Calculate distance from test point to all training points.

KNN Concept Visually: Step - 2

KNN Step 2
KNN Step 2: Select the k nearest neighbors.

KNN Concept Visually: Step - 3

  • Prediction: Red (2 neighbors are Red, 1 is Blue)
KNN Step 3
KNN Step 3: Predict the class based on the majority class of the neighbors.

Advantages and Disadvantages of KNN

    Advantages
    1. Simple and easy to understand.
    2. No training phase, making it fast for small datasets.
    3. Can capture complex relationships if enough data is available.
    Disadvantages
    1. Computationally expensive for large datasets.
    2. Sensitive to the choice of k and the scale of features.
    3. Does not perform well with high-dimensional data.

KNN Hyperparameters

KNN Hyperparameters and their Effects:
HyperparameterDescriptionEffect on Model
n_neighbors: Number of neighborsNumber of nearest neighbors to consider for prediction.Small k => overfitting, larger k => underfitting.
metric: Distance MetricMethod used to calculate distance (e.g. euclidean, manhattan, cosine)Different metrics can capture different relationships in the data.
weightsMethod to weight the neighbors (e.g. uniform, distance)Distance weighting can improve performance by giving more influence to closer neighbors.
Key hyperparameters for KNN and their impact on model performance.