如何修复jupyter笔记本中的内存错误

2024-03-29 08:39:07 发布

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

我正在jupyter笔记本上做一个NLP项目,数据集涉及160000行。在运行给定的代码时,我得到一个内存错误

messages = list(zip(processed, Y))

# defined a seed for reproducibility
seed = 1
np.random.seed = seed
np.random.shuffle(messages)

# calling find_features function for each comments
featuresets = [(find_features(text), label) for (text, label) in messages] 

错误显示为-

<ipython-input-18-faca481e94f7> in find_features(message)
      3     features = {}
      4     for word in word_features:
----> 5         features[word] = (word in words)
      6 
      7     return features

MemoryError: 

有没有办法解决这个问题。 我正在运行windows 64位4gb RAM core i5第8代笔记本电脑


Tags: textinfornlp错误np笔记本jupyter
1条回答
网友
1楼 · 发布于 2024-03-29 08:39:07

不确定它是否能完全解决您的问题,但您似乎创建了一个带有布尔值的字典,该字典将单词搜索结果存储在列表/集合/任何内容中

如果列表中只有几个单词,它仍然会创建一个包含大量False值的大型字典,而您只需要True值(除非您需要知道哪些值已经过测试)

我将替换:

features = {}
for word in word_features:
   features[word] = (word in words)

features = set()
for word in word_features:
    if word in words:
        features.add(word)

或集合理解:

features = {word for word in word_features if word in words}

现在要测试word是否存在于features中,只需执行if word in features:

创建一个只包含匹配单词的set将删除测试所在的条目False,它还将删除,只保留单词所属的键

相关问题 更多 >