简体   繁体   中英

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:

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

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. It's likely that your ipython and related modules are installed in a different environment than scikit-learn. You should be able to test this by making a script and executing it with 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.

To be specific, I believe that your error is due to NB.fit returning self . This makes the jupyter notebook trying to display the NB object via the __repr__ method, which in turn relies on sklearn.utils._pprint . A quick fix would be to change

NB.fit(x,y)

to

NB = NB.fit(x,y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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