import math from operator import itemgetter data = {'A': {'a','b','d'}, 'B': {'a','c'}, 'C': {'b','e'}, 'D': {'c','d','e'}} def UserSimilarity(data):#calculate similarity between diffenenr users item_users = dict() for u,items in data.items(): for i in items: if i not in item_users:#if i not appears in the item_users set create a new item set item_users[i] = set() item_users[i].add(u) #add u into the item_users set #caculate co-rated items between users C = dict() for u in data.keys():#Create and initialize C to be a nest dict with value 0 C[u] = dict() for v in data.keys(): C[u][v] = 0 #print C N = dict() for i, users in item_users.items(): #print users for u in users: if u not in N.keys():#u is not in the key of N then set N[u]=1 N[u] = 1 else : N[u] += 1 #the num of item user u liked for v in users: if u == v: continue C[u][v] += 1 #print u + ' -> ' + v + ' add 1' #print C #print #calculate final similarity matirx W W = dict() for u , related_users in C.items(): W[u] = dict() for v, cur in related_users.items(): W[u][v] = C[u][v] / math.sqrt(N[u] * N[v]) return W def Recommend(data, W):#recommend algorithms rank = dict()#store the recommend result interacted_items = data#all items of users buy W_values = W.values() for u in W.keys():#ergodic all users #print W[u] rank[u] = dict() for v, wuv in sorted(W[u].items(), key = itemgetter(1), reverse = True)[0:3]: #sort the sequence of similarity between users u and v #print u,v, wuv rvi = 1 ''' for item_v in data[v]:#ergodic items user v buy if item_v in interacted_items[u]:#if user u also buyed item rvi (buyed by user v) continue rvi = 1 ''' for item_v in data[v]: if item_v in interacted_items[u]: continue #print v,item_v if item_v in rank[u]: rank[u][item_v] += wuv * rvi else: rank[u][item_v] = wuv * rvi #print u,v,rank[u][v] return rank result = UserSimilarity(data) '''for u, temp in result.items(): for v ,finaResult in temp.items(): print 'similarity between ' + u + ' and ' + v + ' is : ', print result[u][v] ''' recommend = Recommend(data, result) for i, temp in recommend.items(): for j , final in temp.items(): print 'The degree ' + i + ' interested in ' + j + ' is: ', print recommend[i][j]