# 推薦系統實踐--基於用戶的協同過濾算法

http://www.cnblogs.com/qwj-sysu/p/4368874.html

## 基於用戶的協同過濾算法

(1) 找到和目標用戶興趣相似的用戶集合。

(2) 找到這個集合中的用戶喜歡的，且目標用戶沒有聽說過的物品推薦給目標用戶。

jaccard                                                                             余項公式：

`def UserSimilarity(train):    W = dict()    for u in train.keys():        for v in train.keys():            if u == v:                continue            W[u][v] = len(train[u] & train[v])            W[u][v] = /= math.sqrt(len(train[u]) * len(train[v]) * 1.0)    return W`

`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`

`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`

## 用戶相似度計算的改進

`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 / math.log(1   len(users))    #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`

0条回复