利用python实现电影推荐

“协同过滤”是推荐系统中的常用技术,按照分析维度的不同可实现“基于用户”和“基于产品”的推荐。

以下是利用python实现电影推荐的具体方法,其中数据集源于《集体编程智慧》一书,后续的编程实现则完全是自己实现的(原书中的实现比较支离、难懂)。

这里我采用的是“基于产品”的推荐方法,因为一般情况下,产品的种类往往较少,而用户的数量往往非常多,“基于产品”的推荐程序可以很好的减小计算量。


其实基本的思想很简单:

首先读入数据,形成用户-电影矩阵,如图所示:矩阵中的数据为用户(横坐标)对特定电影(纵坐标)的评分。

其次根据用户-电影矩阵计算不同电影之间的相关系数(一般用person相关系数),形成电影-电影相关度矩阵。

其次根据电影-电影相关度矩阵,以及用户已有的评分,通过加权平均计算用户未评分电影的预估评分。例如用户对A电影评3分、B电影评4分、C电影未评分,而C电影与A电影、B电影的相关度分别为0.3和0.8,则C电影的预估评分为(0.3*3+0.8*4)/(0.3+0.8)。

最后对于每一位用户,提取其未评分的电影并按预估评分值倒序排列,提取前n位的电影作为推荐电影。



以下为程序源代码,大块的注释还是比较详细的,便于理解各个模块的作用。此外,程序用到了pandas和numpy库,实现起来会比较简洁,因为许多功能如计算相关系数、排序等功能在这些库中已有实现,直接拿来用即可。

import pandas as pd
import numpy as np

#read the data
data={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5},
'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5, 
 'Just My Luck': 1.5, 'The Night Listener': 3.0}, 
'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
 'Superman Returns': 3.5, 'The Night Listener': 4.0},
'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
 'The Night Listener': 4.5, 'You, Me and Dupree': 2.5},
'Mick LaSalle': {'Just My Luck': 2.0, 'Lady in the Water': 3.0,'Superman Returns': 3.0, 'The Night Listener': 3.0, 'You, Me and Dupree': 2.0}, 
'Jack Matthews': {'Snakes on a Plane': 4.0, 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}

#clean&transform the data
data = pd.DataFrame(data)
#0 represents not been rated
data = data.fillna(0)
#each column represents a movie
mdata = data.T

#calculate the simularity of different movies, normalize the data into [0,1]
np.set_printoptions(3)
mcors = np.corrcoef(mdata, rowvar=0)
mcors = 0.5+mcors*0.5
mcors = pd.DataFrame(mcors, columns=mdata.columns, index=mdata.columns)

#calculate the score of every item of every user
#matrix:the user-movie matrix
#mcors:the movie-movie correlation matrix
#item:the movie id
#user:the user id
#score:score of movie for the specific user 
def cal_score(matrix,mcors,item,user):
    totscore = 0
    totsims = 0
    score = 0
    if pd.isnull(matrix[item][user]) or matrix[item][user]==0:
        for mitem in matrix.columns:
            if matrix[mitem][user]==0:
                continue
            else:
                totscore += matrix[mitem][user]*mcors[item][mitem]
                totsims += mcors[item][mitem]
        score = totscore/totsims
    else:
        score = matrix[item][user]
    return score

#calculate the socre matrix
#matrix:the user-movie matrix
#mcors:the movie-movie correlation matrix
#score_matrix:score matrix of movie for different users 
def cal_matscore(matrix,mcors):
    score_matrix = np.zeros(matrix.shape)
    score_matrix = pd.DataFrame(score_matrix, columns=matrix.columns, index=matrix.index)
    for mitem in score_matrix.columns:
        for muser in score_matrix.index:
            score_matrix[mitem][muser]  = cal_score(matrix,mcors,mitem,muser)
    return score_matrix

#give recommendations: depending on the score matrix
#matrix:the user-movie matrix
#score_matrix:score matrix of movie for different users 
#user:the user id
#n:the number of recommendations
def recommend(matrix,score_matrix,user,n):
    user_ratings = matrix.ix[user]
    not_rated_item = user_ratings[user_ratings==0]
    recom_items = {}
    #recom_items={'a':1,'b':7,'c':3}
    for item in not_rated_item.index:
        recom_items[item] = score_matrix[item][user]
    recom_items = pd.Series(recom_items)
    recom_items = recom_items.sort_values(ascending=False)
    return recom_items[:n]  


#main
score_matrix = cal_matscore(mdata,mcors)
for i in range(10):
    user = input(str(i)+' please input the name of user:')
    print recommend(mdata,score_matrix,user,2)



  • 5
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
随着互联网的发展和普及,人们获取信息的方式也在不断变化。尤其是在娱乐休闲领域,电影、音乐等娱乐活动越来越成为人们的生活方式,电影推荐系统逐渐成为电影网站或APP必备的功能。本文将基于Python语言,介绍电影推荐系统的设计与实现。 一、设计 1. 数据采集和处理 在进行电影推荐之前,需要先搜集和处理相关的电影数据,构建一个电影库。一些常见的电影库包括豆瓣、IMDb、MovieLens等。可以使用Python爬虫技术采集电影信息,使用Pandas等库进行数据处理和清洗。 2. 特征提取 对于每一部电影,需要提取相关的特征,以便进行比较和推荐。常见的特征包括电影类型、演员、导演、评分等。可以使用Python的自然语言处理库,如NLTK进行影评情感分析,提取电影的情感因素。 3. 相似度计算 推荐系统本质上是根据电影的相似度或相关度来进行推荐。常用的相似度计算方法包括欧拉距离、余弦相似度等。可以使用Python的科学计算库NumPy进行计算。 4. 推荐算法 根据用户的历史观看记录和评分,可以采用协同过滤、基于内容的推荐算法等多种推荐算法,利用Python的机器学习库Scikit-learn等进行建模和预测。 二、实现 以基于协同过滤电影推荐系统为例,使用Python实现如下步骤: 1. 数据预处理:使用Pandas等库读取和清洗电影数据,去除冗余信息、缺失值。 2. 相似度计算:计算用户历史观看记录和评分的相似度,比较相似用户的电影喜好。 3. 推荐生成:将相似用户观看过的电影推荐给当前用户,按照电影评分的高低排序。 4. 性能优化:如采用推荐缓存、更新策略等,提高推荐系统的实时性和稳定性。 总结 电影推荐系统是一个功能强大,应用广泛的人工智能应用。使用Python等编程语言,可以实现简单、高效、准确的推荐系统,并不断提升用户体验。未来,电影推荐系统将更多地运用到深度学习、自然语言处理等技术领域中,为用户提供更为智能化、人性化的体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值