Cannot import sklearn in python - python

from sklearn import svm
I am getting the following error while importing sklearn modules.
/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/iterative.py in <module>()
9
10 from scipy.sparse.linalg.interface import LinearOperator
---> 11 from scipy.lib.decorator import decorator
12 from .utils import make_system
13
ImportError: No module named decorator
ImportError Traceback (most recent call last)
<ipython-input-6-e938be4cf50b> in <module>()
----> 1 from sklearn import svm
/usr/local/lib/python2.7/dist-packages/sklearn/__init__.py in <module>()
54 # process, as it may not be compiled yet
55 else:
---> 56 from . import __check_build
57 from .base import clone
58 __check_build # avoid flakes unused variable error
ImportError: cannot import name __check_build
How to resolve this ?

I had the same error time ago, it is caused because either the machine doesn't have the scipy package installed, or it has the wrong distribution (depending on the architecture or OS).
If you have pip in your computer try installing scipy with:
sudo pip install scipy
If you have it already try upgrading or reinstalling the package.
Edit:
Following the solution suggested by #erip, you can create a virtual environmentand test the solution keeping your python global site-packages directory clean:
virtualenv test # This creates a folder with the virtual env
source test/bin/activate
pip install scipy
pip install sklearn
To deactivate and remove the virtual env:
deactivate
rm -r test # Remove the folder

Related

How do I import Pandas on Apple M1 chip

When I call:
import pandas as pd
on tensorflow_macos (for M1 chip)
I get:
ImportError Traceback (most recent call last)
~/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/__init__.py in <module>
28 try:
---> 29 from pandas._libs import hashtable as _hashtable, lib as _lib, tslib as _tslib
30 except ImportError as e: # pragma: no cover
~/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/__init__.py in <module>
12
---> 13 from pandas._libs.interval import Interval
14 from pandas._libs.tslibs import (
ImportError: dlopen(/Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so, 2): no suitable image found. Did find:
/Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so: mach-o, but wrong architecture
/Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so: mach-o, but wrong architecture
The above exception was the direct cause of the following exception:
ImportError Traceback (most recent call last)
<ipython-input-3-94f55571b0d6> in <module>
1 import numpy as np
----> 2 import pandas as pd
3
4 import matplotlib.pyplot as plt
5
~/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/__init__.py in <module>
31 # hack but overkill to use re
32 module = str(e).replace("cannot import name ", "")
---> 33 raise ImportError(
34 f"C extension: {module} not built. If you want to import "
35 "pandas from the source directory, you may need to run "
ImportError: C extension: dlopen(/Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so, 2): no suitable image found. Did find:
/Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so: mach-o, but wrong architecture
/Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so: mach-o, but wrong architecture not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --force' to build the C extensions first.
I hope Xcode Command Line Tools is already installed. If not, please install it then follow the steps.
Step 1:miniforge
Install miniforge for arm64 (Apple Silicon) from miniforge GitHub. Miniforge enables installing python packages natively compiled for Apple Silicon.
Step 2: create Conda environment
Don’t forget to open a new session or to source your .zshrc after miniforge install and before going through this step.
Create an empty Conda environment, then activate it and install python 3.8. and all the needed packages. Please note numpy is unnecessary here as pandas already install it, but it will be overwritten in the last step with the version provided by Apple.
conda create --name mytf
conda activate mytf
conda install -y python==3.8.6
conda install -y pandas matplotlib scikit-learn jupyterlab
I have tested it. It will work after these steps.
As shown in the next post(https://github.com/apple/tensorflow_macos/issues/6), it seems that the solution is to install pandas from conda until they fix this problem.
To install pandas:
conda install pandas
Or installing directly from the source:
https://github.com/pandas-dev/pandas#installation-from-sources
Follow these steps -
Important With Tensorflow Installation
With miniforge installation do this -
conda install numpy scipy scikit-learn pandas matplotlib seaborn
Use Anaconda visual UI to install into the conda environment you created. You just select your environment and search for pandas.

ModuleNotFoundError: No module named 'xgboost.sklearn'

I'm trying to import xgboost into jupyter-notebook but get the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-9-a585b270d0df> in <module>
1 import pandas as pd
2 import numpy as np
----> 3 import xgboost
~/.local/lib/python3.6/site-packages/xgboost/__init__.py in <module>
14 from . import tracker # noqa
15 from .tracker import RabitTracker # noqa
---> 16 from . import dask
17 try:
18 from .sklearn import XGBModel, XGBClassifier, XGBRegressor, XGBRanker
~/.local/lib/python3.6/site-packages/xgboost/dask.py in <module>
31 from .training import train as worker_train
32 from .tracker import RabitTracker
---> 33 from .sklearn import XGBModel, XGBClassifierBase, xgboost_model_doc
34
35 # Current status is considered as initial support, many features are
ModuleNotFoundError: No module named 'xgboost.sklearn'
I've downloaded sklearn as well as sci-kit learn and they work fine...
Any idea what the problem is?
You might need to install your packages properly.
For best practice, you'll need to use a conda environment. Check out how it works here: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
Once you have created your environment, activate it and then install all the packages you need. Presumably, you'll have to run the command:
conda install -c conda-forge xgboost
pip install -U scikit-learn
To install your machine learning packages.
XGBOOST is in xgboost module: It should be imported as:
`from xgboost import XGBRegressor`

CVXPY - ImportError: No module named fastcache

I am trying to generate a python(.egg) file of cvxpy and import it. Used the below to generate the .egg file for https://github.com/cvxgrp/cvxpy:
python setup.py bdist_egg
While trying to import cvxpy I am getting the below error,
<>/cvxpy-1.0.8-py2.7-macosx-10.12-x86_64.egg/cvxpy/__init__.py in <module>()
16
17 __version__ = "1.0.8"
---> 18 from cvxpy.atoms import *
19 from cvxpy.constraints import NonPos, Zero, SOC, PSD
20 from cvxpy.expressions.variable import Variable
<>/cvxpy-1.0.8-py2.7-macosx-10.12-x86_64.egg/cvxpy/atoms/__init__.py in <module>()
<>/cvxpy-1.0.8-py2.7-macosx-10.12-x86_64.egg/cvxpy/atoms/geo_mean.py in <module>()
<>/cvxpy-1.0.8-py2.7-macosx-10.12-x86_64.egg/cvxpy/atoms/atom.py in <module>()
<>/cvxpy-1.0.8-py2.7-macosx-10.12-x86_64.egg/cvxpy/expressions/constants/__init__.py in <module>()
<>/cvxpy-1.0.8-py2.7-macosx-10.12-x86_64.egg/cvxpy/expressions/constants/constant.py in <module>()
ImportError: No module named fastcache
I am new to python, any pointers / refernces to fix this is really appreciated.
Have you installed ‘CVXPY’ properly and tested it using ‘nose’?
Following is an excerpt from cvxpy page https://www.cvxpy.org/install/index.html:
Install Anaconda.
Install cvxpy with conda.
conda install -c conda-forge lapack
conda install -c cvxgrp cvxpy
Test the installation with nose.
conda install nose
nosetests cvxpy

Shapely OSError: Could not find lib c or load any of its variants []

I'm just trying to use the demo code. I run the following in Jupyter Notebook:
from shapely.geometry import shape
Which gives me the following:
OSError Traceback (most recent call last)
<ipython-input-4-cf5b4d0962ea> in <module>()
----> 1 from shapely.geometry import shape
/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/geometry/__init__.py in <module>()
2 """
3
----> 4 from .base import CAP_STYLE, JOIN_STYLE
5 from .geo import box, shape, asShape, mapping
6 from .point import Point, asPoint
/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/geometry/base.py in <module>()
7 from ctypes import pointer, c_size_t, c_char_p, c_void_p
8
----> 9 from shapely.coords import CoordinateSequence
10 from shapely.ftools import wraps
11 from shapely.geos import lgeos, ReadingError
/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/coords.py in <module>()
6 from ctypes import byref, c_double, c_uint
7
----> 8 from shapely.geos import lgeos
9 from shapely.topology import Validating
10
/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/geos.py in <module>()
81 _lgeos = load_dll('geos_c', fallbacks=alt_paths)
82
---> 83 free = load_dll('c').free
84 free.argtypes = [c_void_p]
85 free.restype = None
/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/geos.py in load_dll(libname, fallbacks, mode)
59 raise OSError(
60 "Could not find lib {0} or load any of its variants {1}.".format(
---> 61 libname, fallbacks or []))
62
63 _lgeos = None
OSError: Could not find lib c or load any of its variants [].
However, if I run from the interpreter, everything is fine.
Any idea what's going on?
I simply uninstalled Shapely and re-installed it to fix the issue.
python -m pip uninstall shapely
python -m pip install shapely
You may try to reset the environment variable DYLD_FALLBACK_LIBRARY_PATH:
export DYLD_FALLBACK_LIBRARY_PATH=$(HOME)/lib:/usr/local/lib:/lib:/usr/lib
Source
Try this may help you:
pip install --upgrade --force-reinstall shapely
For macOS users:
brew install geos
pip3 install shapely --upgrade
The way I fixed it:
apk add geos libc-dev musl-dev
pip install Shapely
For me, only works after installing Shapely from the shapely lib and executing the following command from Anaconda Prompt:
pip install Shapely-1.7.1-cp37-cp37m-win_amd64.whl --force-reinstall
This forces a re-install of shapely.
I had to do a conda update --all to fix this.
#user2977865 and #anothernode are correct in their approach but it may not work for everyone.
By default, shapely will look for libraries in the DYLD_PATH.
I had it set to DYLD_LIBRARY_PATH=/usr/local/lib/:/usr/local/mysql/lib/
But these libraries are placed in /usr/lib.
So I had to modify it as follows:
export DYLD_LIBRARY_PATH=/usr/lib/:/usr/local/mysql/lib/
Bonus note: Ensure that your environment variables have been set by restarting terminal and clearing cache of any IDEs you might be using.
I had a very similar issue running code in aws lambda with the error message:
OSError: Could not find lib c or load any of its variants ['libc.musl-x86_64.so.1'].
I added musl-dev to my container installation and adapted the symlink:
ln -s /usr/lib/x86_64-linux-musl/libc.so /lib/libc.musl-x86_64.so.1

Tensorflow installation: ImportError on sklearn - undefined symbol: PyFPE_jbuf

Following the Udacity course on Deep Learning with TensorFlow, and hit a roadblock here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/udacity/1_notmnist.ipynb
I tried to get the installation running, but the initial imports didn't work out so well. Specifically, this import error on a certain file:
/tensorflow/lib/python2.7/site-packages/sklearn/linear_model/cd_fast.so
Running tensorflow in a virtualenv installation on Ubuntu 14.04, should that help.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-3-72264e7a4df2> in <module>()
9 from IPython.display import display, Image
10 from scipy import ndimage
---> 11 from sklearn.linear_model import LogisticRegression
12 from six.moves.urllib.request import urlretrieve
13 from six.moves import cPickle as pickle
.../tensorflow/lib/python2.7/site-packages/sklearn/linear_model/__init__.py in <module>()
15 from .least_angle import (Lars, LassoLars, lars_path, LarsCV, LassoLarsCV,
16 LassoLarsIC)
---> 17 from .coordinate_descent import (Lasso, ElasticNet, LassoCV, ElasticNetCV,
18 lasso_path, enet_path, MultiTaskLasso,
19 MultiTaskElasticNet, MultiTaskElasticNetCV,
.../tensorflow/lib/python2.7/site-packages/sklearn/linear_model/coordinate_descent.py in <module>()
27 from ..utils import ConvergenceWarning
28
---> 29 from . import cd_fast
30
31
ImportError: .../tensorflow/lib/python2.7/site-packages/sklearn/linear_model/cd_fast.so: undefined symbol: PyFPE_jbuf
Anyone here who knows how to proceed? (Maybe this is a very simple error - I'm quite the newbie to this)
UPDATE:
Everything works fine when I run the iPython Notebooks from the usual terminal, as opposed to running them from the tensorflow virtual environment (as I had done when I first posted this question). I do not understand why this happened, though.
I also had trouble with sklearn in working with tensor flow. I had dowload it and install latest version manually to the TSNE module to work. Perhaps this will work for you too?
Step 1: Install Cython manually - you will version > 0.21
mkdir cython
cd cython
wget http://cython.org/release/Cython-0.24.zip
unzip Cython-0.24.zip
cd Cython-0.24
sudo python setup.py install
Step 2: Install latest version of skilearn

Categories