Code Implementation of building Recommender Systems using KNN

Step 1: Import Libraries

First, we import the necessary libraries. numpy is used for numerical operations, and NearestNeighbors from scikit-learn is used to find the nearest neighbors based on cosine similarity.

Python
import numpy as np
from sklearn.neighbors import NearestNeighbors


Step 2: Create User-Item Interaction Matrix

We create a user-item interaction matrix. This matrix represents the ratings given by users to items, where rows represent users and columns represent items. A value of 0 indicates that the user has not rated the item.

Python
# Example user-item interaction matrix (ratings from 1 to 5, 0 means no rating)
user_item_matrix = np.array([
    [4, 0, 0, 5, 1],
    [5, 5, 4, 0, 0],
    [0, 0, 0, 2, 4],
    [0, 3, 0, 0, 5],
    [5, 0, 4, 0, 0]
])


Step 3: Normalize the Matrix

We normalize the user-item matrix by subtracting the mean rating of each user. This step is important to account for differences in user rating behavior.

Python
# Normalize the matrix by subtracting the mean rating of each user
mean_user_rating = np.mean(user_item_matrix, axis=1).reshape(-1, 1)
normalized_matrix = user_item_matrix - mean_user_rating


Step 4: Fit the KNN Model

We fit the KNN model using the normalized user-item matrix. The metric='cosine' parameter specifies that we use cosine similarity to measure the similarity between users.

Python
# Fit the KNN model
knn = NearestNeighbors(metric='cosine', algorithm='brute')
knn.fit(normalized_matrix)


Step 5: Find Nearest Neighbors

For a target user (e.g., user index 0), we find the k nearest neighbors. In this example, we choose n_neighbors=3

Python
# Find the k nearest neighbors for a target user (e.g., user index 0)
target_user_index = 0
distances, indices = knn.kneighbors(normalized_matrix[target_user_index].reshape(1, -1), n_neighbors=3)


Step 6: Aggregate Ratings from Neighbors

We aggregate the ratings from the nearest neighbors. We average the ratings of the k nearest neighbors to predict the ratings for the target user.

Python
# Aggregate ratings from the nearest neighbors
neighbors_ratings = user_item_matrix[indices.flatten()]
predicted_ratings = neighbors_ratings.mean(axis=0)


Step 7: Recommend Items

We identify items that the target user has not rated (i.e., entries with a value of 0). We then recommend the items with the highest predicted ratings.

Python
# Recommend items with the highest predicted ratings that the target user hasn't rated
unrated_items = np.where(user_item_matrix[target_user_index] == 0)[0]
recommended_items = unrated_items[np.argsort(predicted_ratings[unrated_items])[::-1]]

print(f"Recommended items for user {target_user_index}: {recommended_items}")

Output:

Top 10 similar users to user 196 are:
879
431
672
168
275
744
114
600
358
656

The list provided represents the top 10 users who are most similar to user 196 based on some similarity metric, likely using a collaborative filtering approach

Recommender Systems using KNN

Recommender systems are widely used in various applications, such as e-commerce, entertainment, and social media, to provide personalized recommendations to users. One common approach to building recommender systems is using the K-Nearest Neighbors (KNN) algorithm. This method leverages the similarity between users or items to generate recommendations.

Overview of K-Nearest Neighbors (KNN)

KNN is a simple, non-parametric, and instance-based learning algorithm that can be used for classification and regression tasks. In the context of recommender systems, KNN is used to find the closest neighbours (either users or items) based on a similarity metric. The recommendations are then made based on the preferences of these neighbours.

Types of Recommender Systems

  1. User-Based Collaborative Filtering: This approach recommends items to a user by finding similar users (neighbours) who have similar preferences.
  2. Item-Based Collaborative Filtering: This approach recommends items based on the similarity between items. It finds items similar to those the user has liked in the past.

Similar Reads

Steps to Build a KNN-Based Recommender System

1. Data Collection and Preprocessing...

Code Implementation of building Recommender Systems using KNN

Step 1: Import Libraries...

Advantages and Disadvantages

Advantages:...

Contact Us