Importing sklearn.linear_model gives an error - python

I have used
from sklearn.linear_model import LinearRegression
and getting the following errors
Traceback (most recent call last):
File "F:/PyProjects/LRtest.py", line 2, in <module>
from sklearn.linear_model import LinearRegression
File "F:\PyProjects\venv\lib\site-packages\sklearn\__init__.py", line 75, in <module>
from .utils._show_versions import show_versions
File "F:\PyProjects\venv\lib\site-packages\sklearn\utils\_show_versions.py", line 12, in <module>
from ._openmp_helpers import _openmp_parallelism_enabled
ImportError: DLL load failed: The specified module could not be found.
As recommended on earlier questions:
I have uninstalled numpy, sklearn, scikit-learn, scipy and also reinstalled them.
But that does not seem to work for my problem.
Can you tell what does this mean? And also recommend how to solve this?
ImportError: DLL load failed: The specified module could not be found.
I am using
Windows 10, PyCharm Pro 2019-3, Python v3.6, and lib versions given in the image

The package is called scikit-learn, not sklearn. From inside of Python, it is called sklearn. How do you have an entry for sklearn in your package list there with version 0?
Try uninstalling 'sklearn'. You already have the real scikit-learn, so maybe it will do the right thing once the false package is removed.
If that doesn't work, I would create a new python environment from scratch and reinstall the right packages there...
https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
That way you know that you're starting with a clean slate.

First, uninstall numpy and scipy from your command prompt.
pip uninstall numpy
pip uninstall scipy
After that install numpy and scipy from this website.
https://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy

Related

Error when attempting to import sklearn in Jupyter Notebook [duplicate]

I am getting the following error while trying to import from sklearn:
>>> from sklearn import svm
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
from sklearn import svm
File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 16, in <module>
from . import check_build
ImportError: cannot import name check_build
I am using python 2.7, scipy-0.12.0b1 superpack, numpy-1.6.0 superpack, scikit-learn-0.11
I have a windows 7 machine
I have checked several answers for this issue but none of them gives a way out of this error.
Worked for me after installing scipy.
>>> from sklearn import preprocessing, metrics, cross_validation
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
from sklearn import preprocessing, metrics, cross_validation
File "D:\Python27\lib\site-packages\sklearn\__init__.py", line 31, in <module>
from . import __check_build
ImportError: cannot import name __check_build
>>> ================================ RESTART ================================
>>> from sklearn import preprocessing, metrics, cross_validation
>>>
So, simply try to restart the shell!
My solution for Python 3.6.5 64-bit Windows 10:
pip uninstall sklearn
pip uninstall scikit-learn
pip install sklearn
No need to restart command-line but you can do this if you want.
It took me one day to fix this bug. Hope this help.
After installing numpy , scipy ,sklearn still has error
Solution:
Setting Up System Path Variable for Python & the PYTHONPATH Environment Variable
System Variables: add C:\Python34 into path
User Variables: add new: (name)PYTHONPATH (value)C:\Python34\Lib\site-packages;
Usually when I get these kinds of errors, opening the __init__.py file and poking around helps. Go to the directory C:\Python27\lib\site-packages\sklearn and ensure that there's a sub-directory called __check_build as a first step. On my machine (with a working sklearn installation, Mac OSX, Python 2.7.3) I have __init__.py, setup.py, their associated .pyc files, and a binary _check_build.so.
Poking around the __init__.py in that directory, the next step I'd take is to go to sklearn/__init__.py and comment out the import statement---the check_build stuff just checks that things were compiled correctly, it doesn't appear to do anything but call a precompiled binary. This is, of course, at your own risk, and (to be sure) a work around. If your build failed you'll likely soon run into other, bigger problems.
I had the same issue on Windows. Solved it by installing Numpy+MKL from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy (there it's recommended to install numpy+mkl before other packages that depend on it) as suggested by this answer.
I had problems importing SKLEARN after installing a new 64bit version of Python 3.4 from python.org.
Turns out that it was the SCIPY module that was broken, and alos failed when I tried to "import scipy".
Solution was to uninstall scipy and reinstall it with pip3:
C:\> pip uninstall scipy
[lots of reporting messages deleted]
Proceed (y/n)? y
Successfully uninstalled scipy-1.0.0
C:\Users\>pip3 install scipy
Collecting scipy
Downloading scipy-1.0.0-cp36-none-win_amd64.whl (30.8MB)
100% |████████████████████████████████| 30.8MB 33kB/s
Requirement already satisfied: numpy>=1.8.2 in c:\users\johnmccurdy\appdata\loca
l\programs\python\python36\lib\site-packages (from scipy)
Installing collected packages: scipy
Successfully installed scipy-1.0.0
C:\Users>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>>
>>> import sklearn
>>>
If you use Anaconda 2.7 64 bit, try
conda upgrade scikit-learn
and restart the python shell, that works for me.
Second edit when I faced the same problem and solved it:
conda upgrade scikit-learn
also works for me
None of the other answers worked for me. After some tinkering I unsinstalled sklearn:
pip uninstall sklearn
Then I removed sklearn folder from here: (adjust the path to your system and python version)
C:\Users\%USERNAME%\AppData\Roaming\Python\Python36\site-packages
And the installed it from wheel from this site: link
The error was there probably because of a version conflict with sklearn installed somewhere else.
For me,
I was upgrading the existing code into new setup by installing Anaconda from fresh with latest python version(3.7)
For this,
from sklearn import cross_validation,
from sklearn.grid_search import GridSearchCV
to
from sklearn.model_selection import GridSearchCV,cross_validate
no need to uninstall & then re-install sklearn
try this:
from sklearn.model_selection import train_test_split
i had the same problem reinstalling anaconda solved the issue for me
In windows:
I tried to delete sklearn from the shell: pip uninstall sklearn, and re install it but doesn't work ..
the solution:
1- open the cmd shell.
2- cd c:\pythonVERSION\scripts
3- pip uninstall sklearn
4- open in the explorer: C:\pythonVERSION\Lib\site-packages
5- look for the folders that contains sklearn and delete them ..
6- back to cmd: pip install sklearn

How can I import numpy correctly?

I am using qiskit (I am an absolute beginner with some experience with Java and know basic python) but when I tried to run an example code which uses numpy it gave me this error. I tried reinstalling it but it gave me the same error.
I have tried installing and reinstalling it but it didnt work
import numpy as np
from cqc.pythonLib import CQCConnection, qubit
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute
from qiskit.tools.visualization import circuit_drawer
from qiskit.quantum_info import state_fidelity
from qiskit import BasicAer
Traceback (most recent call last):
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/__init__.py", line 40, in <module>
from . import multiarray
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/overrides.py", line 6, in <module>
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/nikhil/.local/lib/python3.6/site-packages/fghjkl.py", line 1, in <module>
import numpy as np
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/__init__.py", line 71, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using /usr/bin/python3.7),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'
Try to upgrade the numpy:
pip install --upgrade numpy
or uninstall it and reinstall it again:
pip uninstall numpy
pip install numpy

Unable to solve this error: ImportError [duplicate]

I am getting the following error while trying to import from sklearn:
>>> from sklearn import svm
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
from sklearn import svm
File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 16, in <module>
from . import check_build
ImportError: cannot import name check_build
I am using python 2.7, scipy-0.12.0b1 superpack, numpy-1.6.0 superpack, scikit-learn-0.11
I have a windows 7 machine
I have checked several answers for this issue but none of them gives a way out of this error.
Worked for me after installing scipy.
>>> from sklearn import preprocessing, metrics, cross_validation
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
from sklearn import preprocessing, metrics, cross_validation
File "D:\Python27\lib\site-packages\sklearn\__init__.py", line 31, in <module>
from . import __check_build
ImportError: cannot import name __check_build
>>> ================================ RESTART ================================
>>> from sklearn import preprocessing, metrics, cross_validation
>>>
So, simply try to restart the shell!
My solution for Python 3.6.5 64-bit Windows 10:
pip uninstall sklearn
pip uninstall scikit-learn
pip install sklearn
No need to restart command-line but you can do this if you want.
It took me one day to fix this bug. Hope this help.
After installing numpy , scipy ,sklearn still has error
Solution:
Setting Up System Path Variable for Python & the PYTHONPATH Environment Variable
System Variables: add C:\Python34 into path
User Variables: add new: (name)PYTHONPATH (value)C:\Python34\Lib\site-packages;
Usually when I get these kinds of errors, opening the __init__.py file and poking around helps. Go to the directory C:\Python27\lib\site-packages\sklearn and ensure that there's a sub-directory called __check_build as a first step. On my machine (with a working sklearn installation, Mac OSX, Python 2.7.3) I have __init__.py, setup.py, their associated .pyc files, and a binary _check_build.so.
Poking around the __init__.py in that directory, the next step I'd take is to go to sklearn/__init__.py and comment out the import statement---the check_build stuff just checks that things were compiled correctly, it doesn't appear to do anything but call a precompiled binary. This is, of course, at your own risk, and (to be sure) a work around. If your build failed you'll likely soon run into other, bigger problems.
I had the same issue on Windows. Solved it by installing Numpy+MKL from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy (there it's recommended to install numpy+mkl before other packages that depend on it) as suggested by this answer.
I had problems importing SKLEARN after installing a new 64bit version of Python 3.4 from python.org.
Turns out that it was the SCIPY module that was broken, and alos failed when I tried to "import scipy".
Solution was to uninstall scipy and reinstall it with pip3:
C:\> pip uninstall scipy
[lots of reporting messages deleted]
Proceed (y/n)? y
Successfully uninstalled scipy-1.0.0
C:\Users\>pip3 install scipy
Collecting scipy
Downloading scipy-1.0.0-cp36-none-win_amd64.whl (30.8MB)
100% |████████████████████████████████| 30.8MB 33kB/s
Requirement already satisfied: numpy>=1.8.2 in c:\users\johnmccurdy\appdata\loca
l\programs\python\python36\lib\site-packages (from scipy)
Installing collected packages: scipy
Successfully installed scipy-1.0.0
C:\Users>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>>
>>> import sklearn
>>>
If you use Anaconda 2.7 64 bit, try
conda upgrade scikit-learn
and restart the python shell, that works for me.
Second edit when I faced the same problem and solved it:
conda upgrade scikit-learn
also works for me
None of the other answers worked for me. After some tinkering I unsinstalled sklearn:
pip uninstall sklearn
Then I removed sklearn folder from here: (adjust the path to your system and python version)
C:\Users\%USERNAME%\AppData\Roaming\Python\Python36\site-packages
And the installed it from wheel from this site: link
The error was there probably because of a version conflict with sklearn installed somewhere else.
For me,
I was upgrading the existing code into new setup by installing Anaconda from fresh with latest python version(3.7)
For this,
from sklearn import cross_validation,
from sklearn.grid_search import GridSearchCV
to
from sklearn.model_selection import GridSearchCV,cross_validate
no need to uninstall & then re-install sklearn
try this:
from sklearn.model_selection import train_test_split
i had the same problem reinstalling anaconda solved the issue for me
In windows:
I tried to delete sklearn from the shell: pip uninstall sklearn, and re install it but doesn't work ..
the solution:
1- open the cmd shell.
2- cd c:\pythonVERSION\scripts
3- pip uninstall sklearn
4- open in the explorer: C:\pythonVERSION\Lib\site-packages
5- look for the folders that contains sklearn and delete them ..
6- back to cmd: pip install sklearn

Error when trying to import sklearn modules : ImportError: DLL load failed: The specified module could not be found

I tried to do the following importations for a machine learning project:
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
I got this error message:
Traceback (most recent call last):
File "C:/Users/Abdelhalim/PycharmProjects/ML/stock pricing.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 12, in <module>
from .utils.fixes import signature
File "C:\Python27\lib\site-packages\sklearn\utils\__init__.py", line 11, in <module>
from .validation import (as_float_array,
File "C:\Python27\lib\site-packages\sklearn\utils\validation.py", line 18, in <module>
from ..utils.fixes import signature
File "C:\Python27\lib\site-packages\sklearn\utils\fixes.py", line 291, in <module>
from scipy.sparse.linalg import lsqr as sparse_lsqr
File "C:\Python27\lib\site-packages\scipy\sparse\linalg\__init__.py", line 112, in <module>
from .isolve import *
File "C:\Python27\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
from .iterative import *
File "C:\Python27\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 7, in <module>
from . import _iterative
ImportError: DLL load failed: The specified module could not be found.
Please help I tried everything but nothing worked. I tried these solutions as well:
ImportError: DLL load failed: Le module spécifié est introuvable
ImportError: DLL load failed: The specified module could not be found
This line points to scipy.
from scipy.sparse.linalg import lsqr as sparse_lsqr
You can try:
pip uninstall scipy
pip install scipy
enjoy!
Reinstallation of scipy, numpy, and scikit-learn packages fixed the error in my case.
You should open up "C:\Python27\lib\site-packages\sklearn\utils\fixes.py", and edit the contents. There are two specific changes you should make:
First, copy-and-paste the contents of https://github.com/scikit-learn/scikit-learn/blob/74a9756fa784d1f22873ad23c8b4948c6e290108/sklearn/utils/fixes.py into the file "C:\Python27\lib\site-packages\sklearn\utils\fixes.py".
Second, replace the line if np_version < (1, 12, 0): with if np_version < (1, 12):.
More background info and detail available here, in a great answer from user DSM.
Install this numpy library instead of the one you use:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
I assume you have Intel Math Kernal Libary installed.
i've found a silly solution, similar to the #saggy ones: iteratively run the script from command line, if compare a "DLL error" look for a package/module/library/wattelapesca name, then pip uninstall thatPackage and re-install it
as a pseudocode:
notWorking = true
while( nonFunge ){
run_the_script_from_command_line()
output = get_last_cmd_output()
if( "ImportError: DLL load failed: blabla" in output ){
doomed_package = look_for_package_module_library_wattelapesca(output)
exec("pip uninstall " + doomed_package )
exec("pip install " + doomed_package )
}else # all ok, the script works
notWorking = false
}
For me uninstalling scipy in conda env and then reinstalling using pip works.
Uninstall: conda remove --force scipy
Install: pip install scipy
DLL missing can happen by a wide range of reasons. In your case it seems there is a mismatch between sklearn and its dependencies(Maybe different 32bit or 64bit installation of packages.). As different answers point out to different packages, a general way to find out dependencies is using:
pip show scikit-learn
and the output is:
Name: scikit-learn
Version: 0.23.1
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author: None
Author-email: None
License: new BSD
Location: c:\users\username\appdata\local\programs\python\python37\lib\site-
packages
Requires: joblib, numpy, threadpoolctl, scipy
So It's probable that the root problem returns to one of 'Requires' packages.
By the way the error lines also can point out which package causes error.
Try reinstalling these packages should solve the problem.
pip install --user --upgrade numpy
pip install --user --upgrade scipy
pip install --user --upgrade matplotlib
pip install --user --upgrade scikit-learn

Error importing sklearn (scikit learn package) in python [duplicate]

I'm trying to call a function from the cluster module, like so:
import sklearn
db = sklearn.cluster.DBSCAN()
and I get the following error:
AttributeError: 'module' object has no attribute 'cluster'
Tab-completing in IPython, I seem to have access to the base, clone, externals, re, setup_module, sys, and warning modules. Nothing else, though others (including cluster) are in the sklearn directory.
Following pbu's advice below and using
from sklearn import cluster
I get:
Traceback (most recent call last):
File "test.py", line 2, in <module>
from sklearn import cluster
File "C:\Python34\lib\site-packages\sklearn\cluster\__init__.py", line 6, in <module>
from .spectral import spectral_clustering, SpectralClustering
File "C:\Python34\lib\site-packages\sklearn\cluster\spectral.py", line 13, in <module>
from ..utils import check_random_state, as_float_array
File "C:\Python34\lib\site-packages\sklearn\utils\__init__.py", line 16, in <module>
from .class_weight import compute_class_weight, compute_sample_weight
File "C:\Python34\lib\site-packages\sklearn\utils\class_weight.py", line 7, in <module>
from ..utils.fixes import in1d
File "C:\Python34\lib\site-packages\sklearn\utils\fixes.py", line 318, in <module>
from scipy.sparse.linalg import lsqr as sparse_lsqr
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\__init__.py", line 109, in <module>
from .isolve import *
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
from .iterative import *
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 7, in <module>
from . import _iterative
ImportError: DLL load failed: The specified module could not be found.
I'm using Python 3.4 on Windows, scikit-learn 0.16.1.
You probably don't use Numpy+MKL, but only Numpy.
I had the same problem and reinstalling Numpy with MKL
pip install --upgrade --force-reinstall "numpy‑1.16.3+mkl‑cp37‑cp37m‑win32.whl"
fixed it.
Note: update the file to the latest version, possibly 64bit - see the list of available Windows binaries
Problem was with scipy/numpy install. I'd been using the (normally excellent!) unofficial installers from http://www.lfd.uci.edu/~gohlke/pythonlibs/. Uninstall/re-install from there made no difference, but installing with the official installers (linked from http://www.scipy.org/install.html) did the trick.
I am using anaconda got the same error as the OP, when loading Orange, or PlotNine.
I can't recall when this start to happen.
Tracing the dependency of Anaconda3\Lib\site-packages\scipy\special\_ufuncs.cp36-win32.pyd, libifcoremd.dll and libmmd.dll are missing in DependencyWalk. Searching them in anaconda root directry, they are located in both ICC_RT and one version of MKL package.
Adding Anaconda3\pkgs\mkl-2017.0.3-0\Library\bin to PATH, seems to fix SciPy and NumPy related DLL load failure, the above package starts to work again.
I still don't know how to fix this properly. Apparently the downside is that the MKL package could be updated and versions may change so does the path. In this aspect Its equally inconvenient as adding a non-managed package.
Reinstalling ICC_RT fixed the issue for me, libmmd.dll and the related dlls are automatically copied into anaconda3/library/bin afterwards, which is automatically added into PATH by activate command. All previous numpy/scipy related cant load DLL errors are gone now.
From the error log, it shows that scipy module is the most recent module fails to import
File "C:\Python34\lib\site-packages\sklearn\utils\fixes.py", line 318, in <module>
from scipy.sparse.linalg import lsqr as sparse_lsqr
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\__init__.py", line 109, in <module>
from .isolve import *
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
from .iterative import *
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 7, in <module>
from . import _iterative
ImportError: DLL load failed: The specified module could not be found.
I have the same error that show the same log, the problem'd gone when I uninstall/install scipy:
pip uninstall scipy
pip install scipy
Place this line on top of the python file
from sklearn import cluster
That should do it :))
For me what fixed it were these commands:
pip uninstall sklearn
pip uninstall scikit-learn
pip uninstall scipy
pip install scipy
pip install scikit-learnhere
I had the same issue and solved it by installing/updating the mkl package:
conda install mkl
or
pip install mkl
Just for full information, this also downgraded the following packages:
The following packages will be UPDATED:
mkl: 2017.0.4-h6d528fc_0 defaults --> 2018.0.3-1 defaults
The following packages will be DOWNGRADED:
numpy: 1.11.3-py34_0 defaults --> 1.10.1-py34_0 defaults
pandas: 0.19.2-np111py34_1 defaults --> 0.18.1-np110py34_0 defaults
scikit-learn: 0.18.1-np111py34_1 defaults --> 0.17-np110py34_1 defaults
scipy: 0.19.1-np111py34_0 defaults --> 0.16.0-np110py34_0 defaults
I struggled trying to figure this one out; tried to download and install the (unofficial) Numpy+MKL library from the website (risky/tedious?).
Ultimately found success by:
Login to command prompt using admin rights; how to here: https://superuser.com/questions/968214/open-cmd-as-admin-with-windowsr-shortcut
Uninstall existing/tangled version of Scipy & Numpy
pip uninstall scipy
pip uninstall numpy
Fresh install Scipy & Numpy
pip install scipy
pip install numpy
Run Jupyter notebook; it worked for me.
The message ImportError: DLL load failed: The specified module could not be found
informs that there is failure to identify and source the required DLL(s) to use the scikit-learn library; a fresh install of scipy/numpy probably enables a better routing of DLL connections called from Jupyter notebook code(s).
download microsoft visual c++ distribution
link : https://www.microsoft.com/en-in/download/details.aspx?id=53840
vc_redist.x64.exe
install and run this .exe file in your computer.. the DLL import module error will not appear after this
now it will work fine enjoy :)

Categories