Home  >  Article  >  Backend Development  >  Recommendation system examples in Python

Recommendation system examples in Python

WBOY
WBOYOriginal
2023-06-10 08:51:152001browse

Python is currently a very popular programming language. Its powerful library system and easy-to-learn syntax make it the first choice for developing various applications. In the field of machine learning, Python is also a commonly used programming language. This article will introduce an example of using Python to build a recommendation system.

Recommendation systems refer to algorithms and applications that recommend products, content or services to users based on their historical behavior, interests and other information. Recommendation systems are widely used in e-commerce, social media, news reading and other fields. Python provides some libraries and frameworks that can be used to build recommendation systems.

1. Types of recommendation systems

Recommendation systems are mainly divided into two types: content-based recommendations and collaborative filtering recommendations.

Content-based recommendation is to recommend similar products based on the user’s past interests and hobbies and the attribute characteristics of the product. This method requires analyzing the characteristics of the product. You can use the text processing library NLTK or Scikit-learn to process the text information of the product attributes, such as the title and description of the product.

Collaborative filtering recommendation is to discover the relationship between users based on their historical behavioral data and recommend products that they may be interested in. There are two main approaches to collaborative filtering recommendation: user-based collaborative filtering and item-based collaborative filtering.

User-based collaborative filtering makes recommendations based on the similarity between users, that is, finding other users with similar interests to the current user, and recommending products to the current user based on the products these users like. This method requires calculating the similarity between users, and you can use cosine_similarity in Scikit-learn to calculate the cosine similarity.

Item-based collaborative filtering makes recommendations based on the similarity between items, that is, finding similar items to the items that the current user likes and recommending these similar items to the current user. This method requires calculating the similarity between items. You can use pairwise_distances in Scikit-learn to calculate the Euclidean distance or cosine distance.

2. Examples of recommendation systems

Next, we will introduce an example of using Python to build an item-based collaborative filtering recommendation system.

First, we need to prepare the data. We select a movie rating data set, which contains information such as movie ID, user ID, and ratings. We can use the Pandas library to read and process data. The following is a code example:

import pandas as pd
ratings_data = pd.read_csv('ratings.csv')
movies_data = pd.read_csv('movies.csv')

Next, we need to preprocess the data and extract the information we need. We need to map movie IDs to movie names and user IDs to labels. The following is a code example:

# 将电影ID映射为电影名称
movie_names = {}
for index, row in movies_data.iterrows():
    movie_names[row['movieId']] = row['title']

# 将用户ID映射为标号
user_ids = {}
user_counter = 0
for index, row in ratings_data.iterrows():
    user_id = row['userId']
    if user_id not in user_ids:
        user_ids[user_id] = user_counter
        user_counter += 1

Then, we need to build a movie rating matrix. The rows of the matrix represent users, the columns represent movies, and each element in the matrix represents the user's rating of the movie. There are some missing values ​​in the matrix, indicating that the corresponding movie has not been rated by users. We need to fill these missing values ​​using fill method. Here is a code example:

import numpy as np
n_users = len(user_ids)
n_movies = max(movie_names.keys())
rating_matrix = np.zeros((n_users, n_movies))
for index, row in ratings_data.iterrows():
    user_id = row['userId']
    movie_id = row['movieId']
    rating = row['rating']
    rating_matrix[user_ids[user_id], movie_id] = rating

# 使用均值填充缺失值
mean_ratings = np.zeros((n_users,))
for i in range(n_users):
    ratings = rating_matrix[i, :]
    ratings = ratings[ratings > 0]
    mean_ratings[i] = ratings.mean()
    rating_matrix[i, ratings == 0] = mean_ratings[i]

Then, we need to calculate the similarity matrix between movies. We can use the pairwise_distances function in Scikit-learn to calculate the Euclidean distance or cosine distance. Here we choose cosine distance. Here is the code sample:

from sklearn.metrics.pairwise import pairwise_distances
movie_similarity = 1 - pairwise_distances(rating_matrix.T, metric='cosine')

Finally, we need to recommend movies to the user. We can recommend movies based on the user's favorite movies that are similar to them. The following is a code example:

# 找到用户最喜欢的电影
user_id = 0
user_ratings = rating_matrix[user_id, :]
fav_movie = np.argmax(user_ratings)
print('用户 %d 最喜欢的电影是 %s' % (user_id, movie_names[fav_movie]))

# 根据相似度找到相似的电影
similar_movies = movie_similarity[fav_movie, :]
top_k = 5
top_k_movies = np.argsort(similar_movies)[::-1][:top_k]
for i, movie_id in enumerate(top_k_movies):
    print('Top %d 推荐电影是 %s' % (i+1, movie_names[movie_id]))

The above is a Python example of the item-based collaborative filtering recommendation system.

Summary

The recommendation system is a widely used machine learning application. Python provides a wealth of libraries and frameworks that can help developers quickly build recommendation systems. This article introduces an example of how to use Python to build an item-based collaborative filtering recommendation system. I hope it will be helpful to readers.

The above is the detailed content of Recommendation system examples in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn