Recommendation System in Python

Recommendation systems are tools in Python that suggest items or content to users based on their preferences and past behaviors. This technology utilizes algorithms to predict users' future preferences, thereby providing them with the most relevant content.

The scope of this system is vast, with widespread use in various industries such as e-commerce, streaming services, and social media. Products, movies, music, books, and more can all be recommended through these systems. The provision of personalized recommendations not only helps foster customer engagement and loyalty but can also boost sales.

Types of Recommendation Systems

Content-Based Recommendation Systems

These operate on the notion that users can receive recommendations for items comparable to those they have previously engaged with. This kind of system utilizes algorithms to pinpoint items that closely resemble a user's preferences, with the objective of creating a list of suggestions tailored to the user.

Algorithm

  • Step 1 Import necessary libraries

  • Step 2 Load the dataset

  • Step 3 Preprocess data

  • Step 4 Compute the similarity matrix

  • Step 5 For each user

    • Select items they have interacted with

    • For each item selected in step 5a

      • Retrieve its similarity scores with all other items

      • Compute a weighted average of the similarity scores, using the user's ratings as weights

    • Sort items in descending order based on their weighted similarity scores

    • Recommend the top N items to the user

  • Step 6 Return recommendations for all users.

Example

Here's a content-based movie recommendation system using movie descriptions ?

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# Create sample movie data
movies_data = {
    'title': ['The Godfather', 'Scarface', 'Casino', 'Goodfellas', 'Pulp Fiction', 'The Matrix'],
    'description': [
        'crime family drama mafia don',
        'crime drug dealer miami gangster',
        'gambling mafia casino las vegas',
        'organized crime gangster mafia',
        'crime pulp fiction violence',
        'science fiction virtual reality action'
    ]
}
data = pd.DataFrame(movies_data)

# Compute TF-IDF vectors for each movie
tfidf = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf.fit_transform(data['description'])

# Compute cosine similarity between all movies
cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix)

# Function to get top 3 similar movies based on input movie
def get_recommendations(title):
    idx = data[data['title'] == title].index[0]
    sim_scores = list(enumerate(cosine_sim[idx]))
    sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
    sim_scores = sim_scores[1:4]  # Top 3 similar movies
    movie_indices = [i[0] for i in sim_scores]
    return data.iloc[movie_indices]['title'].tolist()

# Example usage: get movies similar to 'The Godfather'
recommendations = get_recommendations('The Godfather')
print("Movies similar to 'The Godfather':")
for movie in recommendations:
    print("", movie)
Movies similar to 'The Godfather':
 Goodfellas
 Casino
 Scarface

Collaborative Filtering Recommendation Systems

These rely on data from other users in order to produce recommendations. This variety of systems compares the preferences and behaviors of various users and then suggests items that other users who possess analogous tastes might fancy. Collaborative filtering is commonly more precise compared to content-based systems since it factors in numerous user opinions when producing recommendations.

Algorithm

  • Step 1 Import necessary libraries

  • Step 2 Load ratings data

  • Step 3 Create user-item matrix

  • Step 4 Calculate similarity of user ratings using cosine similarity

  • Step 5 Identify similar users

  • Step 6 Calculate average rating

  • Step 7 Generate recommendations

Example

Here's a collaborative filtering system using user ratings ?

import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

# Create sample ratings data
ratings_data = {
    'userId': [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4],
    'movieId': [1, 2, 3, 1, 2, 4, 2, 3, 4, 1, 3, 4],
    'rating': [5, 3, 4, 4, 5, 2, 5, 4, 3, 3, 5, 4]
}
ratings_df = pd.DataFrame(ratings_data)

# Create user-item matrix
user_item_matrix = pd.pivot_table(ratings_df, values='rating', index='userId', columns='movieId', fill_value=0)
print("User-Item Matrix:")
print(user_item_matrix)

# Calculate cosine similarity between users
user_similarity = cosine_similarity(user_item_matrix)

# Get recommendations for a specific user
def get_collaborative_recommendations(user_id, user_item_matrix, user_similarity, n=2):
    user_idx = user_id - 1  # Convert to 0-based index
    similar_users = user_similarity[user_idx].argsort()[::-1][1:]  # Exclude self
    
    # Get movies not rated by target user
    user_ratings = user_item_matrix.iloc[user_idx]
    unrated_movies = user_ratings[user_ratings == 0].index.tolist()
    
    # Calculate weighted ratings for unrated movies
    recommendations = {}
    for movie in unrated_movies:
        weighted_sum = 0
        similarity_sum = 0
        for similar_user_idx in similar_users:
            if user_item_matrix.iloc[similar_user_idx][movie] > 0:
                weighted_sum += user_similarity[user_idx][similar_user_idx] * user_item_matrix.iloc[similar_user_idx][movie]
                similarity_sum += user_similarity[user_idx][similar_user_idx]
        
        if similarity_sum > 0:
            recommendations[movie] = weighted_sum / similarity_sum
    
    # Sort and return top n recommendations
    sorted_recommendations = sorted(recommendations.items(), key=lambda x: x[1], reverse=True)[:n]
    return sorted_recommendations

# Get recommendations for user 1
user_id = 1
recommendations = get_collaborative_recommendations(user_id, user_item_matrix, user_similarity)
print(f"\nRecommendations for user {user_id}:")
for movie_id, predicted_rating in recommendations:
    print(f"Movie {movie_id}: {predicted_rating:.2f}")
User-Item Matrix:
movieId  1  2  3  4
userId            
1        5  3  4  0
2        4  5  0  2
3        0  5  4  3
4        3  0  5  4

Recommendations for user 1:
Movie 4: 2.89

Comparison of Recommendation Systems

Type Data Required Advantages Disadvantages
Content-Based Item features No cold start for items Limited diversity
Collaborative Filtering User ratings Better accuracy Cold start problem

Conclusion

Recommendation systems are powerful tools that enhance user experience through personalized suggestions. Content-based systems work well when item features are available, while collaborative filtering excels with sufficient user interaction data. The choice between approaches depends on your specific use case and available data.

Updated on: 2026-03-27T11:21:42+05:30

795 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements