我想使用 logit 模型并尝试导入 statsmodels 库。我的版本:Python 3.6.8
我得到的最好建议是降级 scipy 但不清楚如何降级以及降级到哪个版本。请帮忙如何解决。 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'```
原文由 Bhavya Geethika 发布,翻译遵循 CC BY-SA 4.0 许可协议
更新:升级
statsmodels
现在将解决这个问题:pip install statsmodels --upgrade
。从 statsmodels 的 github repo 上的这个问题来看,解决方案似乎是将 SciPy 降级到 1.2 版(当前版本是 1.3,您似乎正在使用)。
至少对我来说,SciPy 1.2 在
scipy.misc
包中有factorial
函数。你可以做
如果您没有标准安装权限,请使用
--user
选项。也许您想避免使用 pip,因为您使用的是 Conda。您也应该能够在 Conda 中固定 scipy 的版本,但是如果您不打算将任何其他包添加到您的环境中,只需使用
pip
版本。当然,降级 SciPy 可能会在其他地方引起问题,但这很难预见,尤其是在不知道您安装了哪些其他包和依赖项的情况下;你只需要找出答案。为没有陷入依赖地狱而祈祷(因为你已经在家门口了)。
更奇怪的是,
scipy.misc.factorial
自1.0版以来已被弃用;scipy.special.factorial
应该改用。然而,在 1.2 版中导入并没有显示任何明确的警告,使用它也没有。这可以解释为什么
statsmodels
仍然使用旧的导入。下一个statsmodels
版本正在修复中。