def generate_big_rules(L, support_data, min_conf): """ Generate big rules from frequent itemsets. Args: L: The list of Lk. support_data: A dictionary. The key is frequent itemset and the value is support. min_conf: Minimal confidence. Returns: big_rule_list: A list which contains all big rules. Each big rule is represented as a 3-tuple. """ big_rule_list = [] sub_set_list = [] for i in range(0, len(L)): for freq_set in L[i]: for sub_set in sub_set_list: if sub_set.issubset(freq_set): conf = support_data[freq_set] / support_data[freq_set - sub_set] big_rule = (freq_set - sub_set, sub_set, conf) if conf >= min_conf and big_rule not in big_rule_list: # print freq_set-sub_set, " => ", sub_set, "conf: ", conf big_rule_list.append(big_rule) sub_set_list.append(freq_set) return big_rule_list
时间: 2024-02-15 08:11:35 浏览: 32
这段代码实现了Apriori算法中的关联规则生成过程。在频繁项集挖掘结束后,可以根据频繁项集生成关联规则,并计算关联规则的置信度,筛选出满足最小置信度要求的强关联规则。
函数generate_big_rules的输入参数包括频繁项集列表L、支持度字典support_data和最小置信度min_conf。其中,频繁项集列表L是一个包含多个列表的列表,每个列表包含若干个频繁项集;支持度字典support_data是一个字典,存储频繁项集及其支持度信息;最小置信度min_conf是指一个关联规则的置信度必须大于等于min_conf才能被认为是强关联规则。
函数内部首先创建了一个空列表big_rule_list,用于存储所有满足要求的强关联规则。接着,创建了一个空列表sub_set_list,用于存储已经检查过的子集,避免重复计算。
通过两个嵌套的循环,对每个频繁项集及其子集进行遍历,计算置信度,并将满足要求的关联规则存储在big_rule_list中。具体来说,外层循环遍历每个频繁项集,内层循环遍历该频繁项集的所有子集。对于每个子集,计算该关联规则的置信度,如果大于等于min_conf并且该关联规则不在big_rule_list中,则将该关联规则加入big_rule_list中。
最后,函数generate_big_rules返回所有满足要求的强关联规则列表big_rule_list。每个关联规则被表示为一个3元组,包括规则的前件、后件以及置信度。
相关问题
def generate_L(data_set, k, min_support): """ Generate all frequent itemsets. Args: data_set: A list of transactions. Each transaction contains several items. k: Maximum number of items for all frequent itemsets. min_support: The minimum support. Returns: L: The list of Lk. support_data: A dictionary. The key is frequent itemset and the value is support. """ support_data = {} C1 = create_C1(data_set) L1 = generate_Lk_by_Ck(data_set, C1, min_support, support_data) Lksub1 = L1.copy() L = [] L.append(Lksub1) for i in range(2, k + 1): Ci = create_Ck(Lksub1, i) Li = generate_Lk_by_Ck(data_set, Ci, min_support, support_data) Lksub1 = Li.copy() L.append(Lksub1) return L, support_data
这段代码实现了Apriori算法中的频繁项集挖掘过程。Apriori算法是一种常见的关联规则挖掘算法,用于发现数据集中的频繁项集。
函数generate_L的输入参数包括数据集data_set、最大项数k和最小支持度min_support。其中,数据集data_set是一个列表,每个元素表示一条交易记录,其中包含多个项;最大项数k是指挖掘的频繁项集中最多包含的项数;最小支持度min_support是指一个项集在所有交易记录中出现的次数占比必须大于等于min_support才能被认为是频繁项集。
函数内部首先创建了一个空字典support_data,用于存储频繁项集及其支持度信息。然后调用函数create_C1创建初始候选项集C1,再调用函数generate_Lk_by_Ck根据C1生成频繁1项集L1。接着,将L1存储在列表L中,并将L1赋值给Lksub1,表示当前频繁项集的项数为1。
接下来,函数generate_L通过一个循环,依次生成包含2~k个项的频繁项集。在每次循环中,首先调用函数create_Ck根据Lksub1生成候选项集Ci,然后调用函数generate_Lk_by_Ck根据Ci生成频繁项集Li。将Li存储在列表L中,并将Li赋值给Lksub1,表示当前频繁项集的项数为i。最后,函数generate_L返回频繁项集列表L和支持度字典support_data。
from apriori import * # 编写代码实现关联规则抽取 dataset = load_data('data/apriori.txt') # 定义 generate_big_rules函数来获取关联规则 def generate_big_rules(L, support_data, min_conf): big_rule_list = [] sub_set_list = [] for i in range(0, len(L)): for freq_set in L[i]: # freq_set:('B4')、('B4', 'C4', 'H4') for sub_set in sub_set_list: #**************** BEGIN ***************** if sub_set.issubset(freq_set): # 计算置信度 # 前件、后件、支持度、置信度 #**************** END ******************* sub_set_list.append(freq_set) return big_rule_list def task(): L, support_data = generate_L(dataset, k=4, min_support=0.06) # 根据频繁项集寻找关联规则,设置置信度为 0.75 big_rules_list = generate_big_rules(L, support_data, min_conf=0.75) return big_rules_list
这段代码的功能是实现关联规则抽取,具体步骤如下:
1. 使用apriori.py中的load_data方法从文件中读入经过离散化处理后的数据集。
2. 定义generate_big_rules函数来获取关联规则。该函数的输入参数包括频繁项集L、支持度信息support_data和最小置信度min_conf。
3. 在函数中,通过两层循环遍历频繁项集L,对于每个频繁项freq_set,再遍历它的子集sub_set_list,判断子集是否是freq_set的子集,如果是,则计算置信度并保存关联规则。
4. 最后返回关联规则列表big_rule_list。
5. 在task函数中,调用generate_L方法得到频繁项集L和支持度信息support_data,然后调用generate_big_rules方法得到关联规则列表big_rules_list,并将其作为返回值。
需要注意的是,该代码中的generate_L函数没有给出,因此可能需要在其他地方进行定义。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)