题目: 简单关联规则算法例题-9个顾客的编号为(T1,T2,T3,T4,T5,T6,T7,T8,T9),每一个顾客购买的商品记录{{I1,I2,I5},{I2,I4},{I2,I3},{I1,I2,I4},{I1,I3},{I2,I3},{I1,I3},{I1,I2,I3,I5},{I1,I2,I3}},求频繁项集;并用python语言实现
解:
1.理论解答
1.1候选1-项集C1为:
项集数 | 支持度计数 |
---|---|
{I1} | 6 |
{I2} | 7 |
{I3} | 6 |
{I4} | 2 |
{I5} | 2 |
由于最小支持度为2/9=22%,因此最小支持度系数为2,将小于最小支持度系数的去掉,得到1-L1:
1.2 频繁1-项集 1-L1
频繁项集 | 支持度计数 |
---|---|
{I1} | 6 |
{I2} | 7 |
{I3} | 6 |
{I4} | 2 |
{I5} | 2 |
1.3 候选2-项集C2为:
项集数 | 支持度计数 |
---|---|
{I1,I2} | 6 |
{I1,I3} | 7 |
{I1,I4} | 6 |
{I1,I5} | 2 |
{I2,I3} | 2 |
{I2,I4} | 2 |
{I2,I5} | 2 |
{I3,I4} | 0 |
{I3,I5} | 1 |
{I4,I5} | 0 |
将小于最小支持度系数去掉后,得到2-L2:
1.4 频繁2-项集2-L2
频繁项集 | 支持度计数 |
---|---|
{I1,I2} | 6 |
{I1,I3} | 7 |
{I1,I4} | 6 |
{I1,I5} | 2 |
{I2,I3} | 2 |
{I2,I4} | 2 |
{I2,I5} | 2 |
1.5 候选3-项集C3为:
项集数 | 支持度计数 |
---|---|
{I1,I2,I3} | 2 |
{I1,I2,I5} | 2 |
{I1,I3,I5} | 1 |
{I2,I3,I4} | 0 |
{I2,I3,I5} | 1 |
{I2,I4,I5} | 1 |
将小于最小支持度系数去掉后,得到3-L3:
1.6 频繁3-项集3-L3
频繁项集 | 支持度计数 |
---|---|
{I1,I2,I3} | 2 |
{I1,I2,I5} | 2 |
随后进行候选4项集,已经找不到大于等于最小支持度系数的项集,算法结束,最终得到的频繁项集和所对应的支持度计数为:
频繁项集 | 支持度计数 |
---|---|
{I1} | 6 |
{I2} | 7 |
{I3} | 6 |
{I4} | 2 |
{I5} | 2 |
{I1,I2} | 6 |
{I1,I3} | 7 |
{I1,I4} | 6 |
{I1,I5} | 2 |
{I2,I3} | 2 |
{I2,I4} | 2 |
{I2,I5} | 2 |
{I1,I2,I3} | 2 |
{I1,I2,I5} | 2 |
2.python代码实现:
2.1 获取数据集
data_set = [['I1', 'I2', 'I5'], ['I2', 'I4'], ['I2', 'I3'], ['I1', 'I2', 'I4'], ['I1', 'I3'], ['I2', 'I3'],['I1', 'I3'], ['I1', 'I2', 'I3', 'I5'], ['I1', 'I2', 'I3']]
可根据实际情况换成其他数据集。
2.2 定义最小支持度和最小置信度
min_sup = 0.2
min_con = 0.8
2.3. 封装程序需要的各个函数
(1)获取下一个频繁项集
# 获取下一个频繁项集
def get_next_fre_item_set(data_set, fre_item_set, can_item_len, min_sup_num):fre_items = list(fre_item_set.keys())next_fre_item_set = {}for i in range(len(fre_items) - 1):for j in range(i + 1, len(fre_items)):tempi = set()if isinstance(fre_items[i], str):tempi.add(fre_items[i])else:tempi = set(list(fre_items[i]))tempj = set()if isinstance(fre_items[j], str):tempj.add(fre_items[j])else:tempj = set(list(fre_items[j]))tempi.update(tempj)if len(tempi) > can_item_len:continueif tempi in list(set(item) for item in next_fre_item_set.keys()):continuefor record in data_set:if tempi.issubset(set(record)):if tempi in list(set(item) for item in next_fre_item_set.keys()):next_fre_item_set[tuple(tempi)] += 1else:next_fre_item_set[tuple(tempi)] = 1for key in list(next_fre_item_set.keys()):if next_fre_item_set[key] < min_sup_num:del next_fre_item_set[key]if len(list(next_fre_item_set.keys())) < 1:return Noneelse:return next_fre_item_set
(2)获取所有的频繁项集
# 获取频繁项集
def get_fre_item_sets(data_set, min_sup):num_record = len(data_set)min_sup_num = min_sup * num_recordfre_item_sets = []fre_item_sets.append({})# 统计每个元素的频次for record in data_set:for item in record:if item in fre_item_sets[0].keys():fre_item_sets[0][item] += 1else:fre_item_sets[0][item] = 1# 删除低于最小支持度的项for item in list(fre_item_sets[0].keys()):if fre_item_sets[0][item] < min_sup_num:del fre_item_sets[0][item]can_item_len = 2while True:if len(fre_item_sets[can_item_len - 2]) < 2:breakelse:next_fre_item_set = get_next_fre_item_set(data_set, fre_item_sets[can_item_len - 2], can_item_len,min_sup_num)if next_fre_item_set == None:breakelse:fre_item_sets.append(next_fre_item_set)can_item_len += 1return fre_item_sets
(3) 计算置信度
# 计算置信度
def calculate_confidence(fre_item_sets, subset, fre_item):len_mother = len(subset)len_son = len(fre_item)mother_key = Noneson_key = Noneif len_mother == 1:mother_key = subset[0]else:mother_keys = list(fre_item_sets[len_mother - 1].keys())for i in range(len(mother_keys)):if set(subset) == set(mother_keys[i]):mother_key = mother_keys[i]breakson_keys = list(fre_item_sets[len_son - 1].keys())for i in range(len(son_keys)):if set(fre_item) == set(son_keys[i]):son_key = son_keys[i]breakreturn fre_item_sets[len_son - 1][son_key] / fre_item_sets[len_mother - 1][mother_key]
(4)获取关联规则
# 获取关联规则
def get_association_rules(fre_item_sets, min_con):def subsets(itemset):N = len(itemset)subsets = []for i in range(1, 2 ** N - 1):tmp = []for j in range(N):if (i >> j) % 2 == 1:tmp.append(itemset[j])subsets.append(tmp)return subsetsassociation_rules = []for i in range(1, len(fre_item_sets)):fre_item_set = fre_item_sets[i]for fre_item in list(fre_item_set.keys()):tmp = {}all_subsets = subsets(fre_item)for s1 in range(len(all_subsets) - 1):for s2 in range(s1 + 1, len(all_subsets)):subset1 = all_subsets[s1]subset2 = all_subsets[s2]if len(subset1) + len(subset2) == len(fre_item) and len(set(subset1) & set(subset2)) == 0:confidence = calculate_confidence(fre_item_sets, subset1, fre_item)if confidence > min_con:temp = str(subset1) + ' > ' + str(subset2)tmp[temp] = confidenceconfidence = calculate_confidence(fre_item_sets, subset2, fre_item)if confidence > min_con:temp = str(subset2) + ' > ' + str(subset1)tmp[temp] = confidenceif tmp.keys():association_rules.append(tmp)return association_rules
2.4 使用以上函数进行关联规则的提取
(1)获取频繁项集并打印
fre_item_sets = get_fre_item_sets(data_set, min_sup)for i in fre_item_sets:print(i)
打印出的频繁项集如下,字典的value为出现的频次如下图所示:
(2)根据频繁项集获取关联规则
association_rules = get_association_rules(fre_item_sets, min_con)
for i in association_rules:print(i)
打印出的关联规则如下,字典的value为置信度如下图所示: