📜  Python |电影推荐系统的实现

📅  最后修改于: 2021-04-17 02:41:07             🧑  作者: Mango

推荐系统是一种试图根据用户的选择来预测或过滤偏好的系统。推荐系统广泛用于电影,音乐,新闻,书籍,研究文章,搜索查询,社交标签和产品等各个领域。

推荐系统以两种方式中的任何一种产生建议列表:

  • 协作过滤:协作过滤方法根据用户的过去行为(即用户购买或搜索的商品)以及其他用户做出的类似决策来构建模型。然后,此模型用于预测用户可能感兴趣的项目(或项目的评级)。
  • 基于内容的过滤:基于内容的过滤方法使用项目的一系列离散特征,以便推荐具有类似属性的其他项目。基于内容的过滤方法完全基于项目的描述和用户偏好的配置文件。它根据用户过去的偏好来推荐项目。

让我们使用Python和Pandas开发基本的推荐系统。

让我们专注于通过提供与特定项目(在本例中为电影)最相似的项目来提供基本的推荐系统。它只是告诉您哪些电影/项目与用户的电影选择最相似。

要下载文件,请单击链接– .tsv文件Movie_Id_Titles.csv。

导入带有定界符“ \ t”的数据集,因为该文件是tsv文件(制表符分隔的文件)。

# import pandas library
import pandas as pd
  
# Get the data
column_names = ['user_id', 'item_id', 'rating', 'timestamp']
  
path = 'https://media.geeksforgeeks.org/wp-content/uploads/file.tsv'
  
df = pd.read_csv(path, sep='\t', names=column_names)
  
# Check the head of the data
df.head()

# Check out all the movies and their respective IDs
movie_titles = pd.read_csv('https://media.geeksforgeeks.org/wp-content/uploads/Movie_Id_Titles.csv')
movie_titles.head()

data = pd.merge(df, movie_titles, on='item_id')
data.head()

# Calculate mean rating of all movies
data.groupby('title')['rating'].mean().sort_values(ascending=False).head()

# Calculate count rating of all movies
data.groupby('title')['rating'].count().sort_values(ascending=False).head()

# creating dataframe with 'rating' count values
ratings = pd.DataFrame(data.groupby('title')['rating'].mean()) 
  
ratings['num of ratings'] = pd.DataFrame(data.groupby('title')['rating'].count())
  
ratings.head()

4

可视化导入:

import matplotlib.pyplot as plt
import seaborn as sns
  
sns.set_style('white')
%matplotlib inline
# plot graph of 'num of ratings column'
plt.figure(figsize =(10, 4))
  
ratings['num of ratings'].hist(bins = 70)

5

# plot graph of 'ratings' column
plt.figure(figsize =(10, 4))
  
ratings['rating'].hist(bins = 70)

6

# Sorting values according to 
# the 'num of rating column'
moviemat = data.pivot_table(index ='user_id',
              columns ='title', values ='rating')
  
moviemat.head()
  
ratings.sort_values('num of ratings', ascending = False).head(10)

7

# analysing correlation with similar movies
starwars_user_ratings = moviemat['Star Wars (1977)']
liarliar_user_ratings = moviemat['Liar Liar (1997)']
  
starwars_user_ratings.head()

8

# analysing correlation with similar movies
similar_to_starwars = moviemat.corrwith(starwars_user_ratings)
similar_to_liarliar = moviemat.corrwith(liarliar_user_ratings)
  
corr_starwars = pd.DataFrame(similar_to_starwars, columns =['Correlation'])
corr_starwars.dropna(inplace = True)
  
corr_starwars.head()

# Similar movies like starwars
corr_starwars.sort_values('Correlation', ascending = False).head(10)
corr_starwars = corr_starwars.join(ratings['num of ratings'])
  
corr_starwars.head()
  
corr_starwars[corr_starwars['num of ratings']>100].sort_values('Correlation', ascending = False).head()

10

# Similar movies as of liarliar
corr_liarliar = pd.DataFrame(similar_to_liarliar, columns =['Correlation'])
corr_liarliar.dropna(inplace = True)
  
corr_liarliar = corr_liarliar.join(ratings['num of ratings'])
corr_liarliar[corr_liarliar['num of ratings']>100].sort_values('Correlation', ascending = False).head()

11