# python推荐算法实例-用python做推荐系统（一）

f= open(‘u.data’)

user_list={}for line inf:

(user,movie,rating,ts)= line.split(‘ ‘)

user_list.setdefault(user,{})

user_list[user][movie]=float(rating)return user_list

user_list就是我们建立用来分析的名单

defcalculate():

user_diff={}for movies in list[‘7’]:for people inlist.keys():

user_diff.setdefault(people,{})for item inlist[people].keys():if item ==movies:

diff= sqrt(pow(list[‘7’][movies] – list[people][item],2))

user_diff[people][item]=diffreturn user_diff

defpeople_rating():

user_diff=calculate()

rating={}for people inuser_diff.keys():

rating.setdefault(people,{})

a=0

b=0for score inuser_diff[people].values():

a+=score

b+=1rating[people]= float(1/(1+(a/b)))return rating

deftop_list():

list=people_rating()

items=list.items()

top= [[v[1],v[0]] for v initems]

top.sort(reverse=True)print(top[0:5])

list =load_data()for k,v in list[‘7’].items():for kk,vv in list[‘547’].items():if k ==kk:print(k,v,kk,vv)

deffind_rec():

rec_list=top_list()

first= rec_list[1][1]

second= rec_list[2][1]

all_list=load_data()for k,v inall_list[first].items():if k not in all_list[‘7’].keys() and v == 5:print(k)for k,v inall_list[second].items():if k not in all_list[‘7’].keys() and v == 5:print (k)

Original: https://blog.csdn.net/weixin_37988176/article/details/109376935
Author: weixin_37988176
Title: python推荐算法实例-用python做推荐系统（一）

(0)