【问题标题】:Python sklearn GaussianNB : "MemoryError" but no leads on how to fixPython sklearn GaussianNB:“MemoryError”,但没有关于如何修复的线索
【发布时间】:2016-02-11 17:52:51
【问题描述】:

我正在运行以下代码来创建和拟合 GaussianNB 分类器:

features_train, features_test, labels_train, labels_test = preprocess()

### compute the accuracy of your Naive Bayes classifier
# import the sklearn module for GaussianNB 
from sklearn.naive_bayes import GaussianNB 
import numpy as np

### create classifier 
clf = GaussianNB()

### fit the classifier on the training features and labels    
clf.fit(features_train, labels_train)

在本地运行以上代码:

>>> runfile('C:/.../naive_bayes')
no. of Chris training emails: 4406
no. of Sara training emails: 4383
>>> clf
GaussianNB()

我相信这会检查出“preprocess()”,因为它成功加载了 features_train、features_test、labels_train、labels_test。

当我尝试 clf.score 或 clf.predict 时,我得到一个 MemoryError:

>>> clf.predict(features_test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\sklearn\naive_bayes.py", line 64, in predict
    jll = self._joint_log_likelihood(X)
  File "C:\Python27\lib\site-packages\sklearn\naive_bayes.py", line 343, in _joint_log_likelihood
    n_ij -= 0.5 * np.sum(((X - self.theta_[i, :]) ** 2) /
MemoryError
>>> clf.score(features_test,labels_test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\sklearn\base.py", line 295, in score
    return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
  File "C:\Python27\lib\site-packages\sklearn\naive_bayes.py", line 64, in predict
    jll = self._joint_log_likelihood(X)
  File "C:\Python27\lib\site-packages\sklearn\naive_bayes.py", line 343, in _joint_log_likelihood
    n_ij -= 0.5 * np.sum(((X - self.theta_[i, :]) ** 2) /
MemoryError

我认为这不是我的内存问题,因为我没有看到我的任务管理器上的 RAM 出现峰值,也没有接近我机器上的内存使用量。

我怀疑这与 Python 版本和库版本有关。

感谢您在诊断此问题方面的任何帮助。我可以根据需要提供更多信息。

【问题讨论】:

  • 听起来你在堆里烧,所以你不妨做一些类似于this
  • @MichalFrystacky 这么想,你不认为它与任何版本更新等有关吗?
  • 有可能,但证据似乎表明使用了太多数据。您可能遇到类似于this 的问题
  • @ximiki,4406 + 4383 是您使用的总大小数据吗?如果是这样,即使我对电子邮件一无所知,也不知道您如何对它们进行编码,我看不出内存如何成为问题。您使用 BoW 方法吗?也许你可以给我们一些关于你在preprocess 函数中做什么的见解。我知道你说你成功通过了它,但也许你预处理它的方式有什么东西让GaussianNB
  • 如果您担心sklearn 版本,您可以测试例如virtualenv 中的最新版本

标签: python scikit-learn


【解决方案1】:

我相信我是在网上阅读了一些相关帖子后回答了我的问题(没有使用之前回答过的 Stackoverflow 帖子)。

对我来说,关键是通过 Anaconda 简单地迁移到 64 位 Python。当在 32 位 Python 中运行的完全相同的代码在 64 位中重试时,所有与“MemoryError”有关的问题都已解决。据我所知,这是唯一改变的变量。

也许这不是一个非常令人满意的答案,但如果这个问题可以留给其他人在未来寻找完全相同的 sklearn MemoryError 问题,那就太好了。

【讨论】:

    【解决方案2】:

    我也参加了相同的 Udacity 课程,但我遇到了同样的问题。我安装了 Anaconda 64bits 并在 Spyder 中执行了脚本,一切都按预期进行

    【讨论】:

      猜你喜欢
      • 2019-11-03
      • 1970-01-01
      • 2016-06-24
      • 2019-02-22
      • 1970-01-01
      • 2018-09-21
      • 2020-12-11
      • 2020-10-07
      • 1970-01-01
      相关资源
      最近更新 更多