zoukankan      html  css  js  c++  java
  • ItemCF算法

    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
  • 相关阅读:
    [day002]剑指 Offer 09. 用两个栈实现队列
    [day003]718. 最长重复子数组
    [linux]关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
    96. 不同的二叉搜索树
    91. 解码方法
    [动态规划]64. 最小路径和
    62.不同路径
    【Java】list根据某一条件进行分组
    【Java】批量生成小程序参数码并打包下载
    【Docker】使用docker制作libreoffice镜像并解决中文乱码问题
  • 原文地址:https://www.cnblogs.com/taotao315/p/3130767.html
Copyright © 2011-2022 走看看