python 关联分析
时间: 2023-10-13 16:21:43 浏览: 50
Python 中常用的关联分析算法是 Apriori 算法,它可以用来挖掘项集之间的关联关系。Apriori 算法是一种频繁项集挖掘算法,它通过扫描数据集来识别频繁项集,然后通过计算置信度或者提升度等指标来确定项集之间的关联关系。在使用 Apriori 算法进行关联分析时,需要设置最小支持度和最小置信度等参数,以便筛选出具有一定意义的关联规则。
在 Python 中,可以使用第三方库 `mlxtend` 来实现 Apriori 算法。该库提供了 `apriori` 函数和 `association_rules` 函数,分别用于计算频繁项集和关联规则。以下是一个简单的使用示例:
```python
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
import pandas as pd
# 构造数据集
data = {'A': [1, 1, 0, 1, 0, 1, 1, 1, 1, 0],
'B': [0, 1, 1, 1, 1, 1, 1, 1, 0, 1],
'C': [1, 1, 1, 1, 0, 1, 1, 0, 1, 1]}
df = pd.DataFrame(data)
# 计算频繁项集
frequent_itemsets = apriori(df, min_support=0.5, use_colnames=True)
# 计算关联规则
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7)
print(rules)
```
以上代码中,我们构造了一个包含三个项的数据集,并使用 `apriori` 函数计算出了最小支持度为 0.5 的频繁项集。然后使用 `association_rules` 函数计算出了置信度大于等于 0.7 的关联规则。最后输出关联规则的结果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)