Author: admin

  • 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
  • ML0060 K Selection in K-Means

    How to select K in K-Means?

    Answer

    To select the optimal number of clusters  K in K-Means, use the visual plot like the elbow method, quantitative metrics like the silhouette score, or statistical methods like the gap statistic. These help balance model fit and generalization without overfitting.

    Elbow Method:
    (1) Plot the within-cluster sum of squares (WCSS) vs.  K .
    (2) Choose the “elbow” point where the rate of improvement slows.
    WCSS can be calculated using the following equation:
     \text{WCSS}(K) = \sum_{k=1}^{K} \sum_{x_i \in C_k} |x_i - \mu_k|^2
    Where:
     C_k is cluster  k ,
     \mu_k is its centroid.

    Here is one plot example to demonstrate the location of the elbow point.

    Silhouette Score:
    The silhouette score measures how well each point lies within its cluster. It ranges from -1 (wrong clustering) to 1 (well-clustered).
    (1) Calculate the average silhouette score for different  K values.
    (2) Choose the  K that yields the highest average silhouette score.
    Silhouette coefficient for point  i can be calculated by the following equation.
     s(i) = \frac{b(i) - a(i)}{\max(a(i), b(i))}
    Where:
     a(i) = intra-cluster distance,
     b(i) = nearest-cluster distance.

    Gap Statistic:
    (1) Compares clustering against a random reference distribution.
    (2) Choose  K that maximizes the gap between observed and expected WCSS.


    Login to view more content

  • ML0059 K-means II

    K-Means is widely used for clustering. Can you discuss its main benefits as well as its disadvantages?

    Answer

    K-Means clustering is a widely used unsupervised learning algorithm that partitions data points into K clusters, where each point belongs to the cluster with the nearest mean. While it is computationally efficient and easy to implement, it relies on prior specification of the number of clusters, assumes spherical clusters, and is sensitive to initialization and outliers.

    Objective Function of K-means:
    J = \sum_{i=1}^{K} \sum_{x_j \in C_i} |x_j - \mu_i|^2
    Where:
     K is the number of clusters.
     C_i is the set of points in cluster  i .
     \mu_i is the centroid of cluster  i .
     x_j is a data point assigned to cluster  i .

    Main Benefits of K-means:
    (1) Simple & Efficient: Fast to compute, easy to implement.
    (2) Scalable: Handles large datasets well.
    (3) Unsupervised Learning: Requires no labeled data.
    (4) Interpretable: Cluster centroids are intuitive and interpretable.
    (5) Works Well on Spherical Clusters: Performs best when clusters are compact and well-separated.

    Main Disadvantages of K-means:
    (1) Must Specify  K : The number of clusters must be known in advance.
    (2) Sensitive to Initialization: Poor starting points may lead to suboptimal clustering.
    (3) Assumes Spherical Clusters: Fails on clusters with irregular shapes or varying densities.
    (4) Affected by Outliers: Outliers can skew centroids and degrade performance.
    (5) Only Uses Euclidean Distance: Not suitable for non-numeric or categorical features.

    Examples below demonstrate K-Means performance on spherical clusters and Irregular shape clusters.


    Login to view more content
  • ML0058 K-means++

    Please explain how K-means++ works.

    Answer

    K-means++ is an improved way to initialize centroids in K-means. K-means++ selects initial centroids one by one using a weighted probability based on squared distances from already chosen centroids. This spreads out the centroids more effectively, reducing the chances of poor clustering and helping the algorithm converge faster and more reliably.

    K-means++ Steps:
    (1) Choose the first centroid  \mu_1 uniformly at random from the dataset.
    (2) For each point  x_i , compute its squared distance to the nearest chosen centroid:
     D(x_i)^2 = \min_{1 \le j \le m} |x_i - \mu_j|^2
    Where:
     \mu_j is one of the already chosen centroids.
    (3) Choose the next centroid  \mu_{m+1} with probability:
     P(x_i) = \frac{D(x_i)^2}{\sum_j D(x_j)^2}
    Where:
     D(x_i)^2 is the squared distance from point  x_i to its nearest already chosen centroid.
     \sum_j D(x_j)^2 is the sum of minimum squared distances from all data points to their nearest chosen centroid.
    (4) Repeat until  K centroids are chosen.
    (5) Then proceed with standard K-means clustering.

    Below shows an example for K-means++ clustering.


    Login to view more content
  • ML0057 K-means

    Please explain how K-means works.

    Answer

    K-means is an iterative unsupervised algorithm that groups data into  K clusters by minimizing intra-cluster distances. It alternates between assigning points to the nearest centroid and updating centroids until convergence. It is fast and easy to implement, but sensitive to initialization and non-convex cluster shapes.

    Goal of K-means
    : Partition data into  K clusters by minimizing within-cluster variance.

    K-means Steps:
    (1) Initialization: Randomly choose  K centroids.
    (2) Assignment step: Assign each point to the closest centroid using Euclidean distance, given by:
     d(x, c_k) = \sqrt{\sum_{i=1}^{n} (x_i - c_{k,i})^2}
    Where:
     x is the data point.
     c_k is the cluster center.
    (3) Update Step: Compute new cluster centers as the mean of all points assigned to that cluster by:
     c_k = \frac{1}{C_k} \sum_{x \in C_k} x
    Where:
     C_k represents the set of points assigned to cluster  k .
    (4) Convergence: Repeat assignment and update steps until cluster centers stabilize or a stopping criterion is met.

    Below shows an example for K-means clustering.


    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
  • ML0053 Hinge Loss for SVM

    Explain the Hinge Loss function used in SVM.

    Answer

    The Hinge Loss function is a key element in Support Vector Machines that penalizes both misclassified points and correctly classified points that lie within the decision margin. It assigns zero loss to points that are correctly classified and lie outside or exactly on the margin, and applies a linearly increasing loss as points move closer to or across the decision boundary. This loss structure encourages the SVM to maximize the margin between classes, promoting robust and generalizable decision boundaries.

    The Hinge Loss is defined as follows.
     \text{Hinge Loss} = \max(0,\ 1 - y \cdot f(\mathbf{x}))
    Where:
     y \in {-1, +1} is the true label,
     f(\mathbf{x}) is the raw model output.

    Hinge Loss is plotted in the figure below.

    Zero Loss: When  y \cdot f(\mathbf{x}) \ge 1 , meaning the point is correctly classified with margin.
    Positive Loss: When  y \cdot f(\mathbf{x}) < 1 , the point is either inside the margin or misclassified.


    Login to view more content
  • ML0052 Non-Linear SVM

    Can you explain the concept of a non-linear Support Vector Machine (SVM)?

    Answer

    A non-linear SVM allows classification of data that isn’t linearly separable by using a kernel function to project the data into a higher-dimensional space implicitly. This approach, known as the kernel trick, provides flexibility in handling complex datasets while maintaining computational efficiency. The choice of kernel, such as RBF, polynomial, or sigmoid, can greatly influence the performance and adaptability of the model.

    Kernel Trick: Converts input data into a higher-dimensional space where a linear separation is possible, even if the original data is non-linearly separable.

    Common Kernels:
    Polynomial Kernel:
    Uses polynomial functions of the input features to capture non-linear patterns in the data.

    K(\mathbf{x}_i, \mathbf{x}_j) = (\gamma \mathbf{x}_i^\top \mathbf{x}_j + c)^d
    Where:
     \mathbf{x}_i, \mathbf{x}_j are input vectors.
    \gamma controls the scale of the inner product.
     c is a constant that controls the influence of higher-order terms.
     d is the degree of the polynomial.

    Radial Basis Function (RBF) Kernel:
    Measures local similarity based on the Euclidean distance between points; nearby points have higher similarity.
     K(\mathbf{x}_i, \mathbf{x}_j) = \exp\left(-\gamma \|\mathbf{x}_i - \mathbf{x}_j\|^2\right)
    Where:
     \mathbf{x}_i, \mathbf{x}_j are input vectors.
     \|\mathbf{x}_i - \mathbf{x}_j\|^2 is the squared Euclidean distance between the vectors.
    \gamma controls the scale of the inner product.
     \sigma is a parameter that controls the width of the Gaussian (spread).

    Sigmoid Kernel:
    Imitates neural activation by applying a tanh function to the dot product of inputs, introducing non-linearity.
     K(\mathbf{x}_i, \mathbf{x}_j) = \tanh(\gamma \mathbf{x}_i^\top \mathbf{x}_j + c)
    Where:
     \mathbf{x}_i, \mathbf{x}_j are input vectors.
    \gamma controls the scale of the inner product.
    c is a bias term.

    Objective: Determine an optimal hyperplane in the transformed space that maximizes the margin between classes, effectively improving classification performance.

     \max_{\boldsymbol{\alpha}} \sum_{i=1}^{n} \alpha_i - \frac{1}{2} \sum_{i=1}^{n} \sum_{j=1}^{n} \alpha_i \alpha_j y_i y_j K(\mathbf{x}_i, \mathbf{x}_j)

    The example below compares a Linear Support Vector Machine with a Non-Linear Support Vector Machine.


    Login to view more content