【发布时间】: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