How to Create a Recommendation System Using Collaborative Filtering


Recommendation systems play an important role in a variety of modern applications, from e-commerce to streaming platforms. Using a recommendation system, users can find products, movies, music, and other items that match their preferences. One popular method for building recommendation systems is collaborative filtering. This article will discuss how to create a simple recommendation system using collaborative filtering in Python.

1. Preparing the Environment

Before starting, make sure Python is installed on your machine. Additionally, you need to install some important libraries:

  • Pandas: For data manipulation and analysis
  • NumPy: For numeric operations
  • Scikit-learn: For machine learning functions

Install this library using the following pip command:
bash

pip install pandas numpy scikit-learn


2. Importing Libraries

The first step in building a recommendation system is to import the necessary libraries:

python

import pandas as pd

import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.metrics.pairwise import cosine_similarity

from sklearn.metrics import mean_squared_error


3. Create and Prepare Data

For this tutorial, we will use the MovieLens dataset, which is a well-known dataset for recommendation systems. This dataset contains data on film ratings by users as well as information about the films themselves. You can download the dataset from MovieLens.
Create a dataset and combine it into one complete data frame:

python

# Create a dataset

ratings = pd.read_csv('path_to_dataset/ratings.csv')

movies = pd.read_csv('path_to_dataset/movies.csv')


# Combine datasets

data = pd.merge(ratings, movies, on='movieId')


The MovieLens dataset consists of two main files: ratings.csv, which includes user rating information for films, and movies.csv, which includes information about the film. After combining the two datasets, we have data that is more complete and ready for further analysis.

4. Create a User-Item Matrix

The next step is to create a matrix where each row represents a user, each column represents a movie, and each cell contains the rating given by the user for that movie:

python

user_item_matrix = data.pivot_table(index='userId', columns='title', values='rating')


This matrix allows us to see the pattern of ratings given by users to films. Next, we can use this matrix to calculate the similarity between users.

5. Calculating Similarity

To provide relevant recommendations, we need to find users who have similar preferences. We will use cosine similarity to measure how similar one user is to another:

python

user_similarity = cosine_similarity(user_item_matrix.fillna(0))

user_similarity_df = pd.DataFrame(user_similarity, index=user_item_matrix.index, columns=user_item_matrix.index)


Cosine similarity measures the similarity between two vectors by comparing the angles between them. A higher similarity value indicates greater similarity between users. This helps us understand user preferences that may not be immediately apparent from the data.

6. Predict Ratings

With calculated user similarities, we can predict ratings for items that have not yet been rated by users. Here is a function to predict ratings based on the preferences of similar users:

python

def predict_ratings(user_item_matrix, user_similarity_df):

    mean_user_rating = user_item_matrix.mean(axis=1)

    ratings_diff = (user_item_matrix.T - mean_user_rating).T

    pred = mean_user_rating[:, np.newaxis] + user_similarity_df.dot(ratings_diff) / np.array([np.abs(user_similarity_df).sum(axis=1)]).T

    return pred


predicted_ratings = predict_ratings(user_item_matrix, user_similarity_df)


This prediction provides an estimated rating for films that have not yet been rated by users based on ratings from similar users. This technique allows the system to make more accurate and relevant recommendations.

7. Evaluate the Model

It is important to evaluate the performance of recommendation systems using metrics such as Root Mean Squared Error (RMSE). This helps us understand how well our predictions compare to the actual rating:

python

def rmse(pred, actual):

    pred = pred[actual.nonzero()].flatten()

    actual = actual[actual.nonzero()].flatten()

    return np.sqrt(mean_squared_error(pred, actual))


actual_ratings = user_item_matrix.values

predicted_ratings = predicted_ratings.values

print(fRMSE: {rmse(predicted_ratings, actual_ratings)}')


RMSE measures the average prediction error, with lower values ​​indicating better model performance. This is an important step to ensure that the recommendation system you build provides accurate and reliable results.

8. Generate Recommendations

Finally, we can use rating predictions to generate movie recommendations for specific users. Here is a function to recommend movies based on predicted ratings:

python

def recommend_movies(user_id, user_item_matrix, predicted_ratings, num_recommendations=5):

    user_ratings = user_item_matrix.loc[user_id].values

    user_predicted_ratings = predicted_ratings[user_id - 1]

    unrated_items = np.where(user_ratings == 0)[0]

    recommendations = [user_item_matrix.columns[i] for i in np.argsort(user_predicted_ratings[unrated_items])[::-1][:num_recommendations]]

    return recommendations


user_id = 1

recommendations = recommend_movies(user_id, user_item_matrix, predicted_ratings)

print(f'Recommended movies for user {user_id}: {recommendations}')


This function identifies the highest rated films that have not yet been rated by users and generates them as recommendations. This allows users to discover new movies they might like based on similar rating patterns from other users.

Implementation and Further Development

Collaborative filtering-based recommendation systems as discussed above can be applied in various applications. For example, Netflix uses a similar method to recommend movies and TV shows to its users. Additionally, Amazon uses a recommendation system to show products that might be of interest to their customers.
To further improve your recommendation system, you can consider some additional techniques:

  • Matrix Factorization: Techniques like Singular Value Decomposition (SVD) can help in reducing the dimensionality of the matrix and reveal deeper patterns in the data.
  • Hybrid Systems: Combining collaborative filtering with content-based filtering can provide more accurate recommendations by considering the features of the item itself.
  • Deep Learning: Using neural networks to learn from more complex data and produce more personalized recommendations.


By exploring these techniques and continually improving your model, you can build recommendation systems that are more effective and relevant to user needs.


Share:

0 Comments:

New Post

Recent Posts

    Support Me