K nearest neighbour
User based nearest neighbour
Machine learning
Collaborative filtering
Recommendation systems

K nearest neighbour vs User based nearest neighbour

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

K nearest neighbour (KNN) and User-based nearest neighbour (User-based NN) are well-known approaches in data science and machine learning, both revolving around the concept of "neighbourhood" but applied in different contexts, serving distinct purposes. This article dives into these two methods, comparing their functionalities, applications, and providing a comprehensive technical overview.

K Nearest Neighbour (KNN)

Overview

KNN is a simple, non-parametric, and lazy learning algorithm used for classification and regression. Its primary purpose is to predict the classification or continuous value of a data point by considering the 'K' closest data points in the feature space.

Technical Explanation

Algorithm Steps:

  1. Choose the number of 'K' neighbours: The algorithm selects 'K' closest neighbours.
  2. Compute Distance: Typically Euclidean distance in the feature space is calculated between the query instance and all the training samples.
  3. Determine the Nearest Neighbours: Sort the sample points based on their distance and select the first 'K' items.
  4. Vote for a Majority (for Classification): The class label is predicted by having neighbours vote for it.
  5. Average Neighbours (for Regression): The continuous value prediction is obtained by averaging the values of 'K' nearest neighbours.

Distance Metric: • Euclidean Distance: d(i,j)=n=1N(xi,nxj,n)2d(i, j) = \sqrt{\sum_{n=1}^{N} (x_{i,n} - x_{j,n})^2}Manhattan Distance: d(i,j)=n=1Nxi,nxj,nd(i, j) = \sum_{n=1}^{N} |x_{i,n} - x_{j,n}| • Other metrics include Minkowski, Hamming, etc.

Applications

Classification: Handwritten digit recognition. • Regression: Housing price prediction based on features like size, location, etc. • Anomaly Detection: Detecting outliers in data.

Pros and Cons

Pros:

• Simple and easy to implement. • No prior model training phase. • Flexible to feature and distance choices.

Cons:

• Computationally expensive as it requires computing distance to all points. • Poor performance on high-dimensional data (curse of dimensionality). • Determines the value of 'K' can be tricky.

User-Based Nearest Neighbour

Overview

User-based NN is specifically applied in collaborative filtering recommender systems. It focuses on finding similar users instead of data instances (as in KNN) based on user behaviour, and it recommends items enjoyed by similar users.

Technical Explanation

Algorithm Steps:

  1. User Similarity Calculation: Identify similarity between users using metrics like Pearson correlation or cosine similarity.
  2. Neighbourhood Formation: Select the 'K' most similar users.
  3. Aggregation: Aggregate ratings of the selected neighbours to predict the target user's rating for an item.
  4. Recommendation: Recommend items with the highest predicted ratings to the user.

Similarity Metrics: • Pearson Correlation Coefficient: Measures the linear correlation between two users. • Cosine Similarity: Measures the cosine of the angle between two preference vectors.

Applications

Movie Recommendations: Suggesting films to a user based on viewing behavior of other similar users. • E-commerce: Product recommendations based on purchase history of like-minded users. • Music Streaming: Music suggestions using listening behavior of similar users.

Pros and Cons

Pros:

• Personalized recommendations. • No need for deep user profile information, only user-item interaction data.

Cons:

• Cold start problem for new users with limited or no ratings. • Scalability issues with large datasets.

Comparison Table

FeatureK Nearest NeighbourUser-Based Nearest Neighbour
PurposeClassification/RegressionRecommender Systems
Learning TypeInstance-based / Lazy LearningCollaborative Filtering
Neighbourhood BasisFeature SimilarityUser Behaviour Similarity
Data RequirementRequires feature vector for each sampleUser-item interaction data
ScalabilityComputationally expensive on large datasetsFaces scalability issues with large user base
Cold Start ProblemNoYes
Distance/Similarity MetricEuclidean, ManhattanPearson, Cosine

Conclusion

Both KNN and User-based NN leverage the concept of "neighbours" but serve very different goals in machine learning. While KNN is a versatile tool applicable in diverse domains including classification and regression tasks, User-based NN shines in personalized recommendation systems. The choice between these methods hinges on the specific application domain, the nature of the data, and computational considerations.

Understanding these nuances allows data scientists and engineers to effectively implement and adapt these techniques for optimal performance in their respective fields.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.