gpt4 book ai didi

Python sklearn GaussianNB : "MemoryError" but no leads on how to fix

转载 作者:太空宇宙 更新时间:2023-11-03 16:57:01 31 4
gpt4 key购买 nike

我正在运行以下代码来创建并拟合 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版本和库版本的问题。

对于诊断此问题的任何帮助,我们表示赞赏。我可以根据需要提供更多信息。

最佳答案

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

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

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

关于Python sklearn GaussianNB : "MemoryError" but no leads on how to fix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35346581/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com