ImportError: No module named naive_bayes - python

I just installed sklearn, my program runs no problem when I import it into the code. However, whenever I try to access the naive_bayes module, I get this error:
ImportError: No module named naive_bayes
Here's how I'm importing it:
from sklearn.naive_bayes import GaussianNB
Not sure where I'm going wrong, any help is much appreciated!

In the spirit of "turn it off, and turn it back on again" solutions, and given the fact that you're getting a Module has no attribute: __version__ when you try and print the scikit-learn version (which should be defined in any self-respecting Python module), I'm going to recommend you uninstall and reinstall scikit-learn:
pip uninstall sklearn
pip install sklearn
Run the following commands, and compare to the output provided, to make sure the package is behaving the way it should:
>>> import sklearn
>>> print(sklearn.__version__)
0.19.0
>>> print(sklearn.__file__)
/usr/local/lib/python3.6/site-packages/sklearn/__init__.py
>>>
The output from printing sklearn.__file__ does not need to match exactly, but it should at least print some location on your computer.
Also, check to make sure your pip matches your python. This depends on what platform you're on, but this mix of version 2 and version 3 sometimes creates a nasty cocktail. Executing pip --version should tell you which version of Python it is tied to.
$ which pip3
/usr/local/bin/pip3
$ pip3 --version
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)
$ which python3
/usr/local/bin/python3
You should be OK with Python version 2, so long as things match.
(Soap box moment: move to Python 3 if you don't have a really good reason for sticking with Python 2!)

It seems that the sklearn installation does not include the naive_bayes in your installation. If the sklearn install correctly, it would say something like
ImportError: No module named 'sklearn.naive_bayes2'
However, in your case, the import error shows a bit different output. Please check the version and run the test file.

I had the same problem while installing sklearn and scikit-learn through pip.
I fixed the issue through the following steps
pip uninstall sklearn (if already installed)
pip uninstall scikit-learn( if already installed)
git clone scikit-learn
cd scikit-learn
python setup.py install
Hope this will help you.

Related

Module Not Found Error after library installation

I am trying to use the Scratch library.
For example:
from scratch.probability import normal_cdf
from scratch.linear_algebra import Vector, dot
#...
I installed Scratch using pip install scratch but I receive this error: ModuleNotFoundError: No module named 'scratch'.
Can you help me?
pip install scratch will install this: https://pypi.org/project/scratch/
But I guess that what you want to use is that: https://github.com/joelgrus/data-science-from-scratch
The second one is not meant to be installed through pip but used directly in a clone of the project as documented in its README.
About pip install scratch-probability (https://pypi.org/project/scratch-probability/#description), as it's not documented, you need to go check the content of the tarball to know how the package inside are named (if they are what's expected).

XGBoostError: sklearn needs to be installed in order to use this module ( GCP Datalab)

I am trying to use Xgboost in GCP datalab. I have already installed sklearn but I keep getting the error :
" XGBoostError: sklearn needs to be installed in order to use this
module"
Below is the code I used:
import sklearn
!pip3 install xgboost
from xgboost.sklearn import XGBClassifier
model = XGBClassifier()
I have tried using Python v 2.7 instead, but no luck...does anyone know how to solve this issue in GCP Datalab?
I also faced the same issue, on python 3.7 32bit on ipython.
Solution: Uninstall the xgboost package by pip uninstall xgboost on terminal/cmd. Cross-check on the your console if you cannot import it. Now again install xgboost pip install xgboost or pip install xgboost-0.81-cp37-cp37m-win32.whl, given that you have already installed sklearn, it will work on newer console session.
xgboost wheel Link: https://pypi.org/project/xgboost/#files
I got the same error with a more complicated project, after releasing a new version suddenly it failed.
luckily in my case, I had docker images for each version, and was able to use pip freeze to see what changed.
In both version I used xgboost==0.81
In the version that worked I had scikit-learn==0.21.3 and in the new version it was scikit-learn==0.22
surprisingly enough, that's now what caused the issue. I've tried to uninstall xgboost like it was suggested here and reverted scikit-learn to the version is was originally on, and still no luck.
what did cause the issue was an update of numpy from 1.17.4 to 1.18.0.
reverting it solved it for me (not sure why)
this was python 3.6 on ubuntu
For me un- then re-installing first sklearn and then xgboost did the trick

Failed to import WordCloud from wordcloud

I installed wordcloud package for my 64 bit windows 10 system with anaconda python 3.5
using python -m pip install D:\folder\wordcloud-1.5.0-cp35-cp35m-win_amd64.whl
command.
It has been successfully installed.
But when I am trying to import from wordcloud import WordCloud,STOPWORDS, I am getting error.Please check this:
Has it something to do with the path of wordcloud-1.5.0-cp35-cp35m-win_amd64.whl file as I have placed it in some random folder?
Thank you in advance.
The simplest way to trace the error is to import the package (which apparently works) with import wordcloud and then list all members of the package with help(wordcloud). Check if the version and functions match your expectations. Check if it also matches the documentation of your package.
I suspect that the name of the WordCloudclass was simply changed.
If you miss functions then your package is broken. If the version number is wrong then you downloaded an old package. Use the Anaconda Navigator that usually comes with Anaconda to reinstall the package. If you do not have the Navigator use whatever package manager you have.

No module named 'gensim.sklearn_api'

I try to use
from gensim.sklearn_api import W2VTransformer
and get
ImportError: No module named 'gensim.sklearn_api'
I used
import gensim
import sklearn
from sklearn.base import BaseEstimator, TransformerMixin
and get the same.
In sklearn_api.w2vmodel – Scikit learn wrapper for word2vec model I could find no advice.
How to install gensim.sklearn_api?
#tursunWali
Class names seem to have changed from v1.0.5. Try calling TFIDF and other methods by prefixing them with do_, e.g. hero.do_tfidf.
Here's a minimal example:
df['pca']=(df['text'].pipe(hero.clean).pipe(hero.do_tfidf).pipe(hero.do_pca))
You can see all the class names in the package source code.
If you're using vanilla Python, pip install -U gensim.
After importing the "texthero" library. Its shown error message:
ModuleNotFoundError: No module named 'gensim.sklearn_api'
I have tried to install 'gensim.sklearn_api', but no such module available yet.
Also created the new anaconda environment for an older version of python-like 3.6 and 3.7 but it's has shown the same error message.
Finally, I have installed the older version of texthero and its work
pip install texthero==1.0.5
The older version of texthero==1.0.5 is very much compatible with python version like 3.6, 3.7 and 3.8
For me it was the version, try: pip install "gensim==3.8.3".

Python: Why can't I import svm? [duplicate]

Using Python 2.7 with scikit-learn 0.14 package. It runs well on some examples from the user guild expect the Linear Models.
Traceback (most recent call last):
File "E:\P\plot_ols.py", line 28, in <module>
from sklearn import datasets, linear_model
File "C:\Python27\lib\site-packages\sklearn\linear_model\__init__.py", line 12, in <module>
from .base import LinearRegression
File "C:\Python27\lib\site-packages\sklearn\linear_model\base.py", line 29, in <module>
from ..utils.sparsefuncs import mean_variance_axis0, inplace_column_scale
ImportError: cannot import name inplace_column_scale
Thank you~
I was able to fix this by going to my python folder and deleting the file:
python27\Lib\site-packages\sklearn\utils\sparsefuncs.pyd
My guess is that the problem was:
An older version of scikit-learn implemented sparsefuncs as a windows DLL
The current version implements it as a python file
If you install a new version on top of an old version it does not delete the old DLL
When you try to import, Python uses the pyd in preference to the py implementation
But the old implementation did not include this function
This suggests that there might be bigger problems caused by installing a new version and it might be wise to delete the whole sklearn directory before reinstalling the new version.
I encountered the same issue in Mac Os.
I solved it by deleting the file manually:
rm /usr/local/lib/python2.7/site-packages/sklearn/utils/sparsefuncs.so
Uninstalling scikit-learn and reinstalling it was the only option that worked for me:
pip uninstall scikit-learn
pip install scikit-learn
I solve this problem by :
pip uninstall scikit-learn
and don't forget to rm the 'sklearn' folder in the python 'site-packages'
rm -rf /path/Python-2.7.5/lib/python2.7/site-packages/sklearn/
then reinstall the package:
pip install scikit-learn
On a related note, this has been posted as a bug on the official Github page, along with some additional solutions, basically suggesting the same solutions as above. Long story short: run a make clean to get rid of the .so file.
I had the same problem. I had originally installed scikit-learn by:
sudo apt-get install python-sklearn
When none of the other solutions posted here worked, I decided to uninstall my version of scikit-learn and reinstall it:
pip2 install --user --install-option="--prefix=" -U scikit-learn
Used pip2 because I have two versions of python, so I use scikit-learn in Python 2.7
same problem happened with can not import _safe_split, have a look
http://stackoverflow.com/questions/41024001/importerror-cannot-import-name-safe-split
https://github.com/scikit-learn/scikit-learn/issues/7582
EDIT: check comment by Andreas Mueller

Categories