python实现ItemCF算法

毕设要用到推荐算法,于是今天在在网上查找资料,找到这位仁兄的资料https://blog.csdn.net/Gamer_gyt/article/details/51346159

#!/usr/sbin/env python
# -*- coding:utf-8 -*-

import math
# ItemCF算法
def ItemSimilarity(train):
    # 物品-物品的共同矩阵
    C = dict()
    # 物品被多少个不同用户购买
    N = dict()
    for u, items in train.items():
        for i in items.keys():
            N.setdefault(i, 0)
            N[i] += 1
            C.setdefault(i, {})
            for j in items.keys():
                if i == j:
                    continue
                C[i].setdefault(j, 0)
                C[i][j] += 1
    # 计算相似度矩阵
    W = dict()
    for i, related_items in C.items():
        W.setdefault(i, {})
        for j, cij in related_items.items():
            W[i][j] = cij / math.sqrt(N[i] * N[j])
    return W

# 推荐前K个用户
def Recommend(train, user_id, W, K):
    rank = dict()
    action_item = train[user_id]
    for item, score in action_item.items():
        for j, wj in sorted(W[item].items(), key=lambda x:x[1], reverse=True)[0:K]:
            if j in action_item.keys():
                continue
            rank.setdefault(j, 0)
            rank[j] += score * wj
    print rank.items()
    return sorted(rank.items(), key=lambda x:x[1], reverse=True)

train = dict()
for line in open('requests.txt', 'r'):
    user, item, score = line.strip().split(",")
    train.setdefault(user, {})
    train[user][item] = float(score)
W = ItemSimilarity(train)
result = Recommend(train, '5', W, 3)
print result

requests.txt

1,101,5.0
1,102,3.0
1,103,2.5
2,101,2.0
2,102,2.5
2,103,5.0
2,104,2.0
3,101,2.0
3,104,4.0
3,105,4.5
3,107,5.0
4,101,5.0
4,103,3.0
4,104,4.5
4,106,4.0
5,101,4.0
5,102,3.0
5,103,2.0
5,104,4.0
5,105,3.5
5,106,4.0

将数据整理到excel表中


最后验证输出结果为:

分别对1,2,3,4,5用户进行验证,发现可以推荐出最适合的,完成。


最后还是感谢这位仁兄了https://blog.csdn.net/Gamer_gyt/article/details/51346159

初次研究算法这方面,有不足之处还请指导。

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值