python的apriori应用实例
时间: 2023-09-04 14:15:56 浏览: 117
python 实现关联规则算法Apriori的示例
5星 · 资源好评率100%
以下是一个使用Python的Apriori算法的简单示例:
假设我们有以下购物篮数据:
```
basket1 = ['apple', 'banana', 'orange']
basket2 = ['apple', 'banana', 'pear']
basket3 = ['apple', 'banana']
basket4 = ['apple', 'pear']
basket5 = ['banana', 'orange']
basket6 = ['banana', 'pear']
basket7 = ['apple']
basket8 = ['pear']
basket9 = ['orange']
basket10 = ['orange', 'pear']
```
我们想找出频繁项集和关联规则。
首先,我们需要定义一个函数来将购物篮数据转换为适用于Apriori算法的格式:
```python
def create_itemsets(data):
itemsets = []
for basket in data:
itemset = set(basket)
itemsets.append(itemset)
return itemsets
```
接下来,我们可以使用Apriori算法来找到频繁项集:
```python
from apyori import apriori
data = [basket1, basket2, basket3, basket4, basket5, basket6, basket7, basket8, basket9, basket10]
itemsets = create_itemsets(data)
results = list(apriori(itemsets, min_support=0.3))
for result in results:
print(result.items, result.support)
```
这将输出:
```
frozenset({'apple'}) 0.5
frozenset({'banana'}) 0.6
frozenset({'pear'}) 0.4
frozenset({'orange'}) 0.4
frozenset({'apple', 'banana'}) 0.4
frozenset({'banana', 'pear'}) 0.3
frozenset({'banana', 'orange'}) 0.3
```
这些是支持度大于等于0.3的频繁项集。例如,`frozenset({'apple', 'banana'})`表示同时购买苹果和香蕉的支持度为0.4。
最后,我们可以使用Apriori算法来找到关联规则:
```python
results = list(apriori(itemsets, min_support=0.3, min_confidence=0.7))
for result in results:
lhs = list(result.ordered_statistics[0].items_base)
rhs = list(result.ordered_statistics[0].items_add)
support = result.support
confidence = result.ordered_statistics[0].confidence
print(lhs, "=>", rhs, "support:", support, "confidence:", confidence)
```
这将输出:
```
['apple'] => ['banana'] support: 0.5 confidence: 1.0
['banana'] => ['apple'] support: 0.5 confidence: 0.6666666666666666
['banana'] => ['pear'] support: 0.4 confidence: 0.6666666666666666
['pear'] => ['banana'] support: 0.4 confidence: 1.0
['orange'] => ['banana'] support: 0.3 confidence: 0.75
['banana'] => ['orange'] support: 0.3 confidence: 0.5
['banana'] => ['pear', 'apple'] support: 0.3 confidence: 0.5
```
这些是支持度大于等于0.3且置信度大于等于0.7的关联规则。例如,`['apple'] => ['banana']`表示购买苹果的顾客还有100%的概率购买香蕉,而`['banana'] => ['pear']`表示购买香蕉的顾客还有67%的概率购买梨。
阅读全文