Machine Learning 简明教程

Machine Learning - Apriori Algorithm

Apriori 是一种流行的算法,用于关联规则挖掘机器学习。它可用于查找事务数据库中的频繁项集并基于这些项集生成关联规则。该算法最先由 Rakesh Agrawal 和 Ramakrishnan Srikant 于 1994 年提出。

Apriori is a popular algorithm used for association rule mining in machine learning. It is used to find frequent itemsets in a transaction database and generate association rules based on those itemsets. The algorithm was first introduced by Rakesh Agrawal and Ramakrishnan Srikant in 1994.

Apriori 算法通过迭代扫描数据库来查找大小逐步增加的频繁项集来运行。它使用“自下而上”方法,从单个项开始,并逐渐向候选项集添加更多项,直到找不到更多的频繁项集。该算法还采用剪枝技术来减少需要检查的候选项集数量。

The Apriori algorithm works by iteratively scanning the database to find frequent itemsets of increasing size. It uses a "bottom-up" approach, starting with individual items and gradually adding more items to the candidate itemsets until no more frequent itemsets can be found. The algorithm also employs a pruning technique to reduce the number of candidate itemsets that need to be checked.

下面是 Apriori 算法中涉及的步骤的简要概述 −

Here’s a brief overview of the steps involved in the Apriori algorithm −

  1. Scan the database to find the support count of each item.

  2. Generate a set of frequent 1-itemsets based on the minimum support threshold.

  3. Generate a set of candidate 2-itemsets by combining frequent 1-itemsets.

  4. Scan the database again to find the support count of each candidate 2-itemset.

  5. Generate a set of frequent 2-itemsets based on the minimum support threshold and prune any candidate 2-itemsets that are not frequent.

  6. Repeat steps 3-5 to generate candidate k-itemsets and frequent k-itemsets until no more frequent itemsets can be found.

Example

在 Python 中,mlxtend 库提供了 Apriori 算法的实现。以下是如何结合使用 mlxtend 库和 sklearn 数据集在 iris 数据集上实现 Apriori 算法的一个示例。

In Python, the mlxtend library provides an implementation of the Apriori algorithm. Below is an example of how to use use the mlxtend library in conjunction with the sklearn datasets to implement the Apriori algorithm on iris dataset.

from mlxtend.frequent_patterns import apriori
from mlxtend.preprocessing import TransactionEncoder
from sklearn import datasets

# Load the iris dataset
iris = datasets.load_iris()

# Convert the dataset into a list of transactions
transactions = []
for i in range(len(iris.data)):
   transaction = []
   transaction.append('sepal_length=' + str(iris.data[i][0]))
   transaction.append('sepal_width=' + str(iris.data[i][1]))
   transaction.append('petal_length=' + str(iris.data[i][2]))
   transaction.append('petal_width=' + str(iris.data[i][3]))
   transaction.append('target=' + str(iris.target[i]))
   transactions.append(transaction)
# Encode the transactions using one-hot encoding
te = TransactionEncoder()
te_ary = te.fit(transactions).transform(transactions)
df = pd.DataFrame(te_ary, columns=te.columns_)

# Find frequent itemsets with a minimum support of 0.3
frequent_itemsets = apriori(df, min_support=0.3, use_colnames=True)

# Print the frequent itemsets
print(frequent_itemsets)

在此示例中,我们从 sklearn 加载了 iris 数据集,其中包含有关鸢尾花的信息。我们将数据集转换为一组事务,其中每个事务表示一朵花并包含其四个属性(花萼长度、花萼宽度、花瓣长度和花瓣宽度)以及目标标签(目标)的值。然后我们使用 one-hot 编码对事务进行编码,并使用 mlxtend 的 apriori 函数找到具有最小支持 0.3 的频繁项集。

In this example, we load the iris dataset from sklearn, which contains information about iris flowers. We convert the dataset into a list of transactions, where each transaction represents a single flower and contains the values for its four attributes (sepal_length, sepal_width, petal_length, and petal_width) as well as its target label (target). We then encode the transactions using one-hot encoding and find frequent itemsets with a minimum support of 0.3 using the apriori function from mlxtend.

此代码的输出将显示频繁项集及其对应的支持计数。由于 iris 数据集相对较小,我们只找到一个频繁项集 −

The output of this code will show the frequent itemsets and their corresponding support counts. Since the iris dataset is relatively small, we only find a single frequent itemset −

Output

   support   itemsets
0  0.333333  (target=0)
1  0.333333  (target=1)
2  0.333333  (target=2)

这表明数据集中的 33% 的事务同时具有 1.4 的花瓣长度值和目标标签 0(对应于 iris 数据集中的 setosa 物种)。

This indicates that 33% of the transactions in the dataset contain both a petal_length value of 1.4 and a target label of 0 (which corresponds to the setosa species in the iris dataset).

Aprioi 算法广泛用于市场篮子分析,以识别客户购买行为中的模式。例如,零售商可能会使用该算法寻找可以一起促销以增加销量的频繁购买的商品。该算法还可以用于其他领域,例如医疗保健、金融和社交媒体,以识别模式并从大型数据集中生成见解。

The Apriori algorithm is widely used in market basket analysis to identify patterns in customer purchasing behavior. For example, a retailer might use the algorithm to find frequently purchased items that can be promoted together to increase sales. The algorithm can also be used in other domains such as healthcare, finance, and social media to identify patterns and generate insights from large datasets.