# K-Nearest Neighbours Explained, Practical Guide & How To Tutorial In Python

by | Aug 22, 2023 | Data Science, Machine Learning

## What is K-nearest neighbours?

K-Nearest Neighbours (KNN) is a simple and widely used classification and regression algorithm in machine learning. It falls under the category of supervised learning algorithms. The algorithm is used for classification, where the goal is to assign a class label to an input data point, and regression, where the goal is to predict a continuous numerical value based on input data.

The basic idea behind the KNN algorithm is to classify or predict a data point based on the majority class or average value of its “k” nearest neighbours in the feature space. In other words, the algorithm determines a data point’s class or value by looking at its nearest neighbours’ class or value.

### How does the KNN algorithm work?

The algorithm consists of 2 phases; the training phase and the prediction phase.

Training Phase:

During training, the algorithm simply stores the feature vectors and their corresponding class labels (for classification) or numerical values (for regression).

Prediction Phase:

• Given a new input data point, the algorithm calculates the distance (e.g., Euclidean distance) between this point and all the points in the training data.
• It then selects the “k” training data points closest to the input data point based on the calculated distances.
• The algorithm assigns the class label that appears most frequently among the “k” nearest neighbours to the input data point for classification.
• For regression, the algorithm calculates the average value of the target values of the “k” nearest neighbours and assigns this average as the predicted value for the input data point.

The choice of the parameter “k” is essential in the KNN algorithm. A smaller ” k ” value can lead to noisy predictions, as outliers might influence the algorithm. On the other hand, a larger value of “k” can lead to smoother decision boundaries but might also cause the algorithm to lose local patterns in the data.

KNN is relatively simple to understand and implement and can work well for specific datasets. However, it has some limitations, such as being sensitive to the choice of a distance metric, becoming computationally expensive for large datasets, and not performing well when the data has varying feature scales.

To use KNN effectively, it’s vital to preprocess the data appropriately and tune the value of “k” based on the characteristics of the data.

## The k-nearest neighbours algorithm

The k-Nearest Neighbours (KNN) algorithm is a simple and intuitive machine learning algorithm for classification and regression tasks. It operates based on the principle that similar data points have identical outcomes. Here’s a step-by-step explanation of the KNN algorithm:

1. Data Collection and Preparation:
• Collect a dataset with labelled examples (for classification) or target values (for regression).
• Split the dataset into a training set and a testing (or validation) set.
2. Choosing a Value for k:
• The critical parameter in KNN is “k,” which represents the number of neighbours influencing the prediction for a new data point.
• A smaller value of k makes the algorithm more sensitive to noise and outliers, while a larger value of k smoothens the decision boundaries.
3. Distance Calculation:
• For each data point in the testing set, calculate the distance (Euclidean distance is commonly used) between that point and all data points in the training set.
4. Finding Nearest Neighbours:
• Select the k training data points with the shortest distances to the testing data point. These are the “k nearest neighbours.”
5. Majority Vote (Classification) or Average (Regression):
• If the problem is binary, count the occurrences of each class among the k nearest neighbours and assign the class with the highest count to the testing data point.
• If the problem has multiple classes, you can use a weighted voting mechanism based on the distances.
• Take the average of the target values of the k nearest neighbours and assign this average as the predicted value for the testing data point.
6. Prediction and Evaluation:
• Repeat steps 3-5 for all data points in the testing set.
• Evaluate the algorithm’s performance using appropriate metrics such as accuracy (for classification) or mean squared error (for regression).
7. Model Tuning and Validation:
• Test the algorithm with different values of k to find the optimal value that provides the best performance on the validation set.
• You can also experiment with different distance metrics based on the characteristics of your data.
8. Final Model and Predictions:
• Once you’ve determined the optimal k value, use the entire training set to train the final KNN model.
• Apply the trained model to make predictions on new, unseen data.

It’s important to note that KNN has some limitations, including sensitivity to the choice of k and the distance metric, the requirement for a significant amount of memory to store the training data, and slower prediction times for large datasets. However, KNN can be a good starting point for simple problems and as a baseline comparison against more complex algorithms.

## Distance metrics for k-nearest neighbours

Distance metrics are crucial in the k-Nearest Neighbours (KNN) algorithm, as they determine how similarity or dissimilarity between data points is measured. The choice of distance metric can significantly impact the performance of the KNN algorithm. Here are some standard distance metrics used in KNN:

1. Euclidean Distance:
• The most commonly used distance metric in KNN.
• Calculates the straight-line distance between two points in the feature space.
• Suitable for continuous numerical features.
2. Manhattan Distance (City Block Distance):
• Calculates the distance between two points by summing the absolute differences between their coordinates along each dimension.
• Practical when dealing with features measured in different units.
3. Minkowski Distance:
• A generalization of both Euclidean and Manhattan distances.
• The parameter “p” controls the degree of the distance metric. When p=2, it’s equivalent to Euclidean distance; when p=1, it’s equivalent to Manhattan distance.
4. Cosine Similarity:
• Measures the cosine of the angle between two vectors in the feature space.
• Particularly useful for high-dimensional data and when the magnitude of the vectors matters less than their directions.
5. Chebyshev Distance:
• Also known as the maximum norm or L∞ norm.
• Calculates the maximum absolute difference between the corresponding coordinates of two points.
• Appropriate when you want to emphasize the largest difference between the coordinates.
6. Hamming Distance:
• Used for comparing binary or categorical data.
• Measures the number of positions at which two strings of equal length differ.
• Appropriate for feature vectors with binary or categorical attributes.
7. Jaccard Distance:
• They are used for sets of data (e.g., text documents represented as sets of words).
• Measures the dissimilarity between two sets by calculating the ratio of the intersection’s size to the union’s size.
• Suitable for text analysis and recommendation systems.
8. Correlation Distance:
• Measures the dissimilarity between two vectors based on their correlation coefficient.
• Reflects how the features of the two vectors vary relative to each other.
• Suitable when you’re interested in capturing linear relationships between features.

The choice of distance metric should align with the characteristics of your data and the problem you’re trying to solve. Experimenting with different distance metrics and evaluating their impact on the KNN algorithm’s performance can help you select the most appropriate one for your task.

## What value should you choose for k in k-nearest neighbours?

Choosing the correct value for k in the k-Nearest Neighbours (KNN) algorithm is a critical step, as it can significantly impact the model’s performance. Selecting an appropriate k value involves finding a balance between bias and variance. Here are a few approaches you can use to choose the optimal k value:

1. Cross-Validation:
• Split your dataset into multiple folds (e.g., using k-fold cross-validation).
• For each fold, train the KNN model with different k values and evaluate its performance on the validation set.
• Calculate the average performance metric (e.g., accuracy) for each k value across all folds.
• Choose the k value that results in the best average performance.
2. Odd vs. Even k Values:
• Choose odd k values to avoid ties when classifying data points with an equal number of nearest neighbours from different classes.
• Using an odd k value prevents indecisiveness in classification.
3. Elbow Method:
• Plot the performance metric (e.g., accuracy) as a function of different k values.
• Look for the point on the plot where the performance stabilizes or starts to decrease. This point resembles an “elbow.”
• This method helps you identify a value k that offers a good trade-off between bias and variance.
4. Grid Search:
• Perform a grid search over a predefined range of k values.
• Train and evaluate the model for each k value in the range.
• Choose the k value that gives the best performance on a validation set.
5. Domain Knowledge:
• Sometimes, domain knowledge can help you make an informed decision about the k value. For example, if you know that the problem is expected to have specific characteristics, you can choose a k value accordingly.
6. Use Case-Specific Testing:
• Experiment with different k values and assess the model’s performance on a separate test dataset that wasn’t used during training.
• This approach helps you directly observe how different k values affect real-world predictions.

It’s important to note that no universally optimal k value works for all datasets and problems. The choice of k should be based on a combination of experimentation, validation, and understanding the nature of your data. Remember that a small k value might lead to overfitting and noise sensitivity, while a large k value might result in over smoothing and loss of local patterns.

The k-Nearest Neighbours (KNN) algorithm has advantages and disadvantages, which are essential when deciding whether to use it for a particular machine learning task.

• Simple and Intuitive: KNN is easy to understand and implement. It serves as a good starting point for beginners in machine learning.
• Non-Parametric: KNN is a non-parametric algorithm, meaning it doesn’t make any assumptions about the underlying data distribution. This makes it versatile and applicable to a wide range of problems.
• Adaptable to Different Data Types: KNN can handle numerical and categorical data. It’s also suitable for mixed data types, making it flexible in various scenarios.
• No Training Phase: KNN doesn’t involve an explicit training phase. The algorithm stores the training data so that new data can be classified or predicted instantly.
• Can Capture Complex Decision Boundaries: KNN can capture intricate decision boundaries, making it useful for problems where the classes are not linearly separable.
• No Model Assumption: Since KNN doesn’t make any assumptions about the data distribution, it can work well for cases where the true relationship between features and target is complex or unknown.

• Computationally Expensive: KNN’s prediction time grows linearly with the size of the training dataset. For large datasets, predicting with KNN can be slow.
• Sensitive to Noisy Data: Outliers and noisy data points can significantly influence the prediction in KNN, especially with small values of “k.”
• Choosing the Right “k”: Selecting the appropriate value for “k” is crucial. A small value can lead to overfitting, while a large value can lead to over smoothing and loss of local patterns.
• Imbalanced Data: KNN doesn’t handle imbalanced datasets well, as the class with more instances might dominate the prediction for new data points.
• Distance Metric Sensitivity: KNN’s performance heavily depends on the choice of distance metric. Using an inappropriate distance metric can lead to suboptimal results.
• Curse of Dimensionality: KNN’s performance can deteriorate in high-dimensional spaces as the distance becomes less meaningful and the data points become more sparse.
• Memory Usage: Storing the entire training dataset for prediction requires significant memory, especially for large datasets.
• Bias from Irrelevant Features: KNN gives equal importance to all features, meaning irrelevant features can negatively impact performance.
• Lack of Explanation: KNN doesn’t provide insights into why a particular prediction was made, as it relies solely on the nearest neighbours.

KNN is a simple and versatile algorithm that can be effective in specific scenarios, especially when the dataset and the complex relationships between features and targets are relatively small. However, knowing its limitations and considering the trade-offs before using KNN for a specific task is essential.

## What is k-nearest neighbours used for?

The k-Nearest Neighbours (KNN) algorithm has found applications in various fields due to its simplicity and versatility. Here are some common areas where KNN is applied:

1. Classification:
• KNN is frequently used for classification tasks where the goal is to assign a class label to a data point based on its neighbours.
• It’s used in image recognition, spam detection, sentiment analysis, medical diagnosis, and more.
2. Regression:
• KNN can be used for regression tasks, where the goal is to predict a continuous numerical value based on the values of neighbouring data points.
• Applications include predicting housing prices, stock prices, and other numeric predictions.
3. Anomaly Detection:
• KNN can help identify outliers or anomalies in a dataset by identifying data points significantly different from their neighbours.
4. Collaborative Filtering:
• In recommendation systems, KNN can identify similar users or items based on their behaviour, helping to make personalized recommendations.
5. Clustering:
• KNN can be applied for clustering or grouping similar data points. It’s used in data segmentation, customer segmentation, and market analysis.
6. Image Segmentation:
• KNN can segment images into regions with similar characteristics, which is helpful in image processing and computer vision tasks.
7. Natural Language Processing (NLP):
• KNN can be used in NLP tasks like text classification and sentiment analysis, where text documents are represented as vectors and similarity is calculated based on words or phrases.
8. Bioinformatics:
• KNN is applied to DNA sequence analysis, protein structure prediction, and other biological data analysis tasks.
9. Geographical and Spatial Analysis:
• KNN is used in geographical information systems (GIS) to analyze spatial data and identify spatial patterns.
10. Quality Control and Manufacturing:
• KNN is applied in quality control processes to identify defective products based on their similarity to known faulty products.
11. Financial Analysis:
• KNN can be used for credit scoring, fraud detection, and stock price prediction in the financial domain.
12. Medicine and Healthcare:
• KNN is used for disease diagnosis based on patient attributes and medical history.
13. Robotics:
• KNN can assist robots in obstacle avoidance by identifying nearby obstacles and planning their paths accordingly.
14. Environmental Monitoring:
• KNN can analyze environmental data and identify trends or anomalies in pollution levels, weather patterns, and more.

Remember that while KNN can be effective in various applications, its performance depends on factors like the choice of a distance metric, the value of “k,” the quality of the data, and the specific problem characteristics.

## KNN in Natural Language Processing (NLP)

K-Nearest Neighbours (KNN) can also be applied to natural language processing (NLP) tasks, where text data is analyzed to extract information, make predictions, or perform other tasks. While KNN is more commonly associated with numerical data, it can be adapted and used effectively in NLP with appropriate feature engineering and distance metrics. Here are some ways KNN can be applied in NLP:

1. Text Classification:
2. Sentiment Analysis:
• KNN can be applied to sentiment analysis tasks where the goal is to determine the sentiment expressed in a text (e.g., positive, negative, neutral).
• Text data can be preprocessed, transformed into numerical vectors using methods like bag-of-words or word embeddings, and then classified using KNN.
3. Document Retrieval:
• KNN can be used for document retrieval tasks where the goal is to find relevant documents for a given query.
• Text documents and queries are represented as numerical vectors, and distance metrics compute similarity scores.
• The top-k documents with the highest similarity scores can be retrieved as results.
4. Named Entity Recognition (NER):
• KNN can be applied to NER tasks, where the goal is to identify entities in text, such as names of people, organizations, locations, etc.
• Text data can be tokenized, and features representing context and linguistic information can be used to create numerical vectors.
5. Text Clustering:
• KNN can perform text clustering, grouping similar documents.
• Vector representations of text data can be clustered using KNN to identify clusters of related documents.
6. Spell Correction:
• KNN can be used for simple spell correction tasks by suggesting correct words based on the k nearest neighbours of a misspelt word.
7. Topic Modeling:
• KNN can assist in topic modelling tasks by identifying similar documents or paragraphs based on their content.
8. Plagiarism Detection:
• KNN can be applied to identify potential cases of plagiarism by measuring the similarity between documents.

Keep in mind that while KNN can be applied to NLP tasks, it might not always yield the best performance compared to more sophisticated algorithms designed specifically for text data, such as support vector machines (SVMs), random forests, or deep learning models.

## How to implement a k-nearest neighbours classifier in Python

Here is an example implementation of a k-Nearest Neighbours (KNN) classification using Python and the popular machine learning library scikit-learn.

Before you start, ensure you have the scikit-learn library installed:

``pip install scikit-learn``

In this example, we will use the well-known Iris dataset for classification tasks. We split the dataset into training and testing sets, create a KNN classifier with k=3, train it on the training data, make predictions on the test data, and calculate the accuracy of the predictions.

``````from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

# Load the Iris dataset as an example
X = iris.data
y = iris.target

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Create a KNN classifier with k=3
knn_classifier = KNeighborsClassifier(n_neighbors=3)

# Train the classifier on the training data
knn_classifier.fit(X_train, y_train)

# Make predictions on the test data
predictions = knn_classifier.predict(X_test)

# Calculate the accuracy of the classifier
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy: {accuracy:.2f}")``````

Remember that this is a basic example, and there are many other aspects you can explore when working with KNN, such as hyperparameter tuning, different distance metrics, handling imbalanced data, and more.

## Conclusion

Tthe k-Nearest Neighbours (KNN) algorithm is a versatile and intuitive classification and regression method that can be applied across various domains. It’s a simple yet powerful approach that relies on the similarity between data points to make predictions. Here’s a recap of the key points:

• KNN Principle: KNN predicts the label or value of a new data point by considering the labels or values of its k nearest neighbours in the training data.
• Distance Metrics: The choice of distance metric (e.g., Euclidean, Manhattan, cosine) is crucial and should be based on the nature of the data.
• Choosing “k”: The right value for “k” is essential. A small “k” can lead to overfitting, while a large “k” can result in over smoothing. Various techniques, such as cross-validation and the elbow method, can help determine the optimal “k.”
• Advantages: KNN is easy to understand, non-parametric, and can handle mixed data types. It suits cases where data is well-clustered or without a clear model assumption.
• Disadvantages: KNN is computationally expensive for large datasets, sensitive to noisy data, and requires careful preprocessing. It can struggle with high-dimensional data and imbalanced datasets.
• Applications: KNN finds applications in classification, regression, clustering, recommendation systems, natural language processing, healthcare, and more.
• Model Interpretability: KNN’s decisions are interpretable individually (e.g., a prediction is based on the majority class of neighbours).
• Trade-offs: When considering KNN, balance its simplicity and interpretability against its limitations regarding efficiency and sensitivity to hyperparameters.

In practice, KNN can be a great starting point for tackling classification and regression problems, providing a baseline to compare with more sophisticated algorithms. However, it’s crucial to understand its strengths and weaknesses, experiment with different settings, and evaluate its performance on your specific data before making it a final choice for your machine learning task.

### Neri Van Otten

Neri Van Otten is the founder of Spot Intelligence, a machine learning engineer with over 12 years of experience specialising in Natural Language Processing (NLP) and deep learning innovation. Dedicated to making your projects succeed.

#### Meet Neri

Neri Van Otten is a machine learning and software engineer with over 12 years of Natural Language Processing (NLP) experience. Dedicated to making your projects succeed.

Popular posts

Connect with us

## Related Articles

### Continual Learning Made Simple, How To Get Started & Top 4 Models

The need for continual learning In the ever-evolving landscape of machine learning and artificial intelligence, the ability to adapt and learn continuously (continual...

### Sequence-to-Sequence Architecture Made Easy & How To Tutorial In Python

What is sequence-to-sequence? Sequence-to-sequence (Seq2Seq) is a deep learning architecture used in natural language processing (NLP) and other sequence modelling...

### Cross-Entropy Loss — Crucial In Machine Learning — Complete Guide & How To Use It

What is cross-entropy loss? Cross-entropy Loss, often called "cross-entropy," is a loss function commonly used in machine learning and deep learning, particularly in...

### Natural Language Generation Explained & 2 How To Tutorials In Python

What is natural language generation? Natural Language Generation (NLG) is a subfield of artificial intelligence (AI) and natural language processing (NLP) that focuses...

### Top 8 Loss Functions Made Simple & How To Implement Them In Python

What are loss functions? Loss functions, also known as a cost or objective functions, are critical component in training machine learning models. It quantifies a...

### How To Implement Cross-lingual Transfer Learning In 5 Different Ways

What is cross-lingual transfer learning? Cross-lingual transfer learning is a machine learning technique that involves transferring knowledge or models from one...

### Text Labelling Made Simple With How To Guide & Tools List

What is text labelling? Text labelling, or text annotation or tagging, assigns labels or categories to text data to make it more understandable and usable for various...

### Language Identification Complete How To Guide In Python [With & Without Libraries]

What is language identification? Language identification is a critical component of Natural Language Processing (NLP), a field dedicated to interacting with computers...

### Multilingual NLP Made Simple — Challenges, Solutions & The Future

Understanding Multilingual NLP In the era of globalization and digital interconnectedness, the ability to understand and process multiple languages is no longer a... 