# coding=utf-8 from collections import defaultdict #使用默认字典,如果查找键不存在,会返回一个默认值 import numpy as np # a = np.random.randint(0, 2, (5, 5)) a = np.array([[0,0,1,1,1], [1,1,0,1,0], [1,0,1,1,0], [0,0,1,1,1], [0,1,0,0,1]]) # print(a) np.savetxt('affinity_dataset.txt', a, fmt='%d') dataset_filename = "affinity_dataset.txt" x = np.loadtxt(dataset_filename) print(x) # 面包。牛奶。奶酪,苹果,香蕉 1 购买, 0 未购买 # 如果顾客购买了苹果,他们也会购买香蕉 就用(3, 4)表示 这条规则的 支持度和置信度 valid_rules = defaultdict(int) # 有效的规则 invalid_rules = defaultdict(int) # 无效规则 num_occurances = defaultdict(int) # 条件相同的规则数量 例:['面包', '牛奶', '奶酪', '苹果', '香蕉'] 总共五组数据,5行5列, 每列代表一种商品, 每种商品中成功购买的次数 #计算过程需要用循环结构依次对每个个体特征值进行处理,第一个特征为规则的前提条件--顾客购买了某一种商品 for sample in x: # sample 样本 1. 循环所有数据集 for premise in range(4): # premise 前提 第一个特征为规则的前提条件--顾客购买了某一种商品 2. 当前数据集里边,刨除最后一个商品 if sample[premise] == 0: # 如果个体不满足条件,即没有买当前商品,继续 3. 如果当前商品没有购买, 那么结束后查看下一个商品状态 continue num_occurances[premise] += 1 # 买了这个商品 则数量 + 1 4. 如果当前商品有购买, 那么 数量加 1 # print(num_occurances[premise]) # 5. 在当前商品是购买状态的情况下,同时还会买后边其他的哪个商品 for conclusion in range(premise, 5): # conclusion 结论 # 当买了这个商品时, 剩下的几种商品中 会同时购买 出现的所有情况次数 if premise == conclusion: # 6 . 比如 当前已买到 苹果, 下一个再买 苹果 的情况, pass掉 continue if sample[conclusion] == 1: # 7. 关联购买的商品 是 购买状态, 比如 苹果 已购买, 同时 香蕉 已购买状态, 说明是有效规则, valid_rules[(premise, conclusion)] += 1 else: # 8. 关联购买的商品 是 购买状态, 比如 苹果 已购买, 同时 香蕉 未购买状态, 说明是无效规则 invalid_rules[(premise, conclusion)] += 1 # print('sample = %s --- premise = %s --- conclusion = %s ' % (sample, premise, conclusion)) # print('valid_rules = ', valid_rules) # print('invalid_rules = ', invalid_rules) # print('num_occurances = ', num_occurances, '\n\n') print(num_occurances) # 计算支持度 support = valid_rules print('suppor = ', support) # 计算置信度,遍历每条规则计算 confidence = defaultdict(float) for premise, conclusion in valid_rules.keys(): rule = (premise, conclusion) confidence[rule] = valid_rules[rule] / num_occurances[premise] # 用符合每一条规则的数量总数/元组中年第一条商品的数量 print('confidence = ', confidence) print('num_occurances = ', num_occurances) features = ['面包', '牛奶', '奶酪', '苹果', '香蕉'] ''' suppor = defaultdict(<class 'int'>, {(0, 1): 1, (1, 3): 1, (2, 3): 3, (0, 3): 2, (3, 4): 2, (2, 4): 2, (1, 4): 1, (0, 2): 1}) confidence = defaultdict(<class 'float'>, {(0, 1): 0.5, (1, 3): 0.5, (2, 3): 1.0, (0, 3): 1.0, (3, 4): 0.5, (2, 4): 0.6666666666666666, (1, 4): 0.5, (0, 2): 0.5}) num_occurances = defaultdict(<class 'int'>, {0: 2, 1: 2, 2: 3, 3: 4}) ''' # 现在我们已经得到了支持度字典和置信度字典,现在定义函数输出每条规则以及支持度和置信度 def show(premise, conclusion, support, confidence, features): premise_name = features[premise] conclusion_name = features[conclusion] print('Rule:如果一个人购买了{0}他将也会买{1}'.format(premise_name, conclusion_name)) print('支持数是{0}'.format(support[(premise, conclusion)])) print('置信度是{0:.3f}'.format(confidence[(premise, conclusion)])) print('\n\n') # print (confidence,type(confidence)) # print(support,type(support)) # if __name__=='__main__': # prem=3 con=4 show(prem,con,support,confidence,features) print(valid_rules) print(invalid_rules) print(num_occurances)
商品推荐案例
猜你喜欢
转载自blog.csdn.net/l1159015838/article/details/81240171
今日推荐
周排行