利用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)
利用python实现电影推荐的更多相关文章
- 转利用python实现电影推荐
“协同过滤”是推荐系统中的常用技术,按照分析维度的不同可实现“基于用户”和“基于产品”的推荐. 以下是利用python实现电影推荐的具体方法,其中数据集源于<集体编程智慧>一书,后续的编程 ...
- 利用Surprise包进行电影推荐
Surprise(Simple Python Recommendation System Engine)是一款推荐系统库,是scikit系列中的一个.简单易用,同时支持多种推荐算法(基础算法.协同过滤 ...
- 【大数据 Spark】利用电影观看记录数据,进行电影推荐
利用电影观看记录数据,进行电影推荐. 目录 利用电影观看记录数据,进行电影推荐. 准备 1.任务描述: 2.数据下载 3.部分数据展示 实操 1.设置输入输出路径 2.配置spark 3.读取Rati ...
- 基于物品的协同过滤item-CF 之电影推荐 python
推荐算法有基于协同的Collaboration Filtering:包括 user Based和item Based:基于内容 : Content Based 协同过滤包括基于物品的协同过滤和基于用户 ...
- Python实现个性化推荐二
基于内容的推荐引擎是怎么工作的 基于内容的推荐系统,正如你的朋友和同事预期的那样,会考虑商品的实际属性,比如商品描述,商品名,价格等等.如果你以前从没接触过推荐系统,然后现在有人拿枪指着你的头,强迫你 ...
- 利用Python,四步掌握机器学习
为了理解和应用机器学习技术,你需要学习 Python 或者 R.这两者都是与 C.Java.PHP 相类似的编程语言.但是,因为 Python 与 R 都比较年轻,而且更加“远离”CPU,所以它们显得 ...
- 利用python 掌握机器学习的过程
转载:http://python.jobbole.com/84326/ 偶然看到的这篇文章,觉得对我挺有引导作用的.特此跟大家分享一下. 为了理解和应用机器学习技术,你需要学习 Python 或者 R ...
- 利用python进行数据分析--(阅读笔记一)
以此记录阅读和学习<利用Python进行数据分析>这本书中的觉得重要的点! 第一章:准备工作 1.一组新闻文章可以被处理为一张词频表,这张词频表可以用于情感分析. 2.大多数软件是由两部分 ...
- 《利用python进行数据分析》读书笔记 --第一、二章 准备与例子
http://www.cnblogs.com/batteryhp/p/4868348.html 第一章 准备工作 今天开始码这本书--<利用python进行数据分析>.R和python都得 ...
随机推荐
- 对偶图 并查集 BZOJ4423
题目链接 题目因为要根据上一次的输出结果来判断这次的输入,也就是要求我们强制在线,不能够把输入全部储存后处理 如果不要求强制在线,我们可以先把所以输入储存起来,从最后开始处理,把删边改成加边,如果在加 ...
- 数组的typedef 和函数的typedef
#include<stdio.h> #include<string.h> #include<stdlib.h> // 数组指针 语法 梳理 // //int a[1 ...
- centos命令行系列之centos查看磁盘空间大小
df -h 扩展: 1.查看当前文件夹所有文件大小 du -sh 2.查看指定文件下所有文件大小 du -h /data/ 3.查看指定文件大小 du -h install.log 4.查指定文件夹大 ...
- spring-boot 全面认知
https://blog.csdn.net/ityouknow/article/details/80490926,这个链接非常全面的讲解了spring-boot的内容,从初级到中级,到最后的提升,觉得 ...
- [codechef July Challenge 2017] IPC Trainers
IPCTRAIN: 训练营教练题目描述本次印度编程训练营(Indian Programming Camp,IPC)共请到了 N 名教练.训练营的日程安排有 M 天,每天最多上一节课.第 i 名教练在第 ...
- MySQL中的文件
查看数据目录: select @@datadir; 共享表空间: ibdata1 Redo log file:ib_logfile0, ib_logfile1 二进制日志:需要配置参数 server- ...
- mysql索引简单分析
索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将对整个表进 ...
- Qt与JS(三)
Qt不错的学习网址: http://www.cnblogs.com/findumars/p/5529526.html ----------------------------------------- ...
- css 让div 的高度和屏幕的高度一样
<html><head><title>无标题文档</title><style type="text/css">html, ...
- js里面判断一个字符串是否包含某个子串的方法
1. ES6的includes, 返回 Boolean var string = "foo", substring = "oo"; string.include ...