mirror of
https://github.com/apachecn/ailearning.git
synced 2026-02-11 22:35:35 +08:00
@@ -74,6 +74,14 @@ Apriori 算法的两个输入参数分别是最小支持度和数据集。
|
||||
|
||||
### 生成候选项集
|
||||
|
||||
下面会创建一个用于构建初始集合的函数,也会创建一个通过扫描数据集以寻找交易记录子集的函数,
|
||||
数据扫描的伪代码如下:
|
||||
* 对数据集中的每条交易记录 tran
|
||||
* 对每个候选项集 can
|
||||
* 检查一下 can 是否是 tran 的子集: 如果是则增加 can 的计数值
|
||||
* 对每个候选项集
|
||||
* 如果其支持度不低于最小值,则保留该项集
|
||||
* 返回所有频繁项集列表
|
||||
以下是一些辅助函数。
|
||||
|
||||
#### 加载数据集
|
||||
@@ -152,6 +160,8 @@ def scanD(D, Ck, minSupport):
|
||||
return retList, supportData
|
||||
```
|
||||
|
||||
完整代码地址: <https://github.com/apachecn/MachineLearning/blob/master/src/python/11.Apriori/apriori.py>
|
||||
|
||||
### 组织完整的 Apriori 算法
|
||||
|
||||
#### 输入频繁项集列表 Lk 与返回的元素个数 k,然后输出所有可能的候选项集 Ck
|
||||
@@ -238,6 +248,8 @@ def apriori(dataSet, minSupport=0.5):
|
||||
|
||||
到这一步,我们就找出我们所需要的 `频繁项集` 和他们的 `支持度` 了,接下来再找出关联规则即可!
|
||||
|
||||
完整代码地址: <https://github.com/apachecn/MachineLearning/blob/master/src/python/11.Apriori/apriori.py>
|
||||
|
||||
## 从频繁项集中挖掘关联规则
|
||||
|
||||
前面我们介绍了用于发现 `频繁项集` 的 Apriori 算法,现在要解决的问题是如何找出 `关联规则`。
|
||||
|
||||
@@ -310,6 +310,8 @@ def testApriori():
|
||||
print 'L(0.7): ', L1
|
||||
print 'supportData(0.7): ', supportData1
|
||||
|
||||
print '->->->->->->->->->->->->->->->->->->->->->->->->->->->->'
|
||||
|
||||
# Apriori 算法生成频繁项集以及它们的支持度
|
||||
L2, supportData2 = apriori(dataSet, minSupport=0.5)
|
||||
print 'L(0.5): ', L2
|
||||
@@ -331,10 +333,10 @@ def testGenerateRules():
|
||||
|
||||
def main():
|
||||
# 测试 Apriori 算法
|
||||
# testApriori()
|
||||
testApriori()
|
||||
|
||||
# 生成关联规则
|
||||
testGenerateRules()
|
||||
# testGenerateRules()
|
||||
|
||||
# # 项目案例
|
||||
# # 构建美国国会投票记录的事务数据集
|
||||
|
||||
Reference in New Issue
Block a user