I am having serious problems with sklearn. In particular, when doing:
from sklearn.linear_model import LinearRegression
I get the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-8-a35af2a4e137> in <module>
----> 1 from sklearn.linear_model import LinearRegression
~/anaconda3/lib/python3.7/site-packages/sklearn/__init__.py in <module>
78 from . import _distributor_init # noqa: F401
79 from . import __check_build # noqa: F401
---> 80 from .base import clone
81 from .utils._show_versions import show_versions
82
~/anaconda3/lib/python3.7/site-packages/sklearn/base.py in <module>
19 from . import __version__
20 from ._config import get_config
---> 21 from .utils import _IS_32BIT
22 from .utils.validation import check_X_y
23 from .utils.validation import check_array
~/anaconda3/lib/python3.7/site-packages/sklearn/utils/__init__.py in <module>
21
22 from .murmurhash import murmurhash3_32
---> 23 from .class_weight import compute_class_weight, compute_sample_weight
24 from . import _joblib
25 from ..exceptions import DataConversionWarning
~/anaconda3/lib/python3.7/site-packages/sklearn/utils/class_weight.py in <module>
5 import numpy as np
6
----> 7 from .validation import _deprecate_positional_args
8
9
~/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in <module>
23 from contextlib import suppress
24
---> 25 from .fixes import _object_dtype_isnan, parse_version
26 from .. import get_config as _get_config
27 from ..exceptions import NonBLASDotWarning, PositiveSpectrumWarning
ImportError: cannot import name 'parse_version' from 'sklearn.utils.fixes' (/Users/federiconutarelli/anaconda3/lib/python3.7/site-packages/sklearn/utils/fixes.py)
which I am not able to fix since in fixes.py I can see
def _parse_version(version_string):
version = []
for x in version_string.split('.'):
try:
version.append(int(x))
except ValueError:
# x may be of the form dev-1ea1592
version.append(x)
return tuple(version)
which seem to me the same as parse_version. Why Is this error occurring? Can anyone please help me?
Thank you all,
Federico
You may resolve this by creating a fresh conda environment. Sometimes some conflicts prevent packages from running correctly.
For my situation, I uninstall the two packages scipy and scikit-learn and then installed them. And then it works well. I try both conda install and pip install for many times, so I can't tell which way to install is better.
In the detailed error report, there should be some lines relevant to scipy. So I think this is an error about the incompatibility between these two packages and therefore some dll doesn't work well.
Related
Getting error, ModuleNotFoundError: No module named 'tensorflow.python.util
I've tried uninstalling and reinstalling more than 10 times, and tried different variations
pip install tensorflow_gpu, pip install tensorflow --user, and also upgraded my pip.
But still this error keeps happening when I try to import keras.models from tensorflow to run a model.
Does anyone know how to resolve the issue? Thanks in advance.
The full error I am getting:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-67-114c1607a26b> in <module>
4 from datetime import datetime
5 import numpy as np
----> 6 from tensorflow import keras
7 from keras.models import Sequential
8 from keras.layers import LSTM
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\__init__.py in <module>
39 import sys as _sys
40
---> 41 from tensorflow.python.tools import module_util as _module_util
42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
43
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\__init__.py in <module>
38 # pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top
39
---> 40 from tensorflow.python.eager import context
41 from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
42
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\context.py in <module>
34 from tensorflow.core.protobuf import rewriter_config_pb2
35 from tensorflow.python import pywrap_tfe
---> 36 from tensorflow.python import tf2
37 from tensorflow.python.client import pywrap_tf_session
38 from tensorflow.python.eager import executor
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\tf2.py in <module>
24
25 from tensorflow.python.platform import _pywrap_tf2
---> 26 from tensorflow.python.util.tf_export import tf_export
27
28
ModuleNotFoundError: No module named 'tensorflow.python.util
My guess is that pip is installing it for python 2 and then you are running in python 3. I have seen this behaviour and then needing to use pip3.
import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
y_pred = dtree.predict(X)
y_pred
The error ::
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-745bbfa9769b> in <module>
1 import pandas
----> 2 from sklearn import tree
3 import pydotplus
4 from sklearn.tree import DecisionTreeClassifier
5 import matplotlib.pyplot as plt
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/__init__.py in <module>
62 else:
63 from . import __check_build
---> 64 from .base import clone
65 from .utils._show_versions import show_versions
66
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/base.py in <module>
12 from scipy import sparse
13 from .externals import six
---> 14 from .utils.fixes import signature
15 from .utils import _IS_32BIT
16 from . import __version__
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/__init__.py in <module>
12 from .murmurhash import murmurhash3_32
13 from .class_weight import compute_class_weight, compute_sample_weight
---> 14 from . import _joblib
15 from ..exceptions import DataConversionWarning
16 from .fixes import _Sequence as Sequence
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/_joblib.py in <module>
20 from joblib import parallel_backend, register_parallel_backend
21 else:
---> 22 from ..externals import joblib
23 from ..externals.joblib import logger
24 from ..externals.joblib import dump, load
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/__init__.py in <module>
117 from .numpy_pickle import load
118 from .compressor import register_compressor
--> 119 from .parallel import Parallel
120 from .parallel import delayed
121 from .parallel import cpu_count
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/parallel.py in <module>
26 from .my_exceptions import TransportableException
27 from .disk import memstr_to_bytes
---> 28 from ._parallel_backends import (FallbackToBackend, MultiprocessingBackend,
29 ThreadingBackend, SequentialBackend,
30 LokyBackend)
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/_parallel_backends.py in <module>
20 from .pool import MemmappingPool
21 from multiprocessing.pool import ThreadPool
---> 22 from .executor import get_memmapping_executor
23
24 # Compat between concurrent.futures and multiprocessing TimeoutError
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/executor.py in <module>
12 from .disk import delete_folder
13 from ._memmapping_reducer import get_memmapping_reducers
---> 14 from .externals.loky.reusable_executor import get_reusable_executor
15
16
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/loky/__init__.py in <module>
10
11 from .backend.context import cpu_count
---> 12 from .backend.reduction import set_loky_pickler
13 from .reusable_executor import get_reusable_executor
14 from .cloudpickle_wrapper import wrap_non_picklable_objects
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/loky/backend/reduction.py in <module>
123 # global variable to change the pickler behavior
124 try:
--> 125 from sklearn.externals.joblib.externals import cloudpickle # noqa: F401
126 DEFAULT_ENV = "cloudpickle"
127 except ImportError:
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/cloudpickle/__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from .cloudpickle import *
4
5 __version__ = '0.6.1'
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py in <module>
165
166
--> 167 _cell_set_template_code = _make_cell_set_template_code()
168
169
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py in _make_cell_set_template_code()
146 )
147 else:
--> 148 return types.CodeType(
149 co.co_argcount,
150 co.co_kwonlyargcount,
TypeError: an integer is required (got type bytes)
So I keep getting this type error and have no clue what to do pretty sure that sklearn is installed. Both X and y are previously defined after creating the training set to be used. I've tried reinstalling scikit-learn, downgrading python and installing through the terminal but none of that helped. Just cant figure out what the issue is here and will really appreciate some help on this
I was able to reproduce the problem in a conda environment by running conda install python==3.8 scikit-learn but then manually downgrading joblib by running pip install joblib<0.14 since joblib<0.14 is incompatible with Python 3.8.
I'm not exactly sure how you got into the this situation, but it should fix it to first uninstall any joblib package that might have been mis-installed:
$ pip uninstall joblib
Then force reinstall/upgrade it with conda:
$ conda update --force-reinstall joblib
Confirm the correct version was installed:
$ python -c 'import joblib; print(joblib.__version__)'
0.17.0
If all else fails, try creating a fresh Anaconda install. Make sure to install packages with conda install and not pip.
In my case, joblib in scikit needs to be reinstalled.
pip uninstall joblib
This happened to me because I pip installed sklearn instead of scikit-learn.
In my case, I did
pip uninstall sklearn --yes
followed by
pip install sklearn
Getting this error when trying to import tf_agents.environments running on Tensorflow 2.3.0, using anaconda environment. Have tried to reinstall tensorflow, still getting same error. Running the jupyter notebook in as admin, still getting same error. I am using python 3.8
from tf_agents.environments import suite_gym
ImportError Traceback (most recent call last)
<ipython-input-8-b9cd8ac2787f> in <module>
----> 1 from tf_agents.environments import suite_gym
2
3 # env = suite_gym.load("Breakout-v4")
4 # env
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\environments\__init__.py in <module>
24 from tf_agents.environments import tf_py_environment
25 from tf_agents.environments import trajectory_replay
---> 26 from tf_agents.environments import utils
27 from tf_agents.environments import wrappers
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\environments\utils.py in <module>
23 from tf_agents.environments import tf_environment
24 from tf_agents.environments import tf_py_environment
---> 25 from tf_agents.policies import random_py_policy
26 from tf_agents.specs import array_spec
27
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\policies\__init__.py in <module>
16 """Policies Module."""
17
---> 18 from tf_agents.policies import actor_policy
19 from tf_agents.policies import boltzmann_policy
20 from tf_agents.policies import epsilon_greedy_policy
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\policies\actor_policy.py in <module>
27 import tensorflow_probability as tfp
28
---> 29 from tf_agents.networks import network
30 from tf_agents.policies import tf_policy
31 from tf_agents.specs import tensor_spec
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\networks\__init__.py in <module>
16 """Networks Module."""
17
---> 18 from tf_agents.networks import actor_distribution_network
19 from tf_agents.networks import actor_distribution_rnn_network
20 from tf_agents.networks import bias_layer
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\networks\actor_distribution_network.py in <module>
24 import tensorflow as tf # pylint: disable=g-explicit-tensorflow-version-import
25
---> 26 from tf_agents.networks import categorical_projection_network
27 from tf_agents.networks import encoding_network
28 from tf_agents.networks import network
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\networks\categorical_projection_network.py in <module>
24 import tensorflow_probability as tfp
25
---> 26 from tf_agents.networks import network
27 from tf_agents.networks import utils
28 from tf_agents.specs import distribution_spec
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\networks\network.py in <module>
31
32 # pylint:disable=g-direct-tensorflow-import
---> 33 from tensorflow.python.keras.engine import network as keras_network # TF internal
34 from tensorflow.python.training.tracking import base # TF internal
35 from tensorflow.python.util import tf_decorator # TF internal
ImportError: cannot import name 'network' from 'tensorflow.python.keras.engine' (C:\Users\Vision\anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\engine\__init__.py)
Am a bit late here, but faced a similar issue.
How I resolved the issue :
Uninstall the current version of tf_agents, use
pip uninstall tf-agents
Please install the current tf-agents-nightly (0.6.0) GitHub Repo.
use pip install tf-agents-nightly
from tf_agents.environments import suite_gym
I installed scikit-learn library in python using the command
pip install -U scikit-learn
When I am trying to import the library or it's module like
from sklearn.model_selection import train_test_split
or simply import sklearn
I am getting the error
ImportError Traceback (most recent call last)
<ipython-input-24-73edc048c06b> in <module>()
----> 1 from sklearn.model_selection import train_test_split
c:\users\ajain9\appdata\local\programs\python\python36-32\lib\site-packages\sklearn\__init__.py in <module>()
132 else:
133 from . import __check_build
--> 134 from .base import clone
135 __check_build # avoid flakes unused variable error
136
c:\users\ajain9\appdata\local\programs\python\python36-32\lib\site-packages\sklearn\base.py in <module>()
11 from scipy import sparse
12 from .externals import six
---> 13 from .utils.fixes import signature
14 from . import __version__
15
c:\users\ajain9\appdata\local\programs\python\python36-32\lib\site-packages\sklearn\utils\__init__.py in <module>()
7 import warnings
8
----> 9 from .murmurhash import murmurhash3_32
10 from .validation import (as_float_array,
11 assert_all_finite,
ImportError: cannot import name 'murmurhash3_32'
Any reason this error might be happening?
I am using Python version 3.6.3 Numpy v 1.13.3 pandas v 0.21.0
I am using windows
Try using virutalenv and install all the libraries required there, it worked for me.
I was using this code:
import tensorflow as tf , and it was working fine.
When I now try to import it, I get the following error
In [2]: import tensorflow as tf
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-41389fad42b5> in <module>()
----> 1 import tensorflow as tf
/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py in <module>()
21 from __future__ import print_function
22
---> 23 from tensorflow.python import *
/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py in <module>()
63 from tensorflow.core.util.event_pb2 import *
64 # Import things out of contrib
---> 65 import tensorflow.contrib as contrib
66
67 # Framework
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/__init__.py in <module>()
28 from tensorflow.contrib import grid_rnn
29 from tensorflow.contrib import layers
---> 30 from tensorflow.contrib import learn
31 from tensorflow.contrib import linear_optimizer
32 from tensorflow.contrib import lookup
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/__init__.py in <module>()
70
71 # pylint: disable=wildcard-import
---> 72 from tensorflow.contrib.learn.python.learn import *
73 from tensorflow.python.util.all_util import make_all
74
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/__init__.py in <module>()
21
22 # pylint: disable=wildcard-import
---> 23 from tensorflow.contrib.learn.python.learn import *
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/__init__.py in <module>()
24 # pylint: disable=wildcard-import
25 from tensorflow.contrib.learn.python.learn import datasets
---> 26 from tensorflow.contrib.learn.python.learn import estimators
27 from tensorflow.contrib.learn.python.learn import graph_actions
28 from tensorflow.contrib.learn.python.learn import learn_io as io
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/__init__.py in <module>()
21
22 from tensorflow.contrib.learn.python.learn.estimators._sklearn import NotFittedError
---> 23 from tensorflow.contrib.learn.python.learn.estimators.autoencoder import TensorFlowDNNAutoencoder
24 from tensorflow.contrib.learn.python.learn.estimators.base import TensorFlowBaseTransformer
25 from tensorflow.contrib.learn.python.learn.estimators.base import TensorFlowEstimator
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/autoencoder.py in <module>()
23
24 from tensorflow.contrib.learn.python.learn import models
---> 25 from tensorflow.contrib.learn.python.learn.estimators.base import TensorFlowBaseTransformer
26 from tensorflow.python.ops import nn
27
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/base.py in <module>()
32 from tensorflow.contrib import layers
33 from tensorflow.contrib.learn.python.learn.estimators import _sklearn
---> 34 from tensorflow.contrib.learn.python.learn.estimators import estimator
35 from tensorflow.contrib.learn.python.learn.estimators._sklearn import NotFittedError
36 from tensorflow.contrib.learn.python.learn.learn_io.data_feeder import setup_train_data_feeder
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in <module>()
37 from tensorflow.contrib.learn.python.learn.estimators import tensor_signature
38 from tensorflow.contrib.learn.python.learn.estimators._sklearn import NotFittedError
---> 39 from tensorflow.contrib.learn.python.learn.learn_io import data_feeder
40 from tensorflow.contrib.learn.python.learn.utils import checkpoints
41
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/__init__.py in <module>()
28 from tensorflow.contrib.learn.python.learn.learn_io.graph_io import read_keyed_batch_examples
29 from tensorflow.contrib.learn.python.learn.learn_io.graph_io import read_keyed_batch_features
---> 30 from tensorflow.contrib.learn.python.learn.learn_io.pandas_io import extract_pandas_data
31 from tensorflow.contrib.learn.python.learn.learn_io.pandas_io import extract_pandas_labels
32 from tensorflow.contrib.learn.python.learn.learn_io.pandas_io import extract_pandas_matrix
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/pandas_io.py in <module>()
22 try:
23 # pylint: disable=g-import-not-at-top
---> 24 import pandas as pd
25 HAS_PANDAS = True
26 except ImportError:
/usr/local/lib/python2.7/dist-packages/pandas/__init__.py in <module>()
23
24 try:
---> 25 from pandas import hashtable, tslib, lib
26 except ImportError as e: # pragma: no cover
27 module = str(e).lstrip('cannot import name ') # hack but overkill to use re
/home/kv/pandas/src/numpy.pxd in init pandas.hashtable (pandas/hashtable.c:38364)()
ValueError: numpy.dtype has the wrong size, try recompiling. Expected 88, got 96
I even tried upgrading numpy, but that didn't solve the issue.
Can you help me resolve this error?
EDIT:
numpy (1.8.2)
tensorflow (0.10.0rc0)
python 2.7.6
You need to update numpy or recompile pandas.
There is a very good answer and explanation provided here: ValueError: numpy.dtype has the wrong size, try recompiling
I would strongly suggest to use the anaconda distribution and install the latest tensorflow using:
conda install -c conda-forge tensorflow
Read more in the official setup guide
Try installing numpy with PIP using
sudo apt-get install python-pip
sudo pip install numpy==1.11.1
or pip3 instead of pip for python 3 like
sudo apt-get install python3-pip
sudo pip3 install numpy==1.11.1
this will help as i also had this error because tensorflow is using numpy 1.11.1