1.txt文件的打开与关闭
2.list的操作
3.字典的创建及保存
4.txt文件的创建与写入
输入:
结果:
import os
#input
min_sup=4
max_item=[]
data = []
transaction=[]
curent_transaction,current_sup=[],[]
result_transaction,result_sup=[],[]
#Storage results
result_dict={}
#exameple file
transactions_file_path="example_data.txt"
#Calculate sup
def apriori(current_itemset):
itemset=[]
for i in current_itemset:
# print(i)
m=0
for j in transaction :
#判断i是否为list类型
if not isinstance( i,list):
for k in j:
if (i== k):
m=m+1
else:
m_set = list(set(i).intersection(set(list(j))))
if len(m_set)==len(i):
m=m+1
if m>=min_sup:
result_dict.update({' '+str(i).replace(',','').replace('[','').replace(']','')+' #SUP:'+' ':m})
itemset.append(i)
return itemset,result_dict
#Count the elements in the current itemset
def item_element(new_transaction):
element=[]
if not isinstance(new_transaction[0],list):
for ele_t in new_transaction:
element.append(ele_t)
else:
element = new_transaction[0]
for transaction_cell in new_transaction:
element = list(set(element).union(set(transaction_cell)))
return element
#Generate a new itemset and record the itemset after each update
def transactions(transaction2,element):
new_transaction=[]
#Judge whether transaction is a list type
if not isinstance(transaction2[0],list) :
for i_elment in transaction2:
for j_element in element:
transport_item = []
transport_item.append(i_elment)
if i_elment!=j_element:
transport_item.append(j_element)
new_transaction.append(transport_item)
else:
for transaction_cell in transaction2:
diffrence_transaction_cell= list(set(element).difference(set(transaction_cell)))
for diffrence_element in diffrence_transaction_cell:
transport_item=[]
for item_element in transaction_cell:
transport_item.append(item_element)
transport_item.append(diffrence_element)
new_transaction.append(transport_item)
#删除重复的item
itemset=[]
Number_transaction=len(new_transaction)
flag=[1]* Number_transaction
for i in range(Number_transaction-1):
# j_d = i + 1
for j_d in range(i+1,Number_transaction):
m=list(set(new_transaction[i]).union(set(list(new_transaction[j_d]))))
if len(m)==len(new_transaction[j_d]):
flag[j_d]=0
for i in range(Number_transaction):
if flag[i]==1:
itemset.append(new_transaction[i])
return itemset
#current_itemset表示随机组合的item的集合,也即当前长度的transaction。
#transaction表示输入的集合,即已知的交易类目集合。
#minsup表示最小支持度
#max_item表示最大itemset中类目编号最大的值
def entrance(current_itemset):
element=item_element(current_itemset)
current_itemset= transactions(current_itemset, element)
if len(current_itemset)==0:
return result_transaction, result_sup
else:
if len(current_itemset[0]) <= len(transaction[0]) :
curent_transaction,current_sup=apriori(current_itemset)
result_transaction.append(curent_transaction)
result_sup.append(current_sup)
#回调函数,Callback function
return entrance(curent_transaction)
return result_transaction,result_sup
#Program entry
with open(transactions_file_path,"r") as file:
for line in file.readlines():
line = line.strip("\n").split(" ")
data =list(map(int, line))
transaction.append(data)
# length.append(len(transaction))
max_item.append(max(data))
max_number = int(max(max_item))
file.close()
#生成第一次的itemset,Generate the first itemset
for i in range(1,max_number+1):
curent_transaction.append(i)
curent_transaction,current_sup=apriori(curent_transaction)
entrance(curent_transaction)
#Write the results to the txt document
result_file=open('itemet_sup.txt',mode='w')
for item_sup in result_dict.items():
for value in range(len(item_sup)):
str1=item_sup[value]
result_file.write(str(str1))
result_file.write("\n")
result_file.close()
#Console Display Results
print(result_dict)
# for sup_len in result_sup:
# for sup in sup_len:
# print(result_transaction[sup_len][sup], end="")
# print(" ",end="")
# print("#sup: ",end="")
# print(sup)
# sup_i=+1
# # for sup_item in result_sup:
# # for sup in sup_item:
# # for result_item in result_transaction:
# # for item in result_item:
# # print(item,end="")
# # print(" ",end="")
# # print("#sup: ",end="")
# # print(sup)