ImportError: cannot import name 'factorial' - python

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.

Related

Cannot import name '_centered' from 'scipy.signal.signaltools'

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

ImportError: cannot import name ExponentialSmoothing

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

Python Sklearn.Model_Selection giving error unable to import comb

I am importing train_test_split as:
from sklearn.model_selection import train_test_split and it is giving an error cannot import name 'comb'.
The versions I am using are scipy 0.18.1 and sklearn 0.17.1
Below is the detail of error, please guide here if you feel something is wrong.
Traceback (most recent call last):
File "<ipython-input-21-e45e815fd516>", line 1, in <module>
from sklearn import model_selection
File "C:\Users\rahulsharma53\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\model_selection\__init__.py", line 1, in <module>
from ._split import BaseCrossValidator
File "C:\Users\rahulsharma53\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 31, in <module>
from ..utils.fixes import signature, comb
ImportError: cannot import name 'comb
The suggestion in the comment above worked for me.
scikit-learn version 0.17.1 doesn't have the 'model-selection' module (it has instead the 'cross_validation' module, reference here). Since you have Anaconda installed, trying to upgrade scikit-learn to the newest version using the pip installer may cause a mismatch with the installed scipy and numpy versions (upgrading them with the pip installer wont solve the problem). The solution is to upgrade all three libraries using anaconda's installer, but before that all existing versions must be uninstalled, first using pip (in case, like me, you already went the pip route) then using conda.
Following the suggestions in the referred link:
pip uninstall:
pip uninstall numpy scipy -y
pip uninstall scikit-learn -y
conda uninstall:
conda uninstall numpy scipy scikit-learn -y
conda install:
conda install numpy scipy scikit-learn -y
Don't forget to restart Anaconda before retrying your import clause.

NameError: global name 'dot_parser' is not defined

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

ImportError: No module named version in Astropy

I currently have an anaconda installation of of Python, which includes astropy and numpy among other useful packages. I recently updated my Astropy individually through pip, by running
pip install --upgrade astropy
After this silly thing that I probably should not have done (I should have upgrades the entire anaconda package), my pyspeckit package stopped working, claiming it could not find the version.py in astropy.
This is the error I get:
/Users/saracamnasio/Research/code/MC_test.py in <module>()
5 import utilities as u
6 import BDdb
----> 7 import pyspeckit
8 import StringIO
9 import corner
/Users/saracamnasio/Research/code/pyspeckit/pyspeckit/__init__.py in <module>()
8
9 if not _ASTROPY_SETUP_:
---> 10 from version import version as __version__
11 import spectrum
12 import specwarnings
ImportError: No module named version
I tried to uninstall and reinstall astropy, as well as update anaconda independently but it's not working to fix it. Suggestions?
Evert's comment is most likely the correct answer: just update pyspeckit. The version you're using is out of date and has some potential inconsistencies in how it does relative imports.
However, what you have uncovered is, if not a bug, definitely not a feature, so it will be removed soon:
https://github.com/pyspeckit/pyspeckit/pull/134

Categories