import math from operator import itemgetter data = {'A':{'a','b','d'}, 'B':{'b','c','e'}, 'C':{'c','d'}, 'D':{'b','c','d'}, 'E':{'a','d'}} def ItemSimilarity(data): #calculate co-rated users between itme C = dict() N = dict() for u, items in data.items(): for i in items: if i not in N: N[i] = 1 else : N[i] += 1 if i not in C: C[i] = dict() for j in items: if i == j: continue if j not in C[i]: C[i][j] = 1 else : C[i][j] += 1 ''' for i , k in C.items(): for j, sim in k.items(): print i, j, sim print print '-----------' ''' #calculate final similarity matrix W W = dict() for i, related_items in C.items(): W[i] = dict() for j, cij in related_items.items(): W[i][j] = cij / math.sqrt(N[i] * N[j]) return W Item_Simi = ItemSimilarity(data) #compute similarity between different items for i, item in Item_Simi.items(): for j, Simi in sorted(item.items(), key = itemgetter(1), reverse = True): print 'The similarity between ' + i + ' and ' + j + ' is ', print Simi def Recommendation(data, W, K): rank = dict() ru = data.keys() rui = 1 for k in ru:#user k rank[k] = dict() for i in data[k]:#i is the items user k buyed #print W[i]#the items buyed by k when k buyed i for j, wj in sorted(W[i].items(), key = itemgetter(1), reverse = True)[0:K]: #j is item ranked top K similarest with i buyed by k if j not in rank[k]:#when the interest user k see item j has never computed rank[k][j] = rui * wj else: rank[k][j] += rui * wj return rank result = Recommendation(data,Item_Simi, 3) for i, j_item in result.items(): for j, interest in sorted(j_item.items(), key = itemgetter(1), reverse = True): print ' the interest ' + i + ' buy ' + j +' is ', print interest