python - 如何修复 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 代笔记本电脑。
解决方案
不确定它是否会完全解决您的问题,但您似乎创建了一个带有布尔值的字典,该字典将搜索单词的结果存储在列表/集合/任何内容中。
如果列表中只有几个单词,它仍然会创建一个包含很多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
just doif word in features:
创建一个set
只包含匹配词的条目会消除 test 所在的条目,False
它也会消除value,只保留该词所属的键。
推荐阅读
- c++ - 使用更大的
() 在 priority_queue - python - 计算连字符平均长度的函数
- python - Selenium Python等待功能,最大等待时间不起作用
- angular - Angular 表单与异步管道绑定以设置模型
- javascript - 由于 Invariant Violaton,Expo Android Emulator 未加载:[React Intl] `Intl` API 必须在运行时可用,并且似乎不可用
- laravel - Laravel 7 Lumen,安装 ffmpeg 问题
- node.js - Google Cloud Functions Node.js 错误报告 - 未记录未处理的拒绝
- linux-kernel - 用于将 IPv6 数据报定向到内核的原始套接字
- sql - 无法为 Spring Boot 存储库中的数组运行 CASE WHEN
- react-native - 有没有办法通过 React Native 应用程序更改时区?