基于用户购买

基于用户推荐产品

a,b用户之间相似度计算公式=(a,b两用户共同购买东西件数)/(a购买产品件数*b购买产品件数)½

from math import sqrt

user = dict()

user["A"] = {'wash closh','knife','dish'}

user["B"] = {'wash closh','flowers','cup'}

user["C"] = {'wash closh','dish'}

user["D"] = {'flowers','knife'}

# print user

def userSimilarity(users):

items_user = dict()

for u,item in users.items():

for i in item:

if i not in items_user:

items_user[i] =set()

items_user[i].add(u)

# print(items_user)

C=dict()

N=dict()

for i,users in items_user.items():

for u in users:

N.setdefault(u,0)

N[u]+=1

C.setdefault(u,{})

for v in users:

if u==v:

continue

C[u].setdefault(v,0)

C[u][v]+=1

#print('C is',C)

#print('N is',N)

W = dict()

for u,related_users in C.items():

W.setdefault(u,{})

for v,cuv in related_users.items():

W[u].setdefault(v,0)

W[u][v]+=cuv/sqrt(N[u]*N[v])

# print('W is',W)

return W

def recommend(user,trains,W):

rank = dict()

items = trains[user]

for u,wuv in W[user].items():

for i in trains[u]:

if i in items:

continue

rank.setdefault(i,0)

rank[i]+=wuv

return sorted(rank.items(),key = None,reverse = False)

# print userSimilarity(user)

list_NO = ["A","B","C","D"]

for i in list_NO:

print "user {} recomand: {}".format(i,recommend(i,user,userSimilarity(user)))

你可能感兴趣的:(基于用户购买)