From de425af2d7018beaca8ea3093acb7c055bbf920d Mon Sep 17 00:00:00 2001 From: Thinkgamer Date: Fri, 2 Aug 2019 16:33:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?add=20SlopeOne=E7=AE=97=E6=B3=95=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Recommend/SlopeOne.py | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Recommend/SlopeOne.py diff --git a/Recommend/SlopeOne.py b/Recommend/SlopeOne.py new file mode 100644 index 0000000..54dfa5a --- /dev/null +++ b/Recommend/SlopeOne.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- + +""" + Time : 2019-08-02 15:29 + Author : Thinkgamer + File : SlopeOne.py + Software: PyCharm +""" + +class SlopeOne: + def __init__(self): + self.user_rate, self.item_rate = self.loadData() + + # 加载数据 + def loadData(self): + user_rate = { + "U1": {"a": 2, "b": 3, "c": 3, "d": 4}, + "U2": {"b": 4, "c": 2, "d": 3, "e": 3}, + "U3": {"a": 4, "b": 2, "c": 3, "e": 2}, + "U4": {"a": 3, "c": 5, "d": 4, "e": 3} + } + item_rate = { + "a": {"U1": 2, "U3": 4, "U4": 3}, + "b": {"U1": 3, "U2": 4, "U3": 2}, + "c": {"U1": 3, "U2": 2, "U3": 3, "U4": 5}, + "d": {"U1": 4, "U2": 3, "U4": 4}, + "e": {"U2": 3, "U3": 2, "U4": 3} + } + return user_rate,item_rate + + # 计算物品之间的评分偏差 + def cal_item_avg_diff(self): + avgs_dict = {} + for item1 in self.item_rate.keys(): + for item2 in self.item_rate.keys(): + avg = 0.0 + user_count = 0 + if item1 != item2: + for user in self.user_rate.keys(): + user_rate = self.user_rate[user] + if item1 in user_rate.keys() and item2 in user_rate.keys(): + user_count += 1 + avg += user_rate[item1] - user_rate[item2] + avg = avg / user_count + avgs_dict.setdefault(item1,{}) + avgs_dict[item1][item2] = avg + return avgs_dict + + # 计算两个电影的共同评分人数 + def item_both_rate_user(self, item1, item2): + count = 0 + for user in self.user_rate.keys(): + if item1 in self.user_rate[user].keys() and item2 in self.user_rate[user].keys(): + count += 1 + return count + + # 预估评分 + def predict(self, user, item, avgs_dict): + total = 0.0 # 分子 + count = 0 # 分母 + for item1 in self.user_rate[user].keys(): + num = self.item_both_rate_user(item, item1) + count += num + total += num * (self.user_rate[user][item1] - avgs_dict[item][item1]) + return total/count + +if __name__ == "__main__": + slope = SlopeOne() + avgs_dict = slope.cal_item_avg_diff() + result = slope.predict("U2", "a", avgs_dict) + print("U2 对 a的预测评分为: %s" % result) \ No newline at end of file From beb17444b468c12ef80aca893d4b69604743af82 Mon Sep 17 00:00:00 2001 From: Thinkgamer Date: Fri, 2 Aug 2019 16:56:56 +0800 Subject: [PATCH 2/3] readme add slope one --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 699aed8..4296736 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,12 @@ http://blog.csdn.net/gamer_gyt/article/details/51346159
13:基于随机变量的熵来进行数据建模和分析
http://blog.csdn.net/gamer_gyt/article/details/53729868
-14:推荐算法的回顾总结
+14:【推荐系统】推荐算法的回顾总结
http://blog.csdn.net/gamer_gyt/article/details/74367714
+15:【推荐系统】基于协同的Slope One算法原理介绍和实现
+https://blog.csdn.net/Gamer_gyt/article/details/98210975 + -------------------------- # 数据采集样例代码 文件夹为 spider,其内会逐步增加一些爬虫程序和数据,希望对你们有帮助 From 2348d5f0fe5f94396967df86e0ae93ddbce1880d Mon Sep 17 00:00:00 2001 From: thinkgamer Date: Mon, 5 Aug 2019 23:27:12 +0800 Subject: [PATCH 3/3] update readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 4296736..3fbc040 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,9 @@ http://blog.csdn.net/gamer_gyt/article/details/74367714
15:【推荐系统】基于协同的Slope One算法原理介绍和实现
https://blog.csdn.net/Gamer_gyt/article/details/98210975 +16:【推荐系统】基于三部图网络结构的知识推荐算法
+https://blog.csdn.net/Gamer_gyt/article/details/98508579 + -------------------------- # 数据采集样例代码 文件夹为 spider,其内会逐步增加一些爬虫程序和数据,希望对你们有帮助