我想尝试从apriori using python下载的apriori算法中分解一些行代码,因为当我尝试计算庞大的数据集时,它会给我的内存错误。 这是我刚刚找到的一些问题。
<?php namespace App\Observers;
class Quotation extends BaseObserver{
//you can override any of the methods if you wish
}
我想知道触发错误时返回var的实际大小,所以我尝试将这些代码细分为此。
def joinSet(itemSet, length):
"""Join a set with itself and returns the n-element itemsets"""
return set([i.union(j) for i in itemSet for j in itemSet if len(i.union(j)) == length])
所以我可以监控每一步,但是我的故障代码与原始代码没有相同的结果。
我还有什么想念? 如果我的方法出错,如果你能给我实际的解决方案,我将非常感激。 谢谢。
答案 0 :(得分:0)
我认为原文可能会返回一个集合,而您将返回一个列表。
def joinSet(itemSet, length):
"""Join a set with itself and returns the n-element itemsets"""
ret = []
for i in itemSet:
for j in itemSet:
if len(i.union(j)) == length:
ret.append(i.union(j))
return set(ret)
此外,我认为您可以通过以下原始编辑节省大量内存:
def joinSet(itemSet, length):
"""Join a set with itself and returns the n-element itemsets"""
return {i.union(j) for i in itemSet for j in itemSet if len(i.union(j)) == length}
这是一个集合理解需要python&gt; = 2.7