Unable to import functions from scipy module.
Gives error :
from scipy.signal.signaltools import _centered
Cannot import name '_centered' from 'scipy.signal.signaltools'
scipy.__version__
1.8.0
I encountered the same problem while using statsmodels~=0.12.x. Increasing the statsmodels package to version 0.13.2, this import issue is resolved.
UPDATE with more notes:
before:
installation of fixed version of statsmodels==0.12.2 which is dependent on scipy
there was newly released scipy==1.8.0 - 2022-02-05
when installing it, got this problem:
from statsmodels.tsa.seasonal import seasonal_decompose
File "/usr/local/lib/python3.8/site-packages/statsmodels/tsa/seasonal.py", line 12, in <module>
from statsmodels.tsa.filters.filtertools import convolution_filter
File "/usr/local/lib/python3.8/site-packages/statsmodels/tsa/filters/filtertools.py", line 18, in <module>
from scipy.signal.signaltools import _centered as trim_centered
ImportError: cannot import name '_centered' from 'scipy.signal.signaltools' (/usr/local/lib/python3.8/site-packages/scipy/signal/signaltools.py)
after:
when bumping up statsmodels to the latest version available 0.13.2 release 2022-02-08, it works
If you are not using statsmodels but other package which is dependent on scipy, have a look if there is newer version available (after the release of scipy to v.1.8.0)
If you need to use that specific version of statsmodels 0.12.x with scipy 1.8.0 I have the following hack.
Basically it just re-publishes the existing (but private) _centered function as a public attribute to the module already imported in RAM.
It is a workaround, and if you can simply upgrade your dependencies to the latest versions. Only use this if you are forced to use those specific versions.
import scipy.signal.signaltools
def _centered(arr, newsize):
# Return the center newsize portion of the array.
newsize = np.asarray(newsize)
currsize = np.array(arr.shape)
startind = (currsize - newsize) // 2
endind = startind + newsize
myslice = [slice(startind[k], endind[k]) for k in range(len(endind))]
return arr[tuple(myslice)]
scipy.signal.signaltools._centered = _centered
As it was mentioned by AndrejHan and ThomasCokelaer, you need to upgrade to statsmodels-0.13.2 with:
$ pip install statsmodels --upgrade
or
$ python3 -m pip install statsmodels --upgrade
Your error should disappear
With scipy 1.8.0, the deprecated form
from scipy.signal.signaltools import _centered
should become
from scipy.signal._signaltools import _centered
Note the underscore in front of signaltools
Related
I tried to import umap in my jupyter notebook but had the following error:
ImportError: cannot import name 'structref' from 'numba.experimental' (C:\Users\name\Anaconda3\lib\site-packages\numba\experimental\__init__.py)
I tried to update conda but doesn't work. What can I do ?
The numba.experimental subpackage was added in version 0.51.0. You can check your version of number using:
import numba
numba.__version__
If it is less then 0.51.0, you will need to install a newer version.
conda install numba=0.51.*
I want to use a logit model and trying to import statsmodels library.
My Version: Python 3.6.8
The best suggestion I got is to downgrade scipy but unclear how to and to what version should I downgrade. Please help how to resolve.
https://github.com/statsmodels/statsmodels/issues/5747
import statsmodels.formula.api as smf
ImportError Traceback (most recent call last)
<ipython-input-52-f897a2d817de> in <module>
----> 1 import statsmodels.formula.api as smf
~/anaconda3/envs/py36/lib/python3.6/site-packages/statsmodels/formula/api.py in <module>
13 from statsmodels.robust.robust_linear_model import RLM
14 rlm = RLM.from_formula
---> 15 from statsmodels.discrete.discrete_model import MNLogit
16 mnlogit = MNLogit.from_formula
17 from statsmodels.discrete.discrete_model import Logit
~/anaconda3/envs/py36/lib/python3.6/site-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:
~/anaconda3/envs/py36/lib/python3.6/site-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
~/anaconda3/envs/py36/lib/python3.6/site-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'```
Update: upgrading statsmodels will fix this issue nowadays: pip install statsmodels --upgrade.
From this issue on statsmodels' github repo, the solution appears to be to downgrade SciPy to version 1.2 (current version is 1.3, which you appear to use).
At least for me, SciPy 1.2 has the factorial function in the scipy.misc package.
You can do
python3.6 -m pip install scipy==1.2 --upgrade
Use the --user option with that if you don't have standard install rights.
Perhaps you want to avoid using pip, since you're using Conda. You should be able to pin the version of scipy in Conda as well, but if you don't plan to add any other packages to your environment, just use the pip version.
Of course, downgrading SciPy may cause issues elsewhere, but that's hard to foresee, especially without knowing exactly what other packages and dependencies you have installed; you'll just have to find out. Fingers crossed for not ending up in dependency hell (as you've already on the doorstep).
For the more curious, scipy.misc.factorial has been deprecated since version 1.0; scipy.special.factorial should be used instead.
Importing in version 1.2 does not, however, show any clear warning, nor does using it. This might explain why statsmodels still uses the old import. A fix is on the way for the next statsmodels release.
Thanks #9769953.
pip3 uninstall statsmodels # make sure to remove old versions
pip3 install statsmodels==0.10.0rc2 --pre --user # install release candidate of statsmodels
Restarting the kernel of the jupyter notebook
fixed it for me.
You can check your versions with pip3 list
Summary: copy&run the following in your terminal:
pip3 uninstall statsmodels -y
pip3 install statsmodels==0.10.0rc2 --pre --user
and don't forget to restart the kernel of your jupyter notebook :)
pip install statsmodels --upgrade
did the trick for me
One easy fix I found is editing the .py file. I was getting the same error as the OP while using Dominance analysis. Edited the dominance.py file to have from scipy.special import factorial and it worked. I would think editing the from scipy.misc import factorial line to from scipy.special import factorial in the statsmodel package code in edgeworth.py would do the same job here.
When I try to import pip package and use pip.get_installed_distributions(), console is printing error:
AttributeError: 'module' object has no attribute 'get_installed_distributions'
Are there any solutions which exclude downgrading pip?
Update
With Python 3.8, the standard library has got a way of querying the environment for installed distributions and their metadata: importlib.metadata. For older Python versions, there's a backport importlib_metadata:
$ pip install importlib-metadata
It is thus advisable to use it (or the backport) instead of relying on pip's internals.
Importing with backwards compatibility:
import sys
if sys.version_info >= (3, 8):
from importlib import metadata as importlib_metadata
else:
import importlib_metadata
Usage examples:
Get names, versions and licenses (check out more available metadata keys in core metadata spec) of all installed distributions:
dists = importlib_metadata.distributions()
for dist in dists:
name = dist.metadata["Name"]
version = dist.version
license = dist.metadata["License"]
print(f'found distribution {name}=={version}')
Querying single distribution by name:
wheel = importlib_metadata.distribution('wheel')
print(wheel.metadata["Name"], 'installed')
Original answer:
The function was moved to the pip._internal subpackage. Import example with backwards compatibility:
try:
from pip._internal.utils.misc import get_installed_distributions
except ImportError: # pip<10
from pip import get_installed_distributions
#hoefling It is not recommended and is bad practice to import items from pip._internal pip has warned against this and preceding the release of pip 10 they made an announcement regarding this too.
A good alternative would be to use setuptools pkg_resources instead. From there you can use pkg_resources.working_set. See the comment from #pradyunsg here.
import pkg_resources
dists = [d for d in pkg_resources.working_set]
# You can filter and use information from the installed distributions.
Adding to #Mmelcor answer, the items returned in the list comprehension is a PathMetadata object, something like:
[wrapt 1.10.11 (/Users/<username>/path/venv/lib/python3.6/site-packages),
widgetsnbextension 3.2.1 (/Users/<username>/path/venv/lib/python3.6/site-packages),....]
You may need to get the string representation before filtering:
import pkg_resources
dists = [str(d) for d in pkg_resources.working_set]
print(dists)
Result:
['wrapt 1.10.11',
'widgetsnbextension 3.2.1',...]
I tried to install statsmodels in python. After installation, I checked with pip freeze. The package can be seen in the list.
When I am trying:
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
I am getting error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name ExponentialSmoothing
I have tried the following link also :
link
As of today (10 May 2018), the problem is solved by simply installing version 0.9.0 rather than the default 0.8.0:
pip install statsmodels==0.9.0rc1
I met the same situation, and the install process recommended in Nish's url didn't work for me. Here's how did I solve the problem (I'm using Mac OS).
Remove statsmodels library first, if you have installed: pip uninstall statsmodels
In your terminal, type git init, to initiate git
Then type git clone git://github.com/statsmodels/statsmodels.git
Change the directory to statsmodels using “cd statsmodels”
Next type python setup.py install
python setup.py build_ext --inplace
Now type python in your terminal and then type from statsmodels.tsa.api import ExponentialSmoothing, to see whether it can import successfully
If using conda, this will make statsmodel 0.9.0
conda update statsmodels
It is the wrong import,
Try
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
You can follow the steps mentioned below:
Step 1: Remove statsmodel using pip uninstall statsmodel
Step 2: Install git from here: https://git-scm.com/downloads
Step 3: Follow steps mentioned under "Installing library(statsmodels)" from link mentioned below:
https://www.analyticsvidhya.com/blog/2018/02/time-series-forecasting-methods/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+AnalyticsVidhya+%28Analytics+Vidhya%29
I was playing with the decision tree algorithm and trying to plot the tree. However the IDE reported following error:
Couldn't import dot_parser, loading of dot files will not be possible.
<class 'pandas.core.frame.DataFrame'>
Traceback (most recent call last):
File "C:/Users/s152730/Desktop/exe1.py", line 70, in <module>
graph = pydot.graph_from_dot_data(test.getvalue())
File "C:\Python27\lib\site-packages\pydot.py", line 220, in graph_from_dot_data
return dot_parser.parse_dot_data(data)
NameError: global name 'dot_parser' is not defined
I have no idea how to deal with this problem because I've tried to uninstall and reinstall both pydot dan pyparsing, which was proposed in other answers, but it didn't help.
Here is my code:
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import ExtraTreeClassifier
from sklearn import tree
from sklearn.externals.six import StringIO
import pydot
from IPython.display import Image
test = StringIO()
tree.export_graphviz(clf, out_file=test, feature_names = attribute_names)
graph = pydot.graph_from_dot_data(test.getvalue())
graph.writepng('test.png')
image(filename = 'test.png')
I am using python2.7 and running on PyCharm, the OS is win8.1.
Thanks for your help.
It seems your error is that you are missing part of a library (pyparsing) because of incorrect order of installation.
See here and here
Obvious to the initiated but not to the newbie: The workaround is to
install pyparsing < 2.0.0 prior to installing pydot (or a package
that depends on pydot.)
$ pip install pyparsing==1.5.7
The solution seems to be to first remove pydot and pyparsing, and then to install pyparsing first, then pydot.
The versions of which to install will most likely change in the future, so at the moment it seems you need to run something like the following: (taken from this lovely answer)
pip uninstall pyparsing
pip install -Iv https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz#md5=9be0fcdcc595199c646ab317c1d9a709
pip install pydot
For me, I found a great tip is to install pydotplus instead, as it is compatible with pyparsing v2.0 and greater. It also has the advantage that it can work with the graphviz installation from Anaconda. I'm using Anaconda v2.4.1 and on Windows 7 x64 and Graphviz 2.38 installed using condas.
I've just update my pydotto 1.2.3, and the error disappears.
sudo pip install -U pydot