Objectives
Upon completion of this lesson, you will be able to:
- define the kNN algorithm
- know when to use kNN
- tune the value of the hyper-parameter k
- appreciate the need for feature normalization
- recognize why outliers can be problematic for kNN
Introduction
k-Nearest Neighbors (k-NN or kNN) is a type of instance-based learning algorithm used in both classification and regression tasks. It is called ‘lazy learning’ as it doesn’t build a model but instead memorizes the training dataset. The prediction is done based on the “neighborhood” of data points in the dataset.
The basic steps involved in the k-NN algorithm are:
Choose the number of k and a distance metric: The number of neighbors (k) and the type of distance are chosen as per the problem context. For example, you could choose k=3 and use Euclidean distance as the metric.
Find the k nearest neighbors of the sample that you want to classify: For each data point that you want to make a prediction for, you identify the k points in the training dataset that are “closest” according to the chosen distance metric.
Assign the label: For a classification problem, the new data point is assigned the class which has the majority among its k nearest neighbors. For a regression problem, the new data point could be assigned the mean value of its k nearest neighbors.
kNN is a simple algorithm that is easy to understand and implement. It works well with smaller datasets that have fewer dimensions (features), and where the data points are clearly separable. However, it may not be suitable for large, high-dimensional datasets, as the computation of distances can become very expensive. It is also sensitive to irrelevant or correlated features, which can impact the calculation of “closest” neighbors.
An important aspect of kNN is the choice of k. A small value of k can make the algorithm sensitive to noise, while a large value of k makes the boundaries between classes less distinct. Typically, the optimal value of k is selected using techniques such as cross-validation.
Examples of applications of kNN include recommendation systems, image recognition, and text categorization, among others.
Required Data Shaping
Prior to applying kNN, the data must be shaped:
- remove outliers
- impute or remove missing feature values
- normalize numeric features
- encode categorical features
Tutorials
In the narrated presentation below, Khoury Boston’s Dr. Schedlbauer provides an introduction to the kNN machine learning algorithm for predicting categorical target variables.
In the presentation below, Dr. Schedlbauer provides a more detailed look at kNN, its implementation, and the data preparation steps required for kNN.
References
No references.
Errata
None collected yet. Let us know.
LS0tCnRpdGxlOiAia05OIGZvciBDbGFzc2lmaWNhdGlvbiBhbmQgUmVncmVzc2lvbiIKcGFyYW1zOgogIGNhdGVnb3J5OiAzCiAgc3RhY2tzOiAwCiAgbnVtYmVyOiA0MTAKICB0aW1lOiA0NQogIG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