首页 > 解决方案 > 如何修复 jupyter notebook 中的内存错误

问题描述

我正在 jupyter notebook 中做一个 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 内核 i5 第 8 代笔记本电脑。

标签: pythonmachine-learningdeep-learningnlpjupyter-notebook

解决方案


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

如果列表中只有几个单词,它仍然会创建一个包含很多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存在于featuresjust doif word in features:

创建一个set只包含匹配词的条目会消除 test 所在的条目,False它也会消除value,只保留该词所属的键。


推荐阅读