关联规则挖掘系统

这学期的大数据课设由三部分组成,记录自己负责的关联规则挖掘系统部分内容。

环境:代码由python实现,版本为3.7版本,界面设计采用tkinter,采用两种算法:Apriori和fpgrowth

Apriori算法使用python的模块efficient-apriori

fpgrowth算法采用orange-associate模块,其实fpgrowth算法在python中有更好地封装:pyfpgrowth

系统的主界面如下图:

在做课设的时候,有一个要求的画出fp树,查阅网上资料后发现并没有好的实现方法,至少我试过了没有得到理想的效果

但是在github上找到一个html+js编写的画fptree的网页,点此跳转

于是就想着将这个网页嵌入python中,如果是pyqt实现的页面的话是比较好解决的,但是tkinter的话,最后找到唯一的一个方法就是使用cefpython3模块,可以内嵌浏览器。

此系统读入数据可放入txt文件中,格式如下:
读入数据格式

数据可以是汉字,字母,数字。

本系统所有代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 19 22:15:06 2019

@author: shanhainanhua
"""

from tkinter import *
from tkinter import filedialog
#from fptree import *
#from pymining import itemmining,assocrules
from orangecontrib.associate.fpgrowth import *
from efficient_apriori import apriori
from orangecontrib.associate.fpgrowth import *
import functools


global minsupport
global minconfidence

data=[]
#事件函数


def LoadData():
global data
data=[]
file_path=filedialog.askopenfilename(title='打开单个文件',
filetypes=[('All Files', '*'),("文本文件", '*.txt')], # 只处理的文件类型
initialdir='g:/') # 初始目录
print(file_path)
e1_value=file_path
#e1.delete(1,END)
e1.insert(END,e1_value)
e1.grid(row=0, column=1,pady=18,padx=10)
f=open(file_path,'rb').readlines()

for line in f:
#line = line.decode('gbk').strip()
line = line.decode('UTF-8').strip()
tmp=[]
#line=line.split(",")
line=line.split(" ")
for item in line:
tmp.append(str(item))
data.append(tmp)
print("输入的数据为{}".format(data))


def Start():
#先获取组件的值
global minsupport
global minconfidence
minsupport=float(e2.get())
minconfidence=float(e3.get())
print("minsupport={}".format(minsupport))
print("minconfidence={}".format(minconfidence))
choice=int(r.get())
if choice==1:
print("选择了Apriori算法")
DoApriori()

if choice==2:
print("选择了fp-growth算法")
DoFpGrowth()



def DoApriori():
'''
datalen=len(data)
thisminsupport=minsupport/datalen
'''
itemsets, rules = apriori(data, minsupport, minconfidence)
print("频繁项集为{}".format(itemsets))
print("关联规则为{}".format(rules))
for item in itemsets:
print(item)
for rule in rules:
print(rule)
text_result.insert(END,"使用Apriori算法生成频繁项集和关联规则\n")
text_result.insert(END,("频繁项集为:\n"+str(itemsets)+"\n"))
text_result.insert(END,("关联规则为:\n"+str(rules)+"\n"))


def DoFpGrowth():
strSet = set(functools.reduce(lambda a,b:a+b, data))
strEncode = dict(zip(strSet,range(len(strSet)))) #编码字典,即:{'ArticleTag_BS': 6,'Country_Argentina': 53,etc...}
strDecode = dict(zip(strEncode.values(), strEncode.keys())) #解码字典,即:{6:'ArticleTag_BS',53:'Country_Argentina',etc...}
data_int = [list(map(lambda item:strEncode[item],row)) for row in data] #编码后的输入数据
itemsets=frequent_itemsets(data_int,.4)
itemsets=dict(itemsets)
#print(itemsets)
rules = association_rules(itemsets, .5) #这里设置置信度
rules = list(rules)
print("关联规则\n")
print(rules)
start=0
tmpitem=[]
for item in itemsets:
tmpj=[]
for j in item:
j=strDecode[j]
tmpj.append(j)
tmpitem.append(set(tmpj))
tmprules=[]
for rule in rules:
tmpj=[]
for j in rule[:-2]:
j=strDecode[list(j)[0]]
tmpj.append(j)
tmprules.append(tmpj)
text_result.insert(END,"使用fp-growth算法生成频繁项集和关联规则\n")
text_result.insert(END,("频繁项集为:\n"+str(tmpitem)+"\n"))
text_result.insert(END,("关联规则为:\n"+str(tmprules)+"\n"))

def Clear():
text_result.delete(1.0,'end')
e1.delete(2.0,END)
e2.delete(0, END)
e3.delete(0, END)

def Close():
root.quit()

def showtree():
root.deiconify()

def save_file():
file_path = filedialog.asksaveasfilename(title=u'保存文件')
print('保存文件:', file_path)
file_text = text_result.get('1.0',END)
if file_path is not None:
with open(file=file_path, mode='a+', encoding='utf-8') as file:
file.write(file_text)
dialog.Dialog(None, {'title': 'File Modified', 'text': '保存完成', 'bitmap': 'warning', 'default': 0,
'strings': ('OK', 'Cancle')})
print('保存完成')

#创建界面,进行事件绑定
root1 = Tk()
root1.geometry("850x450")
root1.title("关联规则挖掘系统")
root1.resizable=(False,False)
root1["background"] = "SkyBlue"
root1.attributes("-alpha", 0.9)


# Thinker总共提供了三种布局组件的方法:pack(),grid()和place()
# grid()方法允许你用表格的形式来管理组件的位置
# row选项代表行,column选项代表列
# 例如row=1,column=2表示第二行第三列(0表示第一行)

fram1=Frame(root1)
Label(fram1,text="频繁项集和关联规则",font=("华文行楷", 20), fg="green").grid(row=0,column=0)
text_result=Text(fram1)
text_result.grid(row=2,column=0)
fram1.grid(row=0,rowspan=4,column=4)

fram2=Frame(root1,width=300,height=500)

btnloaddata=Button(fram2, text="导入数据",command=LoadData)
btnloaddata.grid(row=0,column=0,pady=18)
Label(fram2, text="最小支持度").grid(row=1,column=0,pady=18)
Label(fram2, text="最小置信度").grid(row=2,column=0,pady=18)

e1_value="导入文件的路径:\n"
e1 = Text(fram2,width=20,height=3)
e1.insert(1.0,e1_value)


e2 = Entry(fram2)
e3 = Entry(fram2)


e1.grid(row=0, column=1,pady=18,padx=10)
e2.grid(row=1, column=1,pady=18,padx=10)
e3.grid(row=2, column=1,pady=18,padx=10)

fram2.grid(row=0,column=0,rowspan=4,columnspan=3,pady=18)

# 创建单选项
r = IntVar()
male_select = Radiobutton(root1,text="Apriori",value=1,variable=r)
male_select.grid(row=3,column=0)
female_select = Radiobutton(root1,text="Fp-growth",value=2,variable=r)
female_select.grid(row=3,column=1)

fram3=Frame(root1,width=100,height=10)

btnstart=Button(fram3,text="开始",command=Start)
btnclear=Button(fram3,text="清空",command=Clear)
btnshowtree=Button(fram3,text="显示Fptree",command=showtree)
btnsave=Button(fram3,text="保存结果",command=save_file)
#btnclose=Button(fram3,text="结束",command="")
btnstart.grid(row=0,column=0,padx=10,pady=10)
btnclear.grid(row=0,column=1,padx=10,pady=10)
btnshowtree.grid(row=0,column=2,padx=10,pady=10)
btnsave.grid(row=0,column=3,padx=10,pady=10)
#btnclose.grid(row=0,column=2,padx=10,pady=10)

fram3.grid(row=4,column=0,columnspan=3)

fram1["background"] = "white"
fram2["background"] = "SkyBlue"
fram3["background"] = "SkyBlue"


######################################################################################
# Example of embedding CEF Python browser using Tkinter toolkit.
# This example has two widgets: a navigation bar and a browser.
#
# NOTE: This example often crashes on Mac (Python 2.7, Tk 8.5/8.6)
# during initial app loading with such message:
# "Segmentation fault: 11". Reported as Issue #309.
#
# Tested configurations:
# - Tk 8.5 on Windows/Mac
# - Tk 8.6 on Linux
# - CEF Python v55.3+
#
# Known issue on Linux: When typing url, mouse must be over url
# entry widget otherwise keyboard focus is lost (Issue #255
# and Issue #284).

#以下代码为了嵌入html页面,具体代码没有仔细研究过,只是为了应用,只要修改embed_browser的url路径即可
from cefpython3 import cefpython as cef
import ctypes
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
import sys
import os
import platform
import logging as _logging

global root
root = tk.Tk()

# Fix for PyCharm hints warnings
WindowUtils = cef.WindowUtils()

# Platforms
WINDOWS = (platform.system() == "Windows")
LINUX = (platform.system() == "Linux")
MAC = (platform.system() == "Darwin")

# Globals
logger = _logging.getLogger("tkinter_.py")

# Constants
# Tk 8.5 doesn't support png images
IMAGE_EXT = ".png" if tk.TkVersion > 8.5 else ".gif"


class MainFrame(tk.Frame):

def __init__(self, root):
self.browser_frame = None
self.navigation_bar = None

# Root
root.geometry("900x640+300+200")
tk.Grid.rowconfigure(root, 0, weight=1)
tk.Grid.columnconfigure(root, 0, weight=1)

# MainFrame
tk.Frame.__init__(self, root)
self.master.title("FpTree")
self.master.protocol("WM_DELETE_WINDOW", self.on_close)
self.master.bind("<Configure>", self.on_root_configure)
self.setup_icon()
self.bind("<Configure>", self.on_configure)
self.bind("<FocusIn>", self.on_focus_in)
self.bind("<FocusOut>", self.on_focus_out)

# NavigationBar
self.navigation_bar = NavigationBar(self)
self.navigation_bar.grid(row=0, column=0,
sticky=(tk.N + tk.S + tk.E + tk.W))
tk.Grid.rowconfigure(self, 0, weight=0)
tk.Grid.columnconfigure(self, 0, weight=0)

# BrowserFrame
self.browser_frame = BrowserFrame(self, self.navigation_bar)
self.browser_frame.grid(row=1, column=0,
sticky=(tk.N + tk.S + tk.E + tk.W))
tk.Grid.rowconfigure(self, 1, weight=1)
tk.Grid.columnconfigure(self, 0, weight=1)

# Pack MainFrame
self.pack(fill=tk.BOTH, expand=tk.YES)

def on_root_configure(self, _):
logger.debug("MainFrame.on_root_configure")
if self.browser_frame:
self.browser_frame.on_root_configure()

def on_configure(self, event):
logger.debug("MainFrame.on_configure")
if self.browser_frame:
width = event.width
height = event.height
if self.navigation_bar:
height = height - self.navigation_bar.winfo_height()
self.browser_frame.on_mainframe_configure(width, height)

def on_focus_in(self, _):
logger.debug("MainFrame.on_focus_in")

def on_focus_out(self, _):
logger.debug("MainFrame.on_focus_out")

def on_close(self):
if self.browser_frame:
self.browser_frame.on_root_close()
self.master.destroy()

def get_browser(self):
if self.browser_frame:
return self.browser_frame.browser
return None

def get_browser_frame(self):
if self.browser_frame:
return self.browser_frame
return None

def setup_icon(self):
resources = os.path.join(os.path.dirname(__file__), "resources")
icon_path = os.path.join(resources, "tkinter"+IMAGE_EXT)
if os.path.exists(icon_path):
self.icon = tk.PhotoImage(file=icon_path)
# noinspection PyProtectedMember
self.master.call("wm", "iconphoto", self.master._w, self.icon)


class BrowserFrame(tk.Frame):

def __init__(self, master, navigation_bar=None):
self.navigation_bar = navigation_bar
self.closing = False
self.browser = None
tk.Frame.__init__(self, master)
self.bind("<FocusIn>", self.on_focus_in)
self.bind("<FocusOut>", self.on_focus_out)
self.bind("<Configure>", self.on_configure)
self.focus_set()

def embed_browser(self):
window_info = cef.WindowInfo()
rect = [0, 0, self.winfo_width(), self.winfo_height()]
window_info.SetAsChild(self.get_window_handle(), rect)
self.browser = cef.CreateBrowserSync(window_info,
url="file:///fptree.html") #todo
#只需要修改url中的内容,嵌入本地html
assert self.browser
self.browser.SetClientHandler(LoadHandler(self))
self.browser.SetClientHandler(FocusHandler(self))
self.message_loop_work()

def get_window_handle(self):
if self.winfo_id() > 0:
return self.winfo_id()
elif MAC:
# On Mac window id is an invalid negative value (Issue #308).
# This is kind of a dirty hack to get window handle using
# PyObjC package. If you change structure of windows then you
# need to do modifications here as well.
# noinspection PyUnresolvedReferences
from AppKit import NSApp
# noinspection PyUnresolvedReferences
import objc
# Sometimes there is more than one window, when application
# didn't close cleanly last time Python displays an NSAlert
# window asking whether to Reopen that window.
# noinspection PyUnresolvedReferences
return objc.pyobjc_id(NSApp.windows()[-1].contentView())
else:
raise Exception("Couldn't obtain window handle")

def message_loop_work(self):
cef.MessageLoopWork()
self.after(10, self.message_loop_work)

def on_configure(self, _):
if not self.browser:
self.embed_browser()

def on_root_configure(self):
# Root <Configure> event will be called when top window is moved
if self.browser:
self.browser.NotifyMoveOrResizeStarted()

def on_mainframe_configure(self, width, height):
if self.browser:
if WINDOWS:
ctypes.windll.user32.SetWindowPos(
self.browser.GetWindowHandle(), 0,
0, 0, width, height, 0x0002)
elif LINUX:
self.browser.SetBounds(0, 0, width, height)
self.browser.NotifyMoveOrResizeStarted()

def on_focus_in(self, _):
logger.debug("BrowserFrame.on_focus_in")
if self.browser:
self.browser.SetFocus(True)

def on_focus_out(self, _):
logger.debug("BrowserFrame.on_focus_out")
if self.browser:
self.browser.SetFocus(False)

def on_root_close(self):
if self.browser:
self.browser.CloseBrowser(True)
self.clear_browser_references()
self.destroy()

def clear_browser_references(self):
# Clear browser references that you keep anywhere in your
# code. All references must be cleared for CEF to shutdown cleanly.
self.browser = None


class LoadHandler(object):

def __init__(self, browser_frame):
self.browser_frame = browser_frame

def OnLoadStart(self, browser, **_):
if self.browser_frame.master.navigation_bar:
self.browser_frame.master.navigation_bar.set_url(browser.GetUrl())


class FocusHandler(object):

def __init__(self, browser_frame):
self.browser_frame = browser_frame

def OnTakeFocus(self, next_component, **_):
logger.debug("FocusHandler.OnTakeFocus, next={next}"
.format(next=next_component))

def OnSetFocus(self, source, **_):
logger.debug("FocusHandler.OnSetFocus, source={source}"
.format(source=source))
return False

def OnGotFocus(self, **_):
"""Fix CEF focus issues (#255). Call browser frame's focus_set
to get rid of type cursor in url entry widget."""
logger.debug("FocusHandler.OnGotFocus")
self.browser_frame.focus_set()


class NavigationBar(tk.Frame):
def __init__(self, master):
self.back_state = tk.NONE
self.forward_state = tk.NONE
self.back_image = None
self.forward_image = None
self.reload_image = None

tk.Frame.__init__(self, master)
resources = os.path.join(os.path.dirname(__file__), "resources")

# Back button
back_png = os.path.join(resources, "back"+IMAGE_EXT)
if os.path.exists(back_png):
self.back_image = tk.PhotoImage(file=back_png)
self.back_button = tk.Button(self, image=self.back_image,
command=self.go_back)
self.back_button.grid(row=0, column=0)

# Forward button
forward_png = os.path.join(resources, "forward"+IMAGE_EXT)
if os.path.exists(forward_png):
self.forward_image = tk.PhotoImage(file=forward_png)
self.forward_button = tk.Button(self, image=self.forward_image,
command=self.go_forward)
self.forward_button.grid(row=0, column=1)

# Reload button
reload_png = os.path.join(resources, "reload"+IMAGE_EXT)
if os.path.exists(reload_png):
self.reload_image = tk.PhotoImage(file=reload_png)
self.reload_button = tk.Button(self, image=self.reload_image,
command=self.reload)
self.reload_button.grid(row=0, column=2)

# Url entry
self.url_entry = tk.Entry(self)
self.url_entry.bind("<FocusIn>", self.on_url_focus_in)
self.url_entry.bind("<FocusOut>", self.on_url_focus_out)
self.url_entry.bind("<Return>", self.on_load_url)
self.url_entry.bind("<Button-1>", self.on_button1)
self.url_entry.grid(row=0, column=3,
sticky=(tk.N + tk.S + tk.E + tk.W))
tk.Grid.rowconfigure(self, 0, weight=100)
tk.Grid.columnconfigure(self, 3, weight=100)

# Update state of buttons
self.update_state()

def go_back(self):
if self.master.get_browser():
self.master.get_browser().GoBack()

def go_forward(self):
if self.master.get_browser():
self.master.get_browser().GoForward()

def reload(self):
if self.master.get_browser():
self.master.get_browser().Reload()

def set_url(self, url):
self.url_entry.delete(0, tk.END)
self.url_entry.insert(0, url)

def on_url_focus_in(self, _):
logger.debug("NavigationBar.on_url_focus_in")

def on_url_focus_out(self, _):
logger.debug("NavigationBar.on_url_focus_out")

def on_load_url(self, _):
if self.master.get_browser():
self.master.get_browser().StopLoad()
self.master.get_browser().LoadUrl(self.url_entry.get())

def on_button1(self, _):
"""Fix CEF focus issues (#255). See also FocusHandler.OnGotFocus."""
logger.debug("NavigationBar.on_button1")
self.master.master.focus_force()

def update_state(self):
browser = self.master.get_browser()
if not browser:
if self.back_state != tk.DISABLED:
self.back_button.config(state=tk.DISABLED)
self.back_state = tk.DISABLED
if self.forward_state != tk.DISABLED:
self.forward_button.config(state=tk.DISABLED)
self.forward_state = tk.DISABLED
self.after(100, self.update_state)
return
if browser.CanGoBack():
if self.back_state != tk.NORMAL:
self.back_button.config(state=tk.NORMAL)
self.back_state = tk.NORMAL
else:
if self.back_state != tk.DISABLED:
self.back_button.config(state=tk.DISABLED)
self.back_state = tk.DISABLED
if browser.CanGoForward():
if self.forward_state != tk.NORMAL:
self.forward_button.config(state=tk.NORMAL)
self.forward_state = tk.NORMAL
else:
if self.forward_state != tk.DISABLED:
self.forward_button.config(state=tk.DISABLED)
self.forward_state = tk.DISABLED
self.after(100, self.update_state)

def Click1():
root.deiconify()

if __name__ == '__main__':
logger.setLevel(_logging.INFO)
stream_handler = _logging.StreamHandler()
formatter = _logging.Formatter("[%(filename)s] %(message)s")
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
logger.info("CEF Python {ver}".format(ver=cef.__version__))
logger.info("Python {ver} {arch}".format(
ver=platform.python_version(), arch=platform.architecture()[0]))
logger.info("Tk {ver}".format(ver=tk.Tcl().eval('info patchlevel')))
assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this"
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error
root.withdraw()
#root = tk.Tk()

app = MainFrame(root)
# Tk must be initialized before CEF otherwise fatal error (Issue #306)
cef.Initialize()
root1.mainloop()
app.mainloop()
cef.Shutdown()





关联规则挖掘系统
https://shanhainanhua.github.io/2020/01/13/关联规则挖掘系统/
作者
wantong
发布于
2020年1月13日
许可协议