24

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'```
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
Bhavya Geethika
  • 379
  • 1
  • 2
  • 9
  • 2
    factorial is in the default `math` module – Alec May 23 '19 at 21:44
  • 2
    @AlecA: I don't think that he's trying to import `factorial` directly. The error is given on (the only piece of code provided here) `import statsmodels.formula.api as smf`. – goodvibration May 23 '19 at 21:46
  • Yes @goodvibration its something to do with scipy. I did try importing math module anyway to see if it works and it doesn't. – Bhavya Geethika May 23 '19 at 21:50
  • https://github.com/statsmodels/statsmodels/issues/5747 – Will May 23 '19 at 21:54
  • Thank you for pointing that link - I already saw that link and referenced it in my question. I am not sure if that link provides a resolution - it is unclear. Can you tell me exactly what should fix it? – Bhavya Geethika May 23 '19 at 22:01
  • This is happening for me as i was using `TPOT` package which require scipy > =1.3 and it is not supportive for statsmodel = 0.10.0 – yogesh agrawal Jan 02 '20 at 07:23

4 Answers4

52

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.

9769953
  • 10,344
  • 3
  • 26
  • 37
  • Could you please let me know what may have caused people to downvote this post - should I edit the question differently or improve anything about it? – Bhavya Geethika May 30 '19 at 19:08
  • I downgraded to `scipy 1.2.0` from `1.3.1` and still get the precise error mentioned at the top of page. I have `pip 3.7.3`. However, once I *uninstalled* and reinstalled `statsmodels`, I was good to go (per the suggestion below. – Edmund's Echo Oct 16 '19 at 03:50
9

Thanks @9769953.

  1. pip3 uninstall statsmodels # make sure to remove old versions
  2. pip3 install statsmodels==0.10.0rc2 --pre --user # install release candidate of statsmodels
  3. 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 :)

Clemens
  • 653
  • 6
  • 8
  • this is not necessary anymore as statsmodels is now already at version 1.14.0, the solution by Giorgos Myrianthous suffices – Robert Dec 01 '20 at 18:42
5
pip install statsmodels --upgrade 

did the trick for me

Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
1

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.

Tonechas
  • 13,398
  • 16
  • 46
  • 80