sklearn model_selection Error: ImportError: cannot import name '_approximate_mode' - scikit-learn

I'm trying to import sklearn model_selection but I'm getting the following error:
ImportError Traceback (most recent call last)
<ipython-input-219-6bc6f0a77ca9> in <module>()
----> 1 import sklearn.model_selection
~/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/__init__.py in <module>()
----> 1 from ._split import BaseCrossValidator
2 from ._split import KFold
3 from ._split import GroupKFold
4 from ._split import StratifiedKFold
5 from ._split import TimeSeriesSplit
~/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_split.py in <module>()
21
22 from ..utils import indexable, check_random_state, safe_indexing
---> 23 from ..utils import _approximate_mode
24 from ..utils.validation import _num_samples, column_or_1d
25 from ..utils.validation import check_array
ImportError: cannot import name '_approximate_mode'
I tried uninstalling scikit-learn and reinstalling it but I get the same error. My current version of scikit-learn is 0.21.2.
Thanks for the help!

I shutdown the kernel and restarted it again. Now it's working fine! Thanks!!

You are using python notebook. I found that error once when I have installed the package before importing. I have tested this at Google AI notebook and worked well.
Save the notebook -> shutdown the kernel -> close and open the notebook again.

You can restart the kernel or use the importlib library
from importlib import reload
sklearn= reload(sklearn)

Related

Cannot import name 'network' from 'tensorflow.python.keras.engine'

When trying to load BERT QA I get the following ImportError:
"Cannot import name 'network' from 'tensorflow.python.keras.engine'"
The full error log follows below
Following this post,
ImportError: cannot import name 'network' from 'tensorflow.python.keras.engine'
I have tried the following steps,
pip uninstall tf-agents
pip install tf-agents-nightly
and then in Python,
from tf_agents.environments import suite_gym
However, this did not resolve the problem. Any suggestions would be very welcome!
In case it is helpful, I'm running TensorFlow version 2.3.0. Also, the script init.py referred to below does exist, however the file is empty, i.e. 0 bytes.
ImportError Traceback (most recent call last)
<ipython-input-2-323bac0cb5ba> in <module>
----> 1 from bert_qa import squad
~/anaconda3/lib/python3.8/site-packages/bert_qa/squad.py in <module>
34 from . import model_training_utils
35 from . import bert_modeling as modeling
---> 36 from . import bert_models
37 from . import optimization
38 from . import input_pipeline
~/anaconda3/lib/python3.8/site-packages/bert_qa/bert_models.py in <module>
25 from . import bert_modeling
26 from . import weighted_sparse_categorical_crossentropy
---> 27 from . transformer_encoder import TransformerEncoder
28 from . albert_transformer_encoder import AlbertTransformerEncoder
29 from . import bert_classifier
~/anaconda3/lib/python3.8/site-packages/bert_qa/transformer_encoder.py in <module>
22 import tensorflow as tf
23
---> 24 from tensorflow.python.keras.engine import network # pylint: disable=g-direct-tensorflow-import
25
26 from . import activations
ImportError: cannot import name 'network' from 'tensorflow.python.keras.engine' (/home/user810643/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/__init__.py)
Install tf-agents 0.6.0 version for Tebsorflow 2.3.
pip install tf-agents==0.6.0
Looks like the issue was caused due to TF-Agents and Tensorflow version incompatibility. For more information, please take a look at this release notes. Thanks!

Tensorflow Transformer Colab: AttributeError: module 'tensorflow_core' has no attribute 'compiler'

I tried to import the packages in the Transformer code in Transformer Colab Notebook. Is there a updated tensorflow core version?
from __future__ import absolute_import, division, print_function, unicode_literals
try:
!pip install tf-nightly-2.0-preview
except Exception:
pass
import tensorflow_datasets as tfds
import tensorflow as tf
import time
import numpy as np
import matplotlib.pyplot as plt
I got the following error.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-22c51fa8bb84> in <module>()
5 except Exception:
6 pass
----> 7 import tensorflow_datasets as tfds
8 import tensorflow as tf
9
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/__init__.py in <module>()
96
97 # We still need all the names that are toplevel on tensorflow_core
---> 98 from tensorflow_core import *
99
100 # These should not be visible in the main tf module.
AttributeError: module 'tensorflow_core' has no attribute 'compiler'
The error is from not having tensorflow datasets installed. Use pip3 install tensorflow-datasets for installing and run the script again. Here is a useful TF-datasets offical link.

I am getting error when importing torch and torch vision

I installed torch and torchvision using pip3 on MAC. When I imported the same, getting the below error.
Environment:
OS : macOS High Sierra
Python : 3.7
pip : 3
Pytorch 1.1
code:
import torch
import torchvision
Error:
ImportError Traceback (most recent call
last) in
1 import numpy as np
2 from glob import glob
----> 3 import torch
4 import torchvision
5 from torchvision import datasets
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/init.py
in
77 del _dl_flags
78
---> 79 from torch._C import *
80
81 all += [name for name in dir(_C)
ImportError:
dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/_C.cpython-37m-darwin.so,
9): Library not loaded: /usr/local/opt/libomp/lib/libomp.dylib
Referenced from:
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/lib/libshm.dylib
Reason: image not found
Any help on solving this error would be appreciated.
i think
$ brew install libomp
can help u, cause i solve the same problem by it.
according
github-issue-"libomp.dylib can't be loaded"

Python module incompatibilities

I have just installed gdal into my Python3 root environment using conda:
conda install -c conda-forge gdal
Installation went fine with the usual updates of some dependencies. Now returning to some of my scripts, both netCDF4 and mpl_toolkits.basemap have stopped working. These are the errors I get:
from netCDF4 import Dataset
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-7-09694a7f0e01> in <module>()
----> 1 from netCDF4 import Dataset
2 import numpy as np
3 import matplotlib.pyplot as plt
4 from scipy.interpolate import griddata
5 from mpl_toolkits.basemap import Basemap
/anaconda3/lib/python3.6/site-packages/netCDF4/__init__.py in <module>()
1 # init for netCDF4. package
2 # Docstring comes from extension module _netCDF4.
----> 3 from ._netCDF4 import *
4 # Need explicit imports for names beginning with underscores
5 from ._netCDF4 import __doc__, __pdoc__
ImportError: dlopen(/anaconda3/lib/python3.6/site-packages/netCDF4/_netCDF4.cpython-36m-darwin.so, 2): Library not loaded: #rpath/libhdf5.101.dylib
Referenced from: /anaconda3/lib/python3.6/site-packages/netCDF4/_netCDF4.cpython-36m-darwin.so
Reason: image not found
from mpl_toolkits.basemap import Basemap
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-8-5fb601138328> in <module>()
3 import matplotlib.pyplot as plt
4 from scipy.interpolate import griddata
----> 5 from mpl_toolkits.basemap import Basemap
6 import numpy.ma as ma
7 import glob
/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py in <module>()
35 import numpy as np
36 import numpy.ma as ma
---> 37 import _geoslib
38 import functools
39
ImportError: dlopen(/anaconda3/lib/python3.6/site-packages/_geoslib.so, 2): Library not loaded: #rpath/libgeos-3.5.0.dylib
Referenced from: /anaconda3/lib/python3.6/site-packages/_geoslib.so
Reason: image not found
I have no idea what these errors mean, since those paths do exist. What is most baffling is that the netCDF4 and mpl_toolkits modules did not change (i.e. were not updated/downgraded) with the installation of gdal, so why now are they failing?
I'm at the end of my tether with problems like this. Fortunately I made a copy of my Python root directory before attempting this installation so I can now revert back to it (I've been here many times before).
I know about working in separate conda environments, but am I seriously supposed to install a new environment every time I want to use a new Python module? This not only takes up a lot of harddrive space but most inconveniently means adding new functionality to old codes is impossible (I want to use GDAL to plot some geotiffs using Basemap, which now does not work!).
I cannot imagine proficient Python users battling with these issues - so what am I doing wrong?

Using pytables with python3 fails

importing pytables (3.1.1) in python 3.4.1 fails for me, complaining about a failed cPickle import
i try to use potables (3.1.1) with python (3.4.1)
In [1]: import tables
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-389ecae14f10> in <module>()
----> 1 import tables
/work/projects/vEnv/lib/python3.4/site-packages/tables/__init__.py in <module>()
80
81 # Necessary imports to get versions stored on the cython extension
---> 82 from tables.utilsextension import (
83 get_pytables_version, get_hdf5_version, blosc_compressor_list,
84 blosc_compcode_to_compname_ as blosc_compcode_to_compname,
/work/projects/vEnv/lib/python3.4/site-packages/tables/utilsextension.pyx in init tables.utilsextension (tables/utilsextension.c:15600)()
/work/projects/vEnv/lib/python3.4/site-packages/tables/description.py in <module>()
22 import numpy
23
---> 24 from tables import atom
25 from tables.path import check_name_validity
26
/work/projects/vEnv/lib/python3.4/site-packages/tables/atom.py in <module>()
18 import sys
19 import inspect
---> 20 import cPickle
21
22 import numpy
As i understand cPickle is only used instead of pickle in python2.
As of PyTable 3.x it should work fine with python3.x. How can there be a cPickle import than?
What do i have to do to use pytables with python3?
The error was produced most likely due to a problem with my vEnv. Uninstalling the package and reinstalling it fix the problem.
See github issue

Resources