fla*_*nco 5 python data-mining
尝试实现 apriori 算法并使其达到可以提取所有事务中一起出现的子集的程度。
这就是我所拥有的:
subsets = [set(['Breakfast & Brunch', 'Restaurants']), set(['American (Traditional)', 'Breakfast & Brunch']), set(['American (Traditional)', 'Restaurants']), set(['American (Traditional)', 'Breakfast & Brunch']), set(['Breakfast & Brunch', 'Restaurants']), set(['American (Traditional)', 'Restaurants'])]
Run Code Online (Sandbox Code Playgroud)
例如set(['Breakfast & Brunch', 'Restaurants'])出现两次,我需要跟踪出现的次数以及相应的模式。
我尝试使用:
from collections import Counter
support_set = Counter()
# some code that generated the list above
support_set.update(subsets)
Run Code Online (Sandbox Code Playgroud)
但它会产生这个错误:
supported = itemsets_support(transactions, candidates)
File "apriori.py", line 77, in itemsets_support
support_set.update(subsets)
File"/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py", line 567, in update
self[elem] = self_get(elem, 0) + 1
TypeError: unhashable type: 'set'
Run Code Online (Sandbox Code Playgroud)
任何的想法?
您可以将集合转换frozenset为可散列的实例:
>>> from collections import Counter
>>> subsets = [set(['Breakfast & Brunch', 'Restaurants']), set(['American (Traditional)', 'Breakfast & Brunch']), set(['American (Traditional)', 'Restaurants']), set(['American (Traditional)', 'Breakfast & Brunch']), set(['Breakfast & Brunch', 'Restaurants']), set(['American (Traditional)', 'Restaurants'])]
>>> c = Counter(frozenset(s) for s in subsets)
>>> c
Counter({frozenset(['American (Traditional)', 'Restaurants']): 2, frozenset(['Breakfast & Brunch', 'Restaurants']): 2, frozenset(['American (Traditional)', 'Breakfast & Brunch']): 2})
Run Code Online (Sandbox Code Playgroud)