[英]Break down python process
我想嘗試從使用python從apriori下載的apriori算法中分解一些行代碼,因為當我嘗試計算大數據集時它會給我的內存錯誤。 這是我剛發現的一些問題。
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])
我想知道錯誤觸發時返回var的實際大小,所以我嘗試將這些代碼細分為此。
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 ret
所以我可以監控每一步,但我的故障代碼並沒有給出與原始代碼相同的結果。
我還有什么想念? 如果我的方法出錯,如果你能給我實際的解決方案,我將非常感激。 謝謝。
我想原文可能會返回一個集合,並且您將返回一個列表。
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> = 2.7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.