forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheclat.py
More file actions
48 lines (41 loc) · 1.44 KB
/
eclat.py
File metadata and controls
48 lines (41 loc) · 1.44 KB
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
# -*- coding: utf-8 -*-
# Author: XuMing <[email protected]>
# Brief:
import sys
type = sys.getfilesystemencoding()
def eclat(prefix, items, min_support, freq_items):
while items:
# 初始遍历单个的元素是否是频繁
key, item = items.pop()
key_support = len(item)
if key_support >= min_support:
# print frozenset(sorted(prefix+[key]))
freq_items[frozenset(sorted(prefix + [key]))] = key_support
suffix = [] # 存储当前长度的项集
for other_key, other_item in items:
new_item = item & other_item # 求和其他集合求交集
if len(new_item) >= min_support:
suffix.append((other_key, new_item))
eclat(prefix + [key], sorted(suffix, key=lambda item: len(item[1]), reverse=True), min_support, freq_items)
return freq_items
def eclat_zc(data_set, min_support=1):
"""
Eclat方法
:param data_set:
:param min_support:
:return:
"""
# 将数据倒排
data = {}
trans_num = 0
for trans in data_set:
trans_num += 1
for item in trans:
if item not in data:
data[item] = set()
data[item].add(trans_num)
freq_items = {}
freq_items = eclat([], sorted(data.items(), key=lambda item: len(item[1]), reverse=True), min_support, freq_items)
return freq_items
if __name__ == "__main__":
print('hello')