Python实现FP-growth算法关联规则挖掘(xlsx数据)
FP-growth算法是一种基于频繁模式挖掘的算法,可以用于对数据集进行关联规则挖掘。下面是使用Python实现FP-growth算法对于xlsx数据的示例代码。
首先,需要安装pandas和mlxtend库:
pip install pandas
pip install mlxtend
然后,读取数据集并转化为事务列表:
import pandas as pd
# 读取xlsx文件
df = pd.read_excel('data.xlsx')
# 转化为事务列表
transactions = df.apply(lambda x: x.dropna().tolist(), axis=1).tolist()
接下来,使用mlxtend库中的FP-growth算法实现关联规则挖掘:
from mlxtend.frequent_patterns import fpgrowth
# 运行FP-growth算法
frequent_itemsets = fpgrowth(transactions, min_support=0.01, use_colnames=True)
# 输出结果
print(frequent_itemsets)
其中,min_support参数表示最小支持度阈值,use_colnames参数表示是否使用项集名称而不是项集本身作为输出。
运行以上代码即可得到频繁项集的结果。
原文地址: https://www.cveoy.top/t/topic/or7v 著作权归作者所有。请勿转载和采集!