scikit-learn not importing in jupyter notebook - python

I installed scikit-learn both using pip and conda commandas. Whenever I check the package, it shows that it is already installed but, whenever I try import it, it shows error.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
%matplotlib inline
ERROR:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-33-82d3fc6531ea> in <module>
3 import matplotlib.pyplot as plt
4 import seaborn as sns
----> 5 from sklearn.model_selection import train_test_split
6 get_ipython().run_line_magic('matplotlib', 'inline')
ModuleNotFoundError: No module named 'sklearn'
I have tried reinstalling it multiple times but it shows the same error

Make sure that if you install the sklearn package in the python version that you are working. For suppose if your system has two versions of python installed in it like both python 2 and python 3 then you have installed sklearn package in python 2 and executing your code in python 3 or vice-versa.

I think you don't have latest version of sklearn because in old version of sklearn
below code was not working.
from sklearn.model_selection import train_test_split
Please update your sklearn by following command
conda update scikit-learn

I got the same error while installing these.
What you can do is install all the packages using the command
conda install pandas
and all other packages similarly or all in once
conda install pandas numpy matplotlib scikit-learn
in your project directory.
And now you can open your jupyter notbook using command
jupyter notebook
in the same environment or I should say same project directory.

Related

Error when importing matplotlib in Python

I am new to python and am just getting started. I have a Jupyter Notebook from my university and have to plot something using matplotlib. All I find on the web is this:
import numpy as np
import matplotlib.pyplot as plt
but when I try that, it tells me
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-12-e0e1492b7973> in <module>
1 import numpy as np
----> 2 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
Any help and further tips are very welcome.
matplotlib has to be installed
pip3 install matplotlib
since you have the jupyter tag, installing it with the Anaconda Prompt is an option
conda install matplotlib

Error importing seaborn module in Jupyter Notebook

I'm trying to use the seaborn module in jupyter notebook,I alredy installed all the dependecies and the seaborn too,but when I try to run,it's said that it does not have a module seaborn installed
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-ed806232820c> in <module>
1 import pandas as pd
2 import matplotlib.pyplot as plt
----> 3 import seaborn as sns
ModuleNotFoundError: No module named 'seaborn'
Try restarting notebook first.
Make sure that seaborn is installed by same python interpreter used in the notebook.
Easiest way to be certain of this is to run
!pip install --user seaborn in the active notebook (! allows you to run shell command from your notebook)
Additional info for detecting the cause of the problem
You can check which interpreter is used by the notebook by running:
import sys
print(sys.executable)
I assume pip you used initially is not used by python you used to run the notebook (multiple python installations of the system or conda).
To specify which python is used for pip install you can run
<python-path> -m pip install seaborn where <python-path> is output of sys.executable.

Trouble with Cluster Module Spectral Cluster import of Scikit-Learn, install is functioning with other modules

I am trying to do Kmeans with sklearn and I am getting weird error that says:
Traceback (most recent call last):
File "kmean_test.py", line 2, in <module>
from sklearn.cluster import KMeans
File "C:\Python\lib\site-packages\sklearn\cluster\__init__.py", line 6, in <module>
from .spectral import spectral_clustering, SpectralClustering
ModuleNotFoundError: No module named 'sklearn.cluster.spectral'
Working on Windows, I tried importing in Jupyter and just running a simple script from command.
I have tested my install of Scikit-Learn with the following imports and they work fine.
from sklearn import datasets
from sklearn import naive_bayes
First bit of code that i get the traceback error looks like:
import cv2, numpy as np
from sklearn.cluster import KMeans
def visualize_colors(cluster, centroids):
...
I got this import code
from sklearn.cluster import KMeans
from two tutorial sources but perhaps it is the problem? I have made attempts to import otherways but been unsuccessful
Just in case it was the sklearn install and because of reading through other stackoverflow suggestions I have:
I have tried uninstall and reinstall of numpy (with and without mkl), pandas, scikit-learn, and scipy both from regular command and admin command.
I have tried update as well.
I have downloaded the wheels and run install as administrator.
I am new. Please be kind. I am new to Windows development as well, Ugh! So if it has something to do with some complicated Windows system setup and software install etc etc I may not follow. I have spent 2 days trying to solve this one on my own and finally decided to reach out.
Thanks for any help!
UPDATE:
As per suggested below I have confirmed I do have the most recent version of sklearn 0.21.3
import sklearn
print(sklearn.__version__)
0.21.3
For the record here are my versions of the others:
python --version
Python 3.7.3
import numpy
numpy.version.version
'1.17.2'
import scipy
scipy.version.version
'1.3.1'
import pandas as pd
pd.__version__
'0.24.2'
Finally I would like to mention that I have been able to successfully apply Kmeans to my project without sklearn, however, I am trying to access the % and RGB values of the means. I have found two people showing how to do it and BOTH use sklearn. See here end comment by nathancy on why I am trying to install sklearn to do this specific thing: How to find the average colour of an image in Python with OpenCV?
You have installed an older version of sklearn.
from sklearn.cluster import KMeans with work only if you have the latest sklearn version which is 0.21.3
To check the version use:
import sklearn
print(sklearn.__version__)
Finally install sklearn latest version:
pip install -U scikit-learn

Python 3 - ModuleNotFoundError: No module named 'xgboost'

I am a total newbie in Python 3 and programming in general so I looked at other peoples code and just for the beginning paste one example into Jupyter. But right at the beginning, I get an Error "ModuleNotFoundError: No module named 'xgboost'" Why does this not work?
import pandas as pd
import numpy as np
import re
import sklearn
import xgboost as xgb // error
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.tools as tls
import warnings
warnings.filterwarnings('ignore')
# Going to use these 5 base models for the stacking
from sklearn.ensemble import (RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier, ExtraTreesClassifier)
from sklearn.svm import SVC
from sklearn.cross_validation import KFold
I am assuming you are running Anaconda, because this is the first error you encountered. You need to install this package: https://anaconda.org/anaconda/py-xgboost because the code you copied uses it and needs it.
You will probably get a plotly error too, so install https://anaconda.org/plotly/plotly and remember to restart Jupyter (or the kernel at least).
If you are not running Anaconda, run pip install xgboost and pip install plotly.
I tried
pip install xgboost
and
pip3 install xgboost
But it doesn't work
##ModuleNotFoundError: No module named 'xgboost'
It worked in the Jupyter Notebook cell
import sys
!{sys.executable} -m pip install xgboost
Go to command prompt >> By typing "cmd" in your windows search engine.>> Please type "pip install xgboost".
Later, close your Jupyter notebook and open it again. Run the respective cell.
If you are still getting the error then :
Add a cell in Jupyter notebook and type "pip install xgboost". Run this cell. Now it will work.
Giving a very detailed answer since beginners might be here too. Hope this helps! Be motivated! You can do it!
conda install -c conda-forge xgboost

Tensorflow module exists but notebook gives error

Hi I am using Jupyter notebook to run my python script.I did all the steps to install tensorflow. When I do list env I see tensorflow installed but when I write a script using tensor flow. I get the following error.
ImportError Traceback (most recent call last)
<ipython-input-1-b2d6c7d62f65> in <module>()
2 import numpy as np
3 #import tflearn
----> 4 import tensorflow as tf
5 import tflearn
6 #from tflearn.data_utils import to_categorical
ImportError: No module named 'tensorflow'
Can anyone please help in how to debug
I have followed the following steps
1.conda create --name test python=3.5
2.activate test
3.conda install numpy matplotlib pandas jupyter notebook
4. conda install tensorflow scipy
all the installations were successfull. Except in hdf5 i got error saying revuild using conda > 2.0
If you're trying to import tensorflow in ipython, you should also make sure that ipython is installed in the same environment. And if you run
ipython notebook
This will launch the default ipython (/usr/bin/ipython). You don't want that. So, run venv/bin/ipython instead

Categories