defaultdi的存储效率

2024-05-23 18:36:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试一些计算PMI的例子,尝试计算一些tweet消息(收集了~50k),如果发现实现algorithm的瓶颈在defaultdict(lambda : defaultdict(int)),我不知道为什么:

下面是一个例子,我分析了它,占用了大量的内存和时间

for term, n in p_t.items():
    positive_assoc = sum(pmi[term][tx] for tx in positive_vocab)
    negative_assoc = sum(pmi[term][tx] for tx in negative_vocab)
    semantic_orientation[term] = positive_assoc - negative_assoc

其中部件:

positive_assoc = sum(pmi[term][tx] for tx in positive_vocab)
negative_assoc = sum(pmi[term][tx] for tx in negative_vocab)

由于某种原因分配了大量内存。我假设对于不存在的值,返回0,因此传递给sum函数的数组非常大。你知道吗

我用简单的if value exist和变量sum_pos解决了这个问题。你知道吗

博客的整个实现:

pmi = defaultdict(lambda : defaultdict(int))
for t1 in p_t:
    for t2 in com[t1]:
        denom = p_t[t1] * p_t[t2]
        pmi[t1][t2] = math.log2(p_t_com[t1][t2] / denom)

semantic_orientation = {}
for term, n in p_t.items():
    positive_assoc = sum(pmi[term][tx] for tx in positive_vocab)
    negative_assoc = sum(pmi[term][tx] for tx in negative_vocab)
    semantic_orientation[term] = positive_assoc - negative_assoc

Tags: inforsemanticsumt1pmitermtx
2条回答

defaultdict将为丢失的每个键调用工厂函数。如果您在sum()中使用它,其中有许多键丢失,那么您确实会创建一大堆字典,这些字典会增长到包含更多键而不使用它们。你知道吗

切换到此处使用^{} method以防止创建对象:

positive_assoc = sum(pmi.get(term, {}).get(tx, 0) for tx in positive_vocab)
negative_assoc = sum(pmi.get(term, {}).get(tx, 0) for tx in negative_vocab)

注意,pmi.get()调用返回一个空字典,因此链式的dict.get()调用继续工作,如果没有与给定的term关联的字典,则可以返回默认的0。你知道吗

我喜欢Martjin的回答。。。但这也应该有效,你可能会发现它更具可读性。你知道吗

positive_assoc = sum(pmi[term][tx] for tx in positive_vocab if term in pmi and tx in pmi[term) negative_assoc = sum(pmi[term][tx] for tx in negative_vocab if term in pmi and tx in pmi[term)

相关问题 更多 >