我使用下面的一个函数来运行apriori算法并计算所有项集的支持度、置信度。该函数使用dictionary对象来存储项目的所有值及其相应的支持度、置信度。在
在运行if语句选择支持值最小值为0.15、置信度为0.6的项之后,在dict_items对象不可订阅的下面,我得到了一个错误。在
for key, value in largeSet.items()[1:]:
TypeError: 'dict_items' object is not subscriptable
def runApriori(data_iter, minSupport, minConfidence):
"""
run the apriori algorithm. data_iter is a record iterator
Return both:
- items (tuple, support)
- rules ((pretuple, posttuple), confidence)
"""
itemSet, transactionList = getItemSetTransactionList(data_iter)
freqSet = defaultdict(int)
largeSet = dict()
# Global dictionary which stores (key=n-itemSets,value=support)
# which satisfy minSupport
assocRules = dict()
# Dictionary which stores Association Rules
oneCSet = returnItemsWithMinSupport(itemSet,
transactionList,
minSupport,
freqSet)
currentLSet = oneCSet
k = 2
while(currentLSet != set([])):
largeSet[k-1] = currentLSet
currentLSet = joinSet(currentLSet, k)
currentCSet = returnItemsWithMinSupport(currentLSet,
transactionList,
minSupport,
freqSet)
currentLSet = currentCSet
k = k + 1
def getSupport(item):
"""local function which Returns the support of an item"""
return float(freqSet[item])/len(transactionList)
toRetItems = []
for key, value in largeSet.items():
toRetItems.extend([(tuple(item), getSupport(item))
for item in value])
toRetRules = []
for key, value in largeSet.items()[1:]:
for item in value:
_subsets = map(frozenset, [x for x in subsets(item)])
for element in _subsets:
remain = item.difference(element)
if len(remain) > 0:
confidence = getSupport(item)/getSupport(element)
if confidence >= minConfidence:
toRetRules.append(((tuple(element), tuple(remain)),
confidence))
return toRetItems, toRetRules
if __name__ == "__main__":
inFile = ''
minSupport = 0.15
minConfidence = 0.6
items, rules = runApriori(inFile, minSupport, minConfidence)
printResults(items, rules)
在cpython3.6之前(对于任何Python解释器,
dict
s没有可靠的顺序,所以假设第一个项目是您想要跳过的项目是一个不好的想法。在也就是说,如果您使用的是3.6+、和您知道要跳过第一个元素,可以use ^{} 安全地执行此操作,更改:
收件人:
^{pr2}$你不应该依赖字典有特定的顺序,所以python不允许你跳过字典中的“第一个”项,因为“第一个”取决于有一个特定的顺序。您可以将其强制转换为一个列表:
for key, value in list(largeSet.items())[1:]
,但这取决于字典的顺序是您所期望的。最好只执行for key, value in largeSet.items())
,然后在循环中检查它是否是您不想操作的项,如果是,continue
。或者用熊猫系列。在相关问题 更多 >
编程相关推荐