Error when importing sklearn in python - ImportError - python

I get an error when importing sklearn in python.
I am using MacBook.
>>> import sklearn
Error:
> Traceback (most recent call last): File
> "<pyshell#1>", line 1, in <module>
> import sklearn File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/__init__.py",
> line 81, in <module>
> from . import __check_build # noqa: F401 ImportError: cannot import name '__check_build' from 'sklearn'
> (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/__init__.py)

According to the pypi page for sklearn, you should be using pip install scikit-learn instead of pip install sklearn

Your requirements.txt file needs the line
scikit-learn==0.22.1
or, if you're not storing requirements for the project, then
pip install scikit-learn
on the command line.

Related

Python importing statsmodels no module named 'scipy.linalg'

I've installed scipy and statsmodels, but encounter an error when attempting to import statsmodels:
$ pip install scipy
$ pip install statsmodels
$ python
>>> import statsmodels as sm
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pa354/software/miniconda3/lib/python3.7/site-packages/statsmodels/__init__.py", line 10, in <module>
from statsmodels.tools.sm_exceptions import (ConvergenceWarning, CacheWriteWarning,
File "/home/pa354/software/miniconda3/lib/python3.7/site-packages/statsmodels/tools/__init__.py", line 1, in <module>
from .tools import add_constant, categorical
File "/home/pa354/software/miniconda3/lib/python3.7/site-packages/statsmodels/tools/tools.py", line 8, in <module>
from scipy.linalg import svdvals
ModuleNotFoundError: No module named 'scipy.linalg'
Upgrade to the latest version of scipy with:
$ pip install scipy --upgrade

This is the error that I am getting while executing from sklearn import preprocessing, cross_validation, svm

Traceback (most recent call last):
File "C:/Python27/12.py", line 4, in <module>
from sklearn import preprocessing, cross_validation, svm
File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 57, in <module>
from .base import clone
File "C:\Python27\lib\site-packages\sklearn\base.py", line 10, in <module>
from scipy import sparse
ImportError: No module named scipy
Please help me resolve this.
Install this package using:
easy_install scipy
or
sudo apt-get install python-scipy
or
pip install scipy
or
conda install scikit-learn
if you are using windows refer:
https://stackoverflow.com/a/39577864/6633975
https://stackoverflow.com/a/32064281/6633975

import error in python hmmlearn

I am trying to install python hmmlearn library to build continuous HMM. I have installed all the dependencies and the hmmlearn library from GitHub. The installation completes successfully.
...
Processing dependencies for hmmlearn==0.2.1
Searching for scikit-learn==0.18.1
Best match: scikit-learn 0.18.1
Adding scikit-learn 0.18.1 to easy-install.pth file
Using /usr/local/lib/python2.7/dist-packages
Finished processing dependencies for hmmlearn==0.2.1
But in python, when I try to import GaussianHMM using
from hmmlearn.hmm import GaussianHMM
it gives some import error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "hmmlearn/hmm.py", line 22, in <module>
from .base import _BaseHMM
File "hmmlearn/base.py", line 13, in <module>
from . import _hmmc
ImportError: cannot import name _hmmc
The problem is in hmmlearn/setup.py which doesn't list utils as a submodule.
Add this line to setup.py config.add_subpackage("utils")
## -18,7 +18,7 ## def configuration(parent_package='', top_path=None):
include_dirs=[numpy.get_include()],
libraries=libraries,
)
-
+ config.add_subpackage("utils")
return config
if __name__ == '__main__':

ImportError: No module named 'numpy.ma'

The full error:
import matplotlib
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import matplotlib
File "R:\Python34\lib\site-packages\matplotlib\__init__.py", line 180, in <module>
from matplotlib.cbook import is_string_like
File "R:\Python34\lib\site-packages\matplotlib\cbook.py", line 34, in <module>
import numpy.ma as ma
ImportError: No module named 'numpy.ma'
numpy is imported normally.
How do I install numpy.ma?
I also faced the same situation today. I found that I had saved a file as numpy.py, so check the filenames in your folder.
Re-install the correct version of numpy.
download correct .whl file from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
pip install C:\Path\To\Wheel\Filename.whl # for example: numpy-1.9.2+mkl-cp34-none-win_amd64.whl
Use Your (via terminal) package manager and search.
Example on Ubuntu:
aptitude search numpy
and install package.
In my case:
apt-get install python-numpy

Attempted relative import in non-package when running a Python code

I an new to Python and am not sure how to run this code. I receive the following error:
mona$ python spectral.py
Traceback (most recent call last):
File "spectral.py", line 12, in <module>
from ..base import BaseEstimator, ClusterMixin
ValueError: Attempted relative import in non-package
>>> from ..base import BaseEstimator, ClusterMixin, TransformerMixin
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
I am not sure which other files from that repository should I download or which packages should I download to be able to run this code.
Why would you want to run that script?
Install scikit-learn.
sudo pip install scikit-learn
then you can import spectral clustering in your python script:
read:
http://scikit-learn.org/stable/auto_examples/cluster/plot_lena_segmentation.html#example-cluster-plot-lena-segmentation-py
and
http://scikit-learn.org/stable/auto_examples/cluster/plot_segmentation_toy.html#example-cluster-plot-segmentation-toy-py
You have to run it as a package not just the componant.
have you already installed the sklearn package and dependancies?
if not you should check out this page and go from there to install and configure the package.

Categories