python中有计算关闭频繁项集的函数吗

  • 本文关键字:函数 计算 python python apriori
  • 更新时间 :
  • 英文 :


我正在搜索一个python算法来计算紧密频繁项集关联,但我只找到了一个java代码?有人能帮我吗?

开始

#Import all basic libray
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
import time
from mlxtend.frequent_patterns import fpgrowth
#Task1 : Compute Frequent Item Set using  mlxtend.frequent_patterns
te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)
start_time = time.time()
frequent = fpgrowth(df, min_support=0.001, use_colnames=True)
print('Time to find frequent itemset')
print("--- %s seconds ---" % (time.time() - start_time))
# Task 2&3: Find closed/max frequent itemset using frequent itemset found in task1
su = frequent.support.unique()#all unique support count
#Dictionay storing itemset with same support count key
fredic = {}
for i in range(len(su)):
inset = list(frequent.loc[frequent.support ==su[i]]['itemsets'])
fredic[su[i]] = inset
#Dictionay storing itemset with  support count <= key
fredic2 = {}
for i in range(len(su)):
inset2 = list(frequent.loc[frequent.support<=su[i]]['itemsets'])
fredic2[su[i]] = inset2
#Find Closed frequent itemset
start_time = time.time()
cl = []
for index, row in frequent.iterrows():
isclose = True
cli = row['itemsets']
cls = row['support']
checkset = fredic[cls]
for i in checkset:
if (cli!=i):
if(frozenset.issubset(cli,i)):
isclose = False
break

if(isclose):
cl.append(row['itemsets'])
print('Time to find Close frequent itemset')
print("--- %s seconds ---" % (time.time() - start_time))  

#Find Max frequent itemset
start_time = time.time()
ml = []
for index, row in frequent.iterrows():
isclose = True
cli = row['itemsets']
cls = row['support']
checkset = fredic2[cls]
for i in checkset:
if (cli!=i):
if(frozenset.issubset(cli,i)):
isclose = False
break

if(isclose):
ml.append(row['itemsets'])
print('Time to find Max frequent itemset')
print("--- %s seconds ---" % (time.time() - start_time))

相关内容

  • 没有找到相关文章

最新更新