ImportError: cannot import name 'GaussianProcess' from 'sklearn.gaussian_process' - scikit-learn

I was trying to kaggle kernel of Bayesian Hyperparam Optimization of RF. And I couldn't import sklearn.gaussian_process.GaussianProcess. Please help this poor scikit-learn newbie.
from sklearn.gaussian_process import GaussianProcess as GP
error:
Traceback (most recent call last):
File "C:/Users/Develop/PycharmProjects/reinforcement recommandation system/BNP/bayesianoptimization-of-random-forest.py", line 24, in <module>
from sklearn.gaussian_process import GaussianProcess as GP
ImportError: cannot import name 'GaussianProcess' from 'sklearn.gaussian_process' (C:\Users\Develop\PycharmProjects\reinforcement recommandation system\lib\site-packages\sklearn\gaussian_process\__init__.py)
Process finished with exit code 1

Depending on whether you need the regressor or classifier:
from sklearn.gaussian_process import GaussianProcessRegressor as GP
from sklearn.gaussian_process import GaussianProcessClassifier as GP
Also, have a look at the different modules

It seems that you have to use the old scikit-learn version 0.15-git
The documentation for sklearn.gaussian_process.GaussianProcess is located here:
https://scikit-learn.org/0.15/modules/generated/sklearn.gaussian_process.GaussianProcess.html
I just had the same problem, but I will move on to try to understand if sklearn.gaussian_process.GaussianProcessRegressor in the current version scikit-learn 1.0.2 will work for my purposes.

Related

ImportError: cannot import name 'joblib' from 'sklearn.externals'

I am trying to load my saved model from s3 using joblib
import pandas as pd
import numpy as np
import json
import subprocess
import sqlalchemy
from sklearn.externals import joblib
ENV = 'dev'
model_d2v = load_d2v('model_d2v_version_002', ENV)
def load_d2v(fname, env):
model_name = fname
if env == 'dev':
try:
model=joblib.load(model_name)
except:
s3_base_path='s3://sd-flikku/datalake/doc2vec_model'
path = s3_base_path+'/'+model_name
command = "aws s3 cp {} {}".format(path,model_name).split()
print('loading...'+model_name)
subprocess.call(command)
model=joblib.load(model_name)
else:
s3_base_path='s3://sd-flikku/datalake/doc2vec_model'
path = s3_base_path+'/'+model_name
command = "aws s3 cp {} {}".format(path,model_name).split()
print('loading...'+model_name)
subprocess.call(command)
model=joblib.load(model_name)
return model
But I get this error:
from sklearn.externals import joblib
ImportError: cannot import name 'joblib' from 'sklearn.externals' (C:\Users\prane\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\externals\__init__.py)
Then I tried installing joblib directly by doing
import joblib
but it gave me this error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in load_d2v_from_s3
File "/home/ec2-user/.local/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 585, in load
obj = _unpickle(fobj, filename, mmap_mode)
File "/home/ec2-user/.local/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 504, in _unpickle
obj = unpickler.load()
File "/usr/lib64/python3.7/pickle.py", line 1088, in load
dispatch[key[0]](self)
File "/usr/lib64/python3.7/pickle.py", line 1376, in load_global
klass = self.find_class(module, name)
File "/usr/lib64/python3.7/pickle.py", line 1426, in find_class
__import__(module, level=0)
ModuleNotFoundError: No module named 'sklearn.externals.joblib'
Can you tell me how to solve this?
You should directly use
import joblib
instead of
from sklearn.externals import joblib
It looks like your existing pickle save file (model_d2v_version_002) encodes a reference module in a non-standard location – a joblib that's in sklearn.externals.joblib rather than at top-level.
The current scikit-learn documentation only talks about a top-level joblib – eg in 3.4.1 Persistence example – but I do see a reference in someone else's old issue to a DeprecationWarning in scikit-learn version 0.21 about an older scikit.external.joblib variant going away:
Python37\lib\site-packages\sklearn\externals\joblib_init_.py:15:
DeprecationWarning: sklearn.externals.joblib is deprecated in 0.21 and
will be removed in 0.23. Please import this functionality directly
from joblib, which can be installed with: pip install joblib. If this
warning is raised when loading pickled models, you may need to
re-serialize those models with scikit-learn 0.21+.
'Deprecation' means marking something as inadvisable to rely-upon, as it is likely to be discontinued in a future release (often, but not always, with a recommended newer way to do the same thing).
I suspect your model_d2v_version_002 file was saved from an older version of scikit-learn, and you're now using scikit-learn (aka sklearn) version 0.23+ which has totally removed the sklearn.external.joblib variation. Thus your file can't be directly or easily loaded to your current environment.
But, per the DeprecationWarning, you can probably temporarily use an older scikit-learn version to load the file the old way once, then re-save it with the now-preferred way. Given the warning info, this would probably require scikit-learn version 0.21.x or 0.22.x, but if you know exactly which version your model_d2v_version_002 file was saved from, I'd try to use that. The steps would roughly be:
create a temporary working environment (or roll back your current working environment) with the older sklearn
do imports something like:
import sklearn.external.joblib as extjoblib
import joblib
extjoblib.load() your old file as you'd planned, but then immediately re-joblib.dump() the file using the top-level joblib. (You likely want to use a distinct name, to keep the older file around, just in case.)
move/update to your real, modern environment, and only import joblib (top level) to use joblib.load() - no longer having any references to `sklearn.external.joblib' in either your code, or your stored pickle files.
You can import joblib directly by installing it as a dependency and using import joblib,
Documentation.
Maybe your code is outdated. For anyone who aims to use fetch_mldata in digit handwritten project, you should fetch_openml instead. (link)
In old version of sklearn:
from sklearn.externals import joblib
mnist = fetch_mldata('MNIST original')
In sklearn 0.23 (stable release):
import sklearn.externals
import joblib
dataset = datasets.fetch_openml("mnist_784")
features = np.array(dataset.data, 'int16')
labels = np.array(dataset.target, 'int')
For more info about deprecating fetch_mldata see scikit-learn doc
none of the answers below works for me, with a little changes this modification was ok for me
import sklearn.externals as extjoblib
import joblib
for this error, I had to directly use the following and it worked like a charm:
import joblib
Simple
In case the execution / call to joblib is within another .py program instead of your own (in such case even you have installed joblib, it still causes error from within the calling python programme unless you change the code, i thought would be messy), I tried to create a hardlink:
(windows version)
Python> import joblib
then inside your sklearn path >......\Lib\site-packages\sklearn\externals
mklink /J ./joblib .....\Lib\site-packages\joblib
(you can work out the above using a ! or %, !mklink....... or %mklink...... inside your Python juptyter notebook , or use python OS command...)
This effectively create a virtual folder of joblib within the "externals" folder
Remarks:
Of course to be more version resilient, your code has to check for the version of sklearn is >= 0.23 again before hand.
This would be alternative to changing sklearn vesrion.
When getting error:
from sklearn.externals import joblib it deprecated older version.
For new version follow:
conda install -c anaconda scikit-learn (install using "Anaconda Promt")
import joblib (Jupyter Notebook)
I had the same problem
What I did not realize was that joblib was already installed!
so what you have to do is replace
from sklearn.externals import joblib
with
import joblib
and that is it
After a long investigation, given my computer setup, I've found that was because an SSL certificate was required to download the dataset.

Keras import loading neural network

The script normally works on a local machine, but it also fails when it is uploaded to the server. The program falls on the line:
from keras.models import model_from_json
All versions of libraries on a laptop and server are the same. Python 3.6.5, Keras 2.2.4, Tensorflow 1.5.0, Numpy 1.14.3
Error message:
(base) C:\classX5>python app.py
Using TensorFlow backend.
Traceback (most recent call last):
File "app.py", line 8, in <module>
from keras.models import model_from_json
File "C:\Anaconda3\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Anaconda3\lib\site-packages\keras\utils\__init__.py", line 27, in <mo
dule>
from .multi_gpu_utils import multi_gpu_model
File "C:\Anaconda3\lib\site-packages\keras\utils\multi_gpu_utils.py", line 7,
in <module>
from ..layers.merge import concatenate
File "C:\Anaconda3\lib\site-packages\keras\layers\__init__.py", line 4, in <mo
dule>
from ..engine.base_layer import Layer
File "C:\Anaconda3\lib\site-packages\keras\engine\__init__.py", line 8, in <mo
dule>
from .training import Model
File "C:\Anaconda3\lib\site-packages\keras\engine\training.py", line 21, in <m
odule>
from . import training_arrays
File "C:\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 8,
in <module>
from scipy.sparse import issparse
File "C:\Anaconda3\lib\site-packages\scipy\__init__.py", line 119, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Anaconda3\lib\site-packages\scipy\_lib\_ccallback.py", line 1, in <mo
dule>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
All versions of libraries on a laptop and server are the same. Python
3.6.5, Keras 2.2.4, Tensorflow 1.5.0, Numpy 1.14.3
But NOT the scipy package.
Just uninstall (pip3 uninstall scipy) and reinstall scipy package. It solved the problem for many people.
Posting my same answer here I posted on the linked question:
This is likely an issue with the SciPy Python module compatibility you have installed in your site-packages folder and the OS architecture you're running it on.
After digging in, to give the full background on this, first of all SciPy relies on having NumPy already installed. The SciPy wheel's setup.py file uses NumPy functionality to configure and install the wheel.
SciPy setup.py:
...
if __name__ == '__main__':
from numpy.distutils.core import setup
setup(**configuration(top_path='').todict())
Secondly, when just trying to use the wheel, if you run into this error, you can see after inspecting the wheel's files that the reason is the binary wheels have a naming convention where the shared object file, here it's called _ccallback_c.so, is instead named based on the architecture that the binary wheel supports. When trying to import the shared object by file name in /_lib/_ccallback.py it can't find it, hence this error (line 1 in /_lib/_ccallback.py) because, instead of being named _ccallback_c.so it's called _ccallback_c.cpython-36m-x86_64-linux-gnu.so or another architecture variation:
from . import _ccallback_c
These file names may be an artifact of having not run the NumPy setup process yet or something related to Cython, that I'm not quite sure about. But the easiest fix is to change the .whl extension to .zip and rename all those relevant .so files to not contain the architecture snippet. Then change .zip -> .whl and it should be good to go.

ImportError: cannot import name 'Graph'

I am on windows using Anaconda with python 3.6, I have installed keras by:
conda install -c conda-forge keras
and whene i try this code
from keras.models import Graph
I get this error message:
ImportError Traceback (most recent call last)
<ipython-input-4-74d2d1fd7bad> in <module>()
----> 1 from keras.models import Graph
ImportError: cannot import name 'Graph'
This is not a Python issue. This is because the latest version of keras has removed Graph module from models. You can surely check the documentation.
If you check this Github issue (Why Keras remove graph model?), you can use the following line of code:
from .legacy.models import Graph
However, it is suggested to use functional API instead. Please check The Functional API

Error while importing datasets from sklearn

import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import svm
it gives the following error-
Traceback (most recent call last):
File "/home/songoku/PycharmProjects/untitled/sklearn.py", line 4, in <module>
from sklearn import datasets
File "/home/songoku/PycharmProjects/untitled/sklearn.py", line 4, in <module>
from sklearn import datasets
ImportError: cannot import name 'datasets'
Rename /home/songoku/PycharmProjects/untitled/sklearn.py to something that differs from the SKLearn module name: sklearn.py
Try checking the version of sklearn first. The modules like data-sets and svm is not a part of sklearn packages (0.19 <) i.e less than version number 0.19.0.
You can check this on the command line: pip3 list
it shows you the version of all libraries installed by pip3.
Note: Always double check if you istalled the libraries as a part of anaconda or pip3

sklearn.linear_model not found in TensorFlow Udacity course

I'm following the instructions of the Deep Learning course by Google with TensorFlow. Unfortunately I'm stuck with this workbook right now.
I work in the docker vm with all the assignment code loaded as described here.
When I do all the imports everything works except for following line:
from sklearn.linear_model import LogisticRegression
it throws the following error:
>>> from sklearn.linear_model import LogisticRegression
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sklearn.linear_model
This SO answer sounds promising, but I did not find the source directory of sklearn.
Any help greatly aprreciated.
You can install and upgrade sklearn from the shell with pip. That may or may not be the problem - but at least you'll know its installed.
sudo pip install --upgrade scikit-learn
In your Jupyter notebook:
import pip
pip.main(['install', 'sklearn'])
Please take note that when you are writing your code, you'll import the sklearn package using import sklearn, but when installing the package it with, say, conda, you should do the following:
conda install scikit-learn

Resources