Tag: KNN

  • ML0061 KNN and K-means

    What are the key differences between KNN and K-means?

    Answer

    KNN(K-Nearest Neighbors) is a supervised algorithm that classifies data by considering the labels of its nearest neighbors, emphasizing prediction based on historical data. In contrast, K-Means is an unsupervised clustering technique that groups data together based solely on their similarity, without using any labels.

    Here are the key differences between KNN and K-Means:
    (1) Learning Type
    KNN: Supervised learning algorithm (used for classification/regression).
    K-Means: Unsupervised learning algorithm (used for clustering).
    (2) Objective
    KNN: Predict the label of a new sample based on the majority vote (or average) of its K nearest neighbors.
    K-Means: Partition the dataset into K clusters by minimizing intra-cluster distance.
    (3) Training
    KNN: No explicit training; It simply stores the entire training dataset.
    K-Means: Involves an iterative training process to learn cluster centroids.
    (4) Prediction
    KNN: Computationally expensive, computes the distance from the test point to every training point. Sorts the distances and selects the top  K nearest neighbors. The majority votes for classification. Average of values for regression.
    K-Means: Fast and simple for inference, compute the distance of any new data point to each of the  K centroids. Assign it to the nearest centroid (i.e., predicted cluster).
    (5) Distance Metric Use
    KNN: Used to find neighbors.
    K-Means: Used to assign points to the nearest cluster center.
    (6) Output
    KNN: Outputs a label (classification) or value (regression).
    K-Means: Outputs cluster assignments and centroids.

    The table below summarizes the comparison between KNN and K-Means.


    Login to view more content
  • ML0056 K Selection in KNN

    In the context of designing a K-Nearest Neighbors (KNN) model, can you explain your approach to selecting the value of K?

    Answer

    Selecting the optimal value for ‘K’ in a K-Nearest Neighbors (KNN) model is crucial as it significantly impacts the model’s performance.
    (1) Bias-Variance Tradeoff: The choice of K involves balancing bias and variance.
    A small  K (e.g., 1) leads to low bias and high variance, often resulting in overfitting.
    A large  K increases bias but reduces variance, potentially underfitting the data.
    (2) Use Odd Values for Classification: In binary classification, odd  K avoids ties.
    (3) Cross-Validation Combined with Grid Search: Use k-fold cross-validation to evaluate performance across multiple values of  K , and select the one that minimizes the validation error.
    Cross-Validation Error can be calculated by the below equation.
     CV(K) = \frac{1}{N}\sum_{i=1}^{N} \ell\big(y_i, \hat{y}_i(K)\big)
    Where:
     y_i is the actual outcome for the i‑th instance.
     \hat{y}_i(K) represents the predicted value using  K neighbors.
     N is the total number of validation samples.
     \ell is a loss function.
    (4) Domain Knowledge: In some cases, prior knowledge for the data distribution can help select a reasonable range of  K .

    The example below apply k-fold cross-validation with grid search for K selection in one KNN regression task.


    Login to view more content
  • ML0055 KNN Regression

    Please explain how KNN Regression works.

    Answer

    K-Nearest Neighbors (KNN) Regression is a simple, instance-based learning algorithm that predicts a continuous output by finding the K nearest data points in the training set and averaging their output values. Its simplicity makes it easy to understand and implement, but its performance can be sensitive to the choice of K and the distance metric. Additionally, it can be computationally expensive during the prediction phase, especially for large datasets, since it requires comparing the query point to all training samples. KNN is often referred to as a “lazy learner” because it doesn’t build an explicit model during training—it simply memorizes the training data.
    (1) Instance-based method: KNN doesn’t learn an explicit model; it stores the training data and makes predictions based on similarity.
    (2) Distance-based: It finds the K nearest neighbors to a query point using a distance metric (commonly Euclidean distance).
    (3) Averaging Neighbors: The predicted value is the average of the target values of these K nearest neighbors.
    (4) Sensitive to K and distance metric: The performance depends on the choice of  K and how distance is measured (Euclidean, Manhattan, etc.).
    (5) No training phase: All computation happens during prediction (also called lazy learning).

    Below is the equation for the Prediction Calculation in KNN Regression:
    \hat{y} = \frac{1}{K}\sum_{i=1}^{K} y_i
    Where:
    \hat{y} is the predicted value for the query point.
     y_i represents the target value of the i‑th nearest neighbor.
     K denotes the number of neighbors considered.

    The example below shows KNN for regression.


    Login to view more content
  • ML0054 KNN Classification

    Please explain how KNN classification works.

    Answer

    K-Nearest Neighbors (KNN) is a simple, non-parametric algorithm that predicts a label by majority vote among the  K nearest neighbors of a test point, using a chosen distance metric. It is intuitive and effective for small datasets, though less efficient on large-scale data.
    (1) Instance-based method: KNN doesn’t learn an explicit model; it stores the training data and makes predictions based on similarity.
    (2) Distance-based classification: For a test point  \mathbf{x} , it computes the distance to every training point (e.g., Euclidean distance).
    (3) Majority vote: It selects the  K closest neighbors and assigns the label that appears most frequently among them.
    (4) Sensitive to K and distance metric: The performance depends on the choice of  K and how distance is measured (Euclidean, Manhattan, etc.).
    (5) No training phase: All computation happens during prediction (also called lazy learning).

    Below is the equation for Euclidean Distance calculation:
    \mbox{distance}(x, y) = \sqrt{\sum_{i=1}^{n} (x_i - y_i)^2}
    Where:
     x_i and  y_i are the ith features of the new and training points, respectively.
     n is the number of features

    Below is the equation for the Voting Rule in KNN classification:
    \hat{y} = \arg\max_{c \in \mathcal{C}} \sum_{i=1}^{K} \mathbb{1}(y_i = c)
    Where:
     \hat{y} is the predicted class label for the query point.
     \mathcal{C} represents the set of all possible classes.
     K is the number of nearest neighbors considered.
     y_i is the class label of the i-th neighbor.
     \mathbb{1}(y_i = c) is an indicator function, returning 1 if the neighbor’s class is  c , and 0 otherwise.

    The example below shows KNN for classification.


    Login to view more content