Cannot Import LinearRegression from Sklearn - scikit-learn

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

Related

how to import segmentation-models with tensorflow keras? getting ModuleNotFoundError: No module named 'keras.legacy'

I tried to install segmentation-models using.
!pip install -U segmentation-models==0.2.1
import tensorflow as tf
import tensorflow.keras as keras
print(tf.__version__)
print(keras.__version__)
Output:
2.4.1
2.4.0
# Tried for import
import segmentation_models as sm
Resulted in error: ModuleNotFoundError: No module named 'keras.legacy'
Following is the stack trace
stack Trace:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-16-7b5049dd4be0> in <module>()
----> 1 import segmentation_models as sm
6 frames
/usr/local/lib/python3.7/dist-packages/segmentation_models/__init__.py in <module>()
4
5 from .unet import Unet
----> 6 from .fpn import FPN
7 from .linknet import Linknet
8 from .pspnet import PSPNet
/usr/local/lib/python3.7/dist-packages/segmentation_models/fpn/__init__.py in <module>()
----> 1 from .model import FPN
2
/usr/local/lib/python3.7/dist-packages/segmentation_models/fpn/model.py in <module>()
----> 1 from .builder import build_fpn
2 from ..backbones import get_backbone, get_feature_layers
3 from ..utils import freeze_model
4 from ..utils import legacy_support
5
/usr/local/lib/python3.7/dist-packages/segmentation_models/fpn/builder.py in <module>()
6 from keras.models import Model
7
----> 8 from .blocks import pyramid_block
9 from ..common import ResizeImage
10 from ..common import Conv2DBlock
/usr/local/lib/python3.7/dist-packages/segmentation_models/fpn/blocks.py in <module>()
1 from keras.layers import Add
2
----> 3 from ..common import Conv2DBlock
4 from ..common import ResizeImage
5 from ..utils import to_tuple
/usr/local/lib/python3.7/dist-packages/segmentation_models/common/__init__.py in <module>()
1 from .blocks import Conv2DBlock
----> 2 from .layers import ResizeImage
/usr/local/lib/python3.7/dist-packages/segmentation_models/common/layers.py in <module>()
2 from keras.engine import InputSpec
3 from keras.utils import conv_utils
----> 4 from keras.legacy import interfaces
5 from keras.utils.generic_utils import get_custom_objects
6
ModuleNotFoundError: No module named 'keras.legacy'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
Solution Tried:
To downgrade both tensorflow and keras to
!pip install tensorflow==2.2.0
!pip install keras==2.3.1
But it is creating conflict between the tensorflow.keras and keras.
Is there any way to work this using tensorflow.keras?
As of now the following approach is working.
Do not try with specific version of segmentation_models module.
#install this way
!pip3 install tensorflow==2.2
!pip3 install keras==2.3.1
!pip3 install -U segmentation-models
import tensorflow as tf
import tensorflow.keras as keras
import segmentation_models as sm
Now segmentation_models imported successfully.

ImportError: cannot import name 'DeprecationDict'

I'm trying to import dask_searchcv and error is thrown while import other module DeprecationDict.
I have installed dask_searchcv Version: 0.2.0, and scikit-learn
Version: 0.21.3
I tried importing the dark_searchcv as shown below:
import dask_searchcv as dcv
I'm getting the following error message:
ImportError Traceback (most recent call last)
<ipython-input-19-b8082b897401> in <module>
1 # Instantiate the grid search model
----> 2 import dask_searchcv as dcv
3 # grid_search = dcv.GridSearchCV(estimator = rf, param_grid = param_grid, cv = 3)
4 # grid_search.fit(data, target)
5 # grid_search.best_params_
~/.virtualenvs/dask-test/lib/python3.6/site-packages/dask_searchcv/__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from .model_selection import GridSearchCV, RandomizedSearchCV
4
5 from ._version import get_versions
~/.virtualenvs/dask-test/lib/python3.6/site-packages/dask_searchcv/model_selection.py in <module>
52
53 if _SK_VERSION >= '0.19.1':
---> 54 from sklearn.utils.deprecation import DeprecationDict
55 _RETURN_TRAIN_SCORE_DEFAULT = 'warn'
56
ImportError: cannot import name 'DeprecationDict'
I have tried re-installing sklearn using pip3 install -U scikit-learn but issue still remains unresolved.
Note: I'm running above code using jupyter notebook
dask-searchcv is deprecated, as noted in the docs: https://dask-searchcv.readthedocs.io/en/latest/
You want dask-ml and you should update your import as import dask_ml.model_selection as dcv.

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.

Resources