简体   繁体   English

无法与GaussianNB配合-ModuleNotFoundError:没有名为“ sklearn.utils._pprint”的模块

[英]unable to fit with GaussianNB - ModuleNotFoundError: No module named 'sklearn.utils._pprint'

When trying to run the .fit method of GaussianNB , I receive the following error: 尝试运行GaussianNB.fit方法时,出现以下错误:

ModuleNotFoundError: No module named 'sklearn.utils._pprint'

I am able to cross validate: 我能够交叉验证:

cv_result = cross_val_score(GaussianNB(), X_train, y_train, cv=kfold, scoring=scoring,n_jobs=njobs)

However the following doesn't work: 但是以下方法不起作用:

NB = GaussianNB()
NB.fit(X_train, y_train)

I am currently on an Anaconda Jupyter notebook and using the following version 我目前在Anaconda Jupyter笔记本上,并使用以下版本

jupyter-client==5.3.1
jupyter-console==6.0.0
jupyter-core==4.5.0

scikit-learn==0.21.3

Any idea on what is causing this and how to fix it? 关于导致此问题的原因以及如何解决的任何想法?

That command works fine on scikit-learn version 0.21.3 for me. 对于我来说,该命令在scikit-learn 0.21.3版本上运行良好。 It's likely that your ipython and related modules are installed in a different environment than scikit-learn. 您的ipython和相关模块可能安装在与scikit-learn不同的环境中。 You should be able to test this by making a script and executing it with python. 您应该能够通过编写脚本并使用python执行测试来进行测试。 If that works, but it doesn't work running in ipython console or jupyter notebooks, try reinstalling ipython, jupyter and scikit-learn with anaconda/pip. 如果可以,但不能在ipython控制台或jupyter笔记本中运行,请尝试使用anaconda / pip重新安装ipython,jupyter和scikit-learn。

To be specific, I believe that your error is due to NB.fit returning self . 具体来说,我相信您的错误是由于NB.fit返回self This makes the jupyter notebook trying to display the NB object via the __repr__ method, which in turn relies on sklearn.utils._pprint . 这使jupyter笔记本尝试通过__repr__方法显示NB对象,而该方法又依赖于sklearn.utils._pprint A quick fix would be to change 一个快速的解决办法是改变

NB.fit(x,y)

to

NB = NB.fit(x,y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM