基于邻域的推荐算法

User-Based

  1. 用户相似度:
    w u v = ∣ N ( u ) ∩ N ( v ) ∣ ∣ N ( u ) ∪ N ( v ) ∣ w_{uv} = \frac{|N(u) \cap N(v)|}{|N(u) \cup N(v)|} wuv=N(u)N(v)N(u)N(v)
    w u v = ∣ N ( u ) ∩ N ( v ) ∣ ∣ N ( u ) ∣ ∣ N ( v ) ∣ w_{uv} = \frac{|N(u) \cap N(v)|}{\sqrt{|N(u)| |N(v)|}} wuv=N(u)N(v) N(u)N(v)
  2. 建立物品-用户倒排表,转化为用户相似度矩阵:
def UserSimilarity(train):
        # build inverse table for item_users
        item_users = dict()
        for u, items in train.items():
             for i in items.keys():
                 if i not in item_users:
                     item_users[i] = set()
                 item_users[i].add(u)
        #calculate co-rated items between users
        C = dict()
        N = dict()
        for i, users in item_users.items():
             for u in users:
                 N[u] += 1
                 for v in users:
                     if u == v:
                         continue
                     C[u][v] += 1
        #calculate finial similarity matrix W
        W = dict()
        for u, related_users in C.items():
             for v, cuv in related_users.items():
                 W[u][v] = cuv / math.sqrt(N[u] * N[v])
		return W
  1. UserCF下用户 u u u对物品 i i i的感兴趣程度, S ( u , k ) S(u,k) S(u,k)是和用户 u u u相似度最接近的 K K K个用户, N ( i ) N(i) N(i)是对物品 i i i有过行为的用户集合:
    p ( u , i ) = ∑ v ∈ S ( u , K ) ∩ N ( i ) w u v r v i p(u, i) = \sum_{v\in S(u,K) \cap N(i)}w_{uv}r_{vi} p(u,i)=vS(u,K)N(i)wuvrvi
  2. 代码实现:
def Recommend(user, train, W):
    rank = dict()
    interacted_items = train[user]
    for v, wuv in sorted(W[u].items, key=itemgetter(1), \
        reverse=True)[0:K]:
        for i, rvi in train[v].items:
             if i in interacted_items:
                 #we should filter items user interacted before
                 continue
             rank[i] += wuv * rvi
    return rank
  1. 改进用户相似度计算公式:
    w u v = ∣ N ( u ) ∩ N ( v ) ∣ ∣ N ( u ) ∣ ∣ N ( v ) ∣ w_{uv} = \frac{|N(u) \cap N(v)|}{\sqrt{|N(u)| |N(v)|}} wuv=N(u)N(v) N(u)N(v)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值