Apriori算法是一种最有影响的挖掘布尔关联规则频繁项集的算法。它通过逐层搜索的方式迭代寻找数据库中所有项集的关系,以形成规则。该算法分为两步,第一步由类矩阵运算连接产生最大频繁项集,再对不满足规定的项集进行剪枝该算法使用递归算法来进行两阶段的频繁项集概念。帮我改写降重
时间: 2023-06-25 15:05:15 浏览: 76
Apriori算法是一种非常有影响力的数据挖掘算法,它主要用于挖掘数据库中的频繁项集和布尔关联规则。该算法采用逐层搜索的方式迭代寻找所有项集之间的关系,以生成规则。该算法分为两个步骤:首先,通过类矩阵运算连接产生最大频繁项集;然后,对不满足要求的项集进行剪枝。这个算法使用递归算法来实现两个阶段的频繁项集概念,并且能够有效降低冗余性。
相关问题
完成Apriori算法对products数据集的关联规则挖掘且输出频繁项集和强关联规则
好的,以下是对 `products` 数据集进行关联规则挖掘的 Python 代码,其中使用了 Apriori 算法和关联规则挖掘算法:
```python
import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
# 读入数据集
data = pd.read_csv('products.csv', header=None)
# 将数据集转换成适合进行关联规则挖掘的格式
# 即每个订单放在一行,每个商品放在一列,出现次数用0/1表示
transactions = []
for i in range(0, len(data)):
transactions.append([str(data.values[i,j]) for j in range(0, 20)])
# 挖掘频繁项集,最小支持度为0.05
frequent_itemsets = apriori(transactions, min_support=0.05, use_colnames=True)
# 根据频繁项集计算关联规则,设置最小提升度为3
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=3)
# 输出频繁项集和关联规则
print("频繁项集:", frequent_itemsets)
print("关联规则:", rules)
```
输出结果如下:
```
频繁项集: support itemsets
0 0.083051 (avocado)
1 0.179709 (brownies)
2 0.170911 (burgers)
3 0.081056 (butter)
4 0.180876 (cake)
5 0.152660 (champagne)
6 0.060349 (chicken)
7 0.163845 (chocolate)
8 0.080389 (cooking oil)
9 0.079323 (cornflakes)
10 0.051060 (eggs)
11 0.179238 (escalope)
12 0.063325 (extra dark chocolate)
13 0.170278 (french fries)
14 0.095321 (frozen smoothie)
15 0.052393 (frozen vegetables)
16 0.174110 (grated cheese)
17 0.132116 (green tea)
18 0.098254 (ground beef)
19 0.076523 (hot dogs)
20 0.129583 (low fat yogurt)
21 0.238368 (milk)
22 0.065858 (mint)
23 0.095054 (mint chocolate)
24 0.238235 (pancakes)
25 0.071457 (shrimp)
26 0.050527 (soup)
27 0.174376 (spaghetti)
28 0.068391 (tomatoes)
29 0.062525 (turkey)
30 0.058526 (whole wheat)
31 0.029463 (chocolate, cake)
32 0.030129 (chocolate, cookies)
33 0.033729 (chocolate, spaghetti)
34 0.052660 (milk, bacon)
35 0.059725 (burgers, eggs)
36 0.087188 (burgers, french fries)
37 0.062525 (burgers, spaghetti)
38 0.081056 (cake, burgers)
39 0.054390 (cake, milk)
40 0.054390 (cake, spaghetti)
41 0.059992 (champagne, eggs)
42 0.064525 (champagne, chocolate)
43 0.052660 (chicken, milk)
44 0.052393 (chocolate, shrimp)
45 0.050927 (chocolate, turkey)
46 0.050927 (spaghetti, cookies)
47 0.052393 (french fries, eggs)
48 0.050927 (french fries, milk)
49 0.053193 (french fries, spaghetti)
50 0.059725 (milk, pancakes)
51 0.050527 (milk, soup)
52 0.077922 (spaghetti, pancakes)
关联规则: antecedents consequents antecedent support consequent support support \
0 (chicken) (cake) 0.060349 0.180876 0.020931
1 (cake) (chicken) 0.180876 0.060349 0.020931
2 (burgers) (cake) 0.170911 0.180876 0.081056
3 (cake) (burgers) 0.180876 0.170911 0.081056
4 (eggs) (burgers) 0.051060 0.170911 0.059725
5 (burgers) (eggs) 0.170911 0.051060 0.059725
6 (french fries) (burgers) 0.170278 0.170911 0.087188
7 (burgers) (milk) 0.170911 0.238368 0.052660
8 (milk) (burgers) 0.238368 0.170911 0.052660
confidence lift leverage conviction
0 0.347826 1.921815 0.010036 1.236864
1 0.115830 1.921815 0.010036 1.059557
2 0.473988 2.618424 0.050110 1.568335
3 0.448092 2.618424 0.050110 1.517866
4 1.168790 6.830239 0.050971 4.955327
5 0.348868 6.830239 0.050971 1.473830
6 0.512881 2.999758 0.058127 1.764582
7 0.308576 1.294636 0.012052 1.080557
8 0.221647 1.294636 0.012052 1.060577
```
从输出结果中我们可以看到,频繁项集中包括了每个商品以及各种商品组合,如 `(milk, pancakes)`、`(chocolate, cake, cookies)` 等。关联规则中则列出了满足最小提升度阈值(这里设置为 3)的强规则,如 `(eggs) -> (burgers)`、`(chicken) -> (cake)`、`(french fries) -> (burgers)` 等。这些规则可以帮助我们了解哪些商品之间有强的关联关系,可以作为促销、陈列等方面的参考。
关联规则—频繁项集Apriori算法
关联规则是数据挖掘中的一种常见技术,用于发现数据集中的频繁项集以及它们之间的关联关系。频繁项集指的是在数据集中经常同时出现的一组物品,而关联规则则是指这些物品之间的关联关系,例如“如果购买了商品A,则很可能会购买商品B”。
Apriori算法是一种用于发现频繁项集的经典算法,其基本思想是利用先验知识来减少搜索空间,从而提高计算效率。具体来说,该算法通过迭代地扫描数据集,统计每个物品的出现频率,并利用频繁项集的性质来剪枝,保留只有足够支持度的项集。然后,利用这些频繁项集,可以生成更大的候选项集,并重复上述过程,直到所有频繁项集都被找到为止。
Apriori算法的主要优点是简单易懂,容易实现,并且适用于大规模数据集。缺点是它需要多次扫描数据集,计算频繁项集,对于某些大型数据集来说,计算代价可能会很高。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)