Cannot import Kmeans anymore - python-3.x

I tried to import Kmeans
from sklearn.cluster import KMeans
And I got this error :
AttributeError: module 'sklearn.utils._openmp_helpers' has no attribute '__pyx_capi__'
Never had this problem before and that's not the first time I use Kmeans.
I tried to uninstall sklearn and install it again, but that doesn't change anything.
I have the last version of sklearn, if it could help :
import sklearn
sklearn.__version__
'0.24.1'
EDIT : Here's the full error, if you want details :
----> 1 from sklearn.cluster import KMeans
~\Anaconda3\lib\site-packages\sklearn\cluster\__init__.py in <module>
4 """
5
----> 6 from ._spectral import spectral_clustering, SpectralClustering
7 from ._mean_shift import mean_shift, MeanShift, estimate_bandwidth, get_bin_seeds
8 from ._affinity_propagation import affinity_propagation, AffinityPropagation
~\Anaconda3\lib\site-packages\sklearn\cluster\_spectral.py in <module>
17 from ..base import BaseEstimator, ClusterMixin
18 from ..utils import check_random_state, as_float_array, check_scalar
---> 19 from ..metrics.pairwise import pairwise_kernels
20 from ..neighbors import kneighbors_graph, NearestNeighbors
21 from ..manifold import spectral_embedding
~\Anaconda3\lib\site-packages\sklearn\metrics\__init__.py in <module>
39 from ._dist_metrics import DistanceMetric
40
---> 41 from . import cluster
42 from .cluster import adjusted_mutual_info_score
43 from .cluster import adjusted_rand_score
~\Anaconda3\lib\site-packages\sklearn\metrics\cluster\__init__.py in <module>
20 from ._supervised import fowlkes_mallows_score
21 from ._supervised import entropy
---> 22 from ._unsupervised import silhouette_samples
23 from ._unsupervised import silhouette_score
24 from ._unsupervised import calinski_harabasz_score
~\Anaconda3\lib\site-packages\sklearn\metrics\cluster\_unsupervised.py in <module>
14 from ...utils import check_X_y
15 from ...utils import _safe_indexing
---> 16 from ..pairwise import pairwise_distances_chunked
17 from ..pairwise import pairwise_distances
18 from ...preprocessing import LabelEncoder
~\Anaconda3\lib\site-packages\sklearn\metrics\pairwise.py in <module>
31 from ..utils.fixes import sp_version, parse_version
32
---> 33 from ._pairwise_distances_reduction import PairwiseDistancesArgKmin
34 from ._pairwise_fast import _chi2_kernel_fast, _sparse_manhattan
35 from ..exceptions import DataConversionWarning
sklearn\metrics\_pairwise_distances_reduction.pyx in init sklearn.metrics._pairwise_distances_reduction()
AttributeError: module 'sklearn.utils._openmp_helpers' has no attribute '__pyx_capi__'

Related

Which Tensorflow Package for Keras 2.3

I am trying to use Keras on my machine but I keep getting the error message
"AttributeError: module 'tensorflow.python.keras.backend' has no attribute 'get_graph'".
Stack Trace is:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-dd13cebc6f04> in <module>
----> 1 import acgan
~/Documents/GitHub/Keras-GAN/acgan/acgan.py in <module>
1 from __future__ import print_function, division
2
----> 3 from keras.datasets import mnist
4 from keras.layers import Input, Dense, Reshape, Flatten, Dropout, multiply
5 from keras.layers import BatchNormalization, Activation, Embedding, ZeroPadding2D
/anaconda3/lib/python3.6/site-packages/keras/__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
/anaconda3/lib/python3.6/site-packages/keras/utils/__init__.py in <module>
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7 from . import losses_utils
8 from . import metrics_utils
/anaconda3/lib/python3.6/site-packages/keras/utils/conv_utils.py in <module>
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11
/anaconda3/lib/python3.6/site-packages/keras/backend/__init__.py in <module>
----> 1 from .load_backend import epsilon
2 from .load_backend import set_epsilon
3 from .load_backend import floatx
4 from .load_backend import set_floatx
5 from .load_backend import cast_to_floatx
/anaconda3/lib/python3.6/site-packages/keras/backend/load_backend.py in <module>
88 elif _BACKEND == 'tensorflow':
89 sys.stderr.write('Using TensorFlow backend.\n')
---> 90 from .tensorflow_backend import *
91 else:
92 # Try and load external backend.
/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in <module>
52
53 # Private TF Keras utils
---> 54 get_graph = tf_keras_backend.get_graph
55 # learning_phase_scope = tf_keras_backend.learning_phase_scope #
TODO
56 name_scope = tf.name_scope
AttributeError: module 'tensorflow.python.keras.backend' has no attribute 'get_graph'
I googled the issue and found this
https://forums.xilinx.com/t5/Deephi-DNNDK/Module-tensorflow-python-keras-backend-has-no-attribute-get/td-p/1022555
which tells me that it is tensorflow version related, but not which version to use. I am using Python3 on MacOS.
Thank you for your help.
I couldn't reproduce the error you observe with Keras 2.3.0 & tensorflow 2.0.0rc1. Although, I was able to start the training of acgan, I got a different error after one iteration: Resource localhost/_AnonymousVar84/N10tensorflow3VarE does not exist.
I could successfully run the example with the following versions:
Keras 2.2.4/tensorflow 1.14.0
tensorflow 2.0.0rc1 and replace keras with tensorflow.keras in the imports.

How to fix this error while using statsmodels" ImportError: cannot import name 'factorial'"?

I have already gone through this answer
While importing auto_arima from pmdarima: ERROR : cannot import name 'factorial' from 'scipy.misc'
but couldn't fix the error,I do not understand how to use developer version.
Is there any other method for applying Seasonal ARIMA model?
import statsmodels.api as sm
mod = sm.tsa.statespace.SARIMAX(train_weekly.Price,
order=(1, 0, 0),
seasonal_order=(1, 1, 0, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results = mod.fit()
print(results.summary().tables[0])
print(results.summary().tables[1])
Following is the output
ImportError Traceback (most recent call last)
<ipython-input-30-a5d9120bdc57> in <module>()
----> 1 import statsmodels.api as sm
2 mod = sm.tsa.statespace.SARIMAX(train_weekly.Price,
3 order=(1, 0, 0),
4 seasonal_order=(1, 1, 0, 12),
5 enforce_stationarity=False,
3 frames
/usr/local/lib/python3.6/dist-packages/statsmodels/api.py in <module>()
14 from . import robust
15 from .robust.robust_linear_model import RLM
---> 16 from .discrete.discrete_model import (Poisson, Logit, Probit,
17 MNLogit, NegativeBinomial,
18 GeneralizedPoisson,
/usr/local/lib/python3.6/dist-packages/statsmodels/discrete/discrete_model.py in <module>()
43
44 from statsmodels.base.l1_slsqp import fit_l1_slsqp
---> 45 from statsmodels.distributions import genpoisson_p
46
47 try:
/usr/local/lib/python3.6/dist-packages/statsmodels/distributions/__init__.py in <module>()
1 from .empirical_distribution import ECDF, monotone_fn_inverter, StepFunction
----> 2 from .edgeworth import ExpandedNormal
3 from .discrete import genpoisson_p, zipoisson, zigenpoisson, zinegbin
/usr/local/lib/python3.6/dist-packages/statsmodels/distributions/edgeworth.py in <module>()
5 import numpy as np
6 from numpy.polynomial.hermite_e import HermiteE
----> 7 from scipy.misc import factorial
8 from scipy.stats import rv_continuous
9 import scipy.special as special
ImportError: cannot import name 'factorial'
I just want to use seasonal ARIMA,if there is any other way please help me.
Thanks.
Seems like there is a version mismatch of scipy module. Try downgrading scipy module.
In windows execute the following command in Administrative mode,
pip3 install --user scipy==1.2.0
If you're using Linux,
python3.6 -m pip install scipy==1.2 --upgrade

Error importing 'getargspec' statsmodels

I'm trying to do a logistic regression using statsmodels & an unable to import. I'm using statsmodels v0.8.0 with Python 3.x in a Jupyter notebook. How am I able to import correctly?
ImportError Traceback (most recent call last)
<ipython-input-47-6030a6549dc0> in <module>()
----> 1 import statsmodels.api as sm
/Applications/anaconda/lib/python3.6/site-packages/statsmodels/api.py in <module>()
3 from . import tools
4 from .tools.tools import add_constant, categorical
----> 5 from . import regression
6 from .regression.linear_model import OLS, GLS, WLS, GLSAR
7 from .regression.recursive_ls import RecursiveLS
/Applications/anaconda/lib/python3.6/site-packages/statsmodels/regression/__init__.py in <module>()
----> 1 from .linear_model import yule_walker
2
3 from statsmodels import NoseWrapper as Tester
4 test = Tester().test
/Applications/anaconda/lib/python3.6/site-packages/statsmodels/regression/linear_model.py in <module>()
51 cache_readonly,
52 cache_writable)
---> 53 import statsmodels.base.model as base
54 import statsmodels.base.wrapper as wrap
55 from statsmodels.emplike.elregress import _ELRegOpts
/Applications/anaconda/lib/python3.6/site-packages/statsmodels/base/model.py in <module>()
8 from statsmodels.stats.contrast import ContrastResults, WaldTestResults
9 from statsmodels.tools.decorators import resettable_cache, cache_readonly
---> 10 import statsmodels.base.wrapper as wrap
11 from statsmodels.tools.numdiff import approx_fprime
12 from statsmodels.tools.sm_exceptions import ValueWarning, \
/Applications/anaconda/lib/python3.6/site-packages/statsmodels/base/wrapper.py in <module>()
3
4 import numpy as np
----> 5 from statsmodels.compat.python import get_function_name, iteritems, getargspec
6
7 class ResultsWrapper(object):
ImportError: cannot import name 'getargspec'

sklearn import error - ImportError: cannot import name 'comb'

I am trying to do this:
from sklearn.model_selection import train_test_split
and getting an error:
In [31]: from sklearn.model_selection import train_test_split
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-31-73edc048c06b> in <module>()
----> 1 from sklearn.model_selection import train_test_split
/usr/local/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
/usr/local/lib/python3.6/site-packages/sklearn/model_selection/_split.py in <module>()
29 from ..externals.six import with_metaclass
30 from ..externals.six.moves import zip
---> 31 from ..utils.fixes import signature, comb
32 from ..base import _pprint
33
ImportError: cannot import name 'comb'
any help would be much apreciated.
SciPy >=0.19 uses from scipy.special import comb instead of from scipy.misc import comb. https://github.com/scikit-learn/scikit-learn/pull/9046
utils/fixes.py source
try: # SciPy >= 0.19
from scipy.special import comb, logsumexp
except ImportError:
from scipy.misc import comb, logsumexp # noqa
I had the same error. conda remove --name your environment --all. Next, re-install the environment. It worked for me.

Cannot Import LinearRegression from Sklearn

from sklearn.linear_model import LinearRegression
gives me this error in Jupyter Notebook:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-127-36ba82e2d702> in <module>()
----> 1 from sklearn.linear_model import LinearRegression
2
3 lin_reg = LinearRegression()
4 lin_reg.fit(housing_prepared, housing_labels)
C:\Users\David\Anaconda2\lib\site-packages\sklearn\linear_model\__init__.py in <module>()
19 MultiTaskElasticNet, MultiTaskElasticNetCV,
20 MultiTaskLassoCV)
---> 21 from .huber import HuberRegressor
22 from .sgd_fast import Hinge, Log, ModifiedHuber, SquaredLoss, Huber
23 from .stochastic_gradient import SGDClassifier, SGDRegressor
C:\Users\David\Anaconda2\lib\site-packages\sklearn\linear_model\huber.py in <module>()
10 from ..utils import check_X_y
11 from ..utils import check_consistent_length
---> 12 from ..utils import axis0_safe_slice
13 from ..utils.extmath import safe_sparse_dot
14
ImportError: cannot import name axis0_safe_slice
I can import things from sklearn.preprocessing fine. Thanks for your help!
Don't know what the exact issue was, but uninstalling and reinstalling scikit-learn fixed this for me:
pip uninstall scikit-learn
pip install scikit-learn

Resources