[英]How to count the occurrences of sets which are part of a list in Python?
Trying to implement the apriori algorithm and made it to the point where I can extract the subsets occurring together in all transactions. 尝试实现apriori算法并使其达到我可以提取所有事务中一起出现的子集的程度。
This is what I have: 这就是我所拥有的:
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'])]
For example set(['Breakfast & Brunch', 'Restaurants'])
occurs twice and I need to keep track of the numbers of occurrences together with the corresponding patterns. 例如,
set(['Breakfast & Brunch', 'Restaurants'])
出现两次,我需要跟踪出现的次数以及相应的模式。
I tried to use: 我试着用:
from collections import Counter
support_set = Counter()
# some code that generated the list above
support_set.update(subsets)
but it generates this error: 但它会生成此错误:
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'
Any idea? 任何的想法?
You can turn the sets to frozenset
instances which are hashable: 您可以将集合转换为可
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})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.