Home  >  Article  >  Backend Development  >  Build a movie recommendation system using Python

Build a movie recommendation system using Python

WBOY
WBOYforward
2023-04-12 13:13:072152browse

In daily data mining work, in addition to using Python to handle classification or prediction tasks, sometimes it also involves tasks related to recommendation systems.

Recommendation systems are used in various fields, common examples include playlist generators for video and music services, product recommenders for online stores, or content recommenders for social media platforms. In this project, we create a movie recommender.

Collaborative filtering automatically predicts (filters) users' interests by collecting the preferences or taste information of many users. Recommender systems have been developed for a long time so far, and their models are based on various techniques such as weighted average, correlation, machine learning, deep learning, etc.

The Movielens 20M dataset has over 20 million movie ratings and tagging events since 1995. In this article, we will retrieve information from movie.csv & rating.csv files. Use Python libraries: Pandas, Seaborn, Scikit-learn and SciPy to train the model using cosine similarity in the k-nearest neighbor algorithm.

The following are the core steps of the project:

  1. Import and merge datasets and create Pandas DataFrame
  2. Add necessary features to analyze the data
  3. Use Seaborn to visualize and analyze data
  4. Filter invalid data by setting thresholds
  5. Create a pivot table with users as the index and movies as the columns
  6. Create a KNN model And output 5 recommendations similar to each movie

Import data

Import and merge datasets and create Pandas DataFrame

MovieLens 20M dataset since 1995 Over 20 million movie ratings and tagging activities since.

# usecols 允许选择自己选择的特征,并通过dtype设定对应类型
movies_df=pd.read_csv('movies.csv', 
usecols=['movieId','title'], 
dtype={'movieId':'int32','title':'str'})
movies_df.head()

Build a movie recommendation system using Python

ratings_df=pd.read_csv('ratings.csv',
 usecols=['userId', 'movieId', 'rating','timestamp'],
 dtype={'userId': 'int32', 'movieId': 'int32', 'rating': 'float32'})
ratings_df.head()

Build a movie recommendation system using Python

Check if there are any null values ​​and the number of entries in both data.

# 检查缺失值
movies_df.isnull().sum()

movieId 0

title 0

dtype: int64

ratings_df.isnull().sum()

userId 0

movieId 0

rating 0

timestamp 0

dtype: int64

print("Movies:",movies_df.shape)
print("Ratings:",ratings_df.shape)

Movies: (9742, 2)

Ratings: (100836, 4)

Merge dataframe on column 'movieId'

# movies_df.info()
# ratings_df.info()
movies_merged_df=movies_df.merge(ratings_df, on='movieId')
movies_merged_df.head()

Build a movie recommendation system using Python

The imported datasets have now been merged successfully.

Add Derived Features

Add necessary features to analyze the data.

Create 'Average Rating' & 'Rating Count' columns by grouping user ratings by movie title.

movies_average_rating=movies_merged_df.groupby('title')['rating']
 .mean().sort_values(ascending=False)
.reset_index().rename(columns={'rating':'Average Rating'})
movies_average_rating.head()

Build a movie recommendation system using Python

movies_rating_count=movies_merged_df.groupby('title')['rating']
.count().sort_values(ascending=True)
 .reset_index().rename(columns={'rating':'Rating Count'}) #ascending=False
movies_rating_count_avg=movies_rating_count.merge(movies_average_rating, on='title')
movies_rating_count_avg.head()

Build a movie recommendation system using Python

Currently 2 new derived features have been created.

Data Visualization

Using Seaborn to visualize data:

  • After analysis, it was found that many movies have perfect 5 stars on a data set rated by nearly 100,000 users Average rating. This indicates the presence of outliers, which we need to further confirm through visualization.
  • The ratings of many movies are relatively single. It is recommended to set a rating threshold in order to generate valuable recommendations.

Use seaborn & matplotlib to visualize data to better observe and analyze the data.

Plot a histogram of the newly created features and view their distribution. Set the bin size to 80. The setting of this value requires detailed analysis and reasonable setting.

# 导入可视化库
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(font_scale = 1)
plt.rcParams["axes.grid"] = False
plt.style.use('dark_background')
%matplotlib inline

# 绘制图形
plt.figure(figsize=(12,4))
plt.hist(movies_rating_count_avg['Rating Count'],bins=80,color='tab:purple')
plt.ylabel('Ratings Count(Scaled)', fontsize=16)
plt.savefig('ratingcounthist.jpg')

plt.figure(figsize=(12,4))
plt.hist(movies_rating_count_avg['Average Rating'],bins=80,color='tab:purple')
plt.ylabel('Average Rating',fontsize=16)
plt.savefig('avgratinghist.jpg')

Build a movie recommendation system using Python

Figure 1 Average Rating Histogram

Build a movie recommendation system using Python

Figure 2 Rating Count Histogram

Now create a joinplot 2D chart to visualize these two features together.

plot=sns.jointplot(x='Average Rating',
 y='Rating Count',
 data=movies_rating_count_avg,
 alpha=0.5, 
 color='tab:pink')
plot.savefig('joinplot.jpg')

Build a movie recommendation system using Python

Two-dimensional graph of Average Rating and Rating Count

分析

  • 图1证实了,大部分电影的评分都是较低的。除了设置阈值之外,我们还可以在这个用例中使用一些更高百分比的分位数。
  • 直方图 2 展示了“Average Rating”的分布函数。

数据清洗

运用describe()函数得到数据集的描述统计值,如分位数和标准差等。

pd.set_option('display.float_format', lambda x: '%.3f' % x)
print(rating_with_RatingCount['Rating Count'].describe())
count 100836.000
mean58.759
std 61.965
min1.000
25% 13.000
50% 39.000
75% 84.000
max329.000
Name: Rating Count, dtype: float64

设置阈值并筛选出高于阈值的数据。

popularity_threshold = 50
popular_movies= rating_with_RatingCount[
rating_with_RatingCount['Rating Count']>=popularity_threshold]
popular_movies.head()
# popular_movies.shape

Build a movie recommendation system using Python

至此已经通过过滤掉了评论低于阈值的电影来清洗数据。

创建数据透视表

创建一个以用户为索引、以电影为列的数据透视表

为了稍后将数据加载到模型中,需要创建一个数据透视表。并设置'title'作为索引,'userId'为列,'rating'为值。

import os
movie_features_df=popular_movies.pivot_table(
index='title',columns='userId',values='rating').fillna(0)
movie_features_df.head()
movie_features_df.to_excel('output.xlsx')

Build a movie recommendation system using Python

接下来将创建的数据透视表加载到模型。

建立 kNN 模型

建立 kNN 模型并输出与每部电影相似的 5 个推荐

使用scipy.sparse模块中的csr_matrix方法,将数据透视表转换为用于拟合模型的数组矩阵。

from scipy.sparse import csr_matrix
movie_features_df_matrix = csr_matrix(movie_features_df.values)

最后,使用之前生成的矩阵数据,来训练来自sklearn中的NearestNeighbors算法。并设置参数:metric = 'cosine', algorithm = 'brute'

from sklearn.neighbors import NearestNeighbors
model_knn = NearestNeighbors(metric = 'cosine',
 algorithm = 'brute')
model_knn.fit(movie_features_df_matrix)

现在向模型传递一个索引,根据'kneighbors'算法要求,需要将数据转换为单行数组,并设置n_neighbors的值。

query_index = np.random.choice(movie_features_df.shape[0])
distances, indices = model_knn.kneighbors(movie_features_df.iloc[query_index,:].values.reshape(1, -1),
n_neighbors = 6)

最后在 query_index 中输出出电影推荐。

for i in range(0, len(distances.flatten())):
if i == 0:
print('Recommendations for {0}:n'
.format(movie_features_df.index[query_index]))
else:
print('{0}: {1}, with distance of {2}:'
.format(i, movie_features_df.index[indices.flatten()[i]],
distances.flatten()[i]))
Recommendations for Harry Potter and the Order of the Phoenix (2007):

1: Harry Potter and the Half-Blood Prince (2009), with distance of 0.2346513867378235:
2: Harry Potter and the Order of the Phoenix (2007), with distance of 0.3396233320236206:
3: Harry Potter and the Goblet of Fire (2005), with distance of 0.4170845150947571:
4: Harry Potter and the Prisoner of Azkaban (2004), with distance of 0.4499547481536865:
5: Harry Potter and the Chamber of Secrets (2002), with distance of 0.4506162405014038:

至此我们已经能够成功构建了一个仅基于用户评分的推荐引擎。

总结

以下是我们构建电影推荐系统的步骤摘要:

  1. 导入和合并数据集并创建 Pandas DataFrame
  2. 为了更好分析数据创建衍生变量
  3. 使用 Seaborn 可视化数据
  4. 通过设置阈值来清洗数据
  5. 创建了一个以用户为索引、以电影为列的数据透视表
  6. 建立一个 kNN 模型,并输出 5 个与每部电影最相似的推荐

写在最后

以下是可以扩展项目的一些方法:

  • 这个数据集不是很大,可以在项目中的包含数据集中的其他文件来扩展这个项目的范围。
  • 可以利用' ratings.csv' 中时间戳,分析评级在一段时间内的变化情况,并且可以在解析我们的模型时,根据时间戳对评级进行加权。
  • 该模型的性能远优于加权平均或相关模型,但仍有提升的空间,如使用高级 ML 算法甚至 DL 模型。


The above is the detailed content of Build a movie recommendation system using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete