Python 3.6 AttributeError:模块“statsmodels”没有属性“compat”
Posted
技术标签:
【中文标题】Python 3.6 AttributeError:模块“statsmodels”没有属性“compat”【英文标题】:Python 3.6 AttributeError: module 'statsmodels' has no attribute 'compat' 【发布时间】:2018-11-09 11:21:19 【问题描述】:对 Python 完全陌生。在这个错误上没有看到任何命中。这是我所做的,几乎是击键:
-
干净安装 Python 3.6。
pip install -U statsmodels
pip install scipy
pip install numpy
pip install statsmodels --upgrade
(在 Python 中):import statsmodels.api as sm -> "AttributeError: module 'statsmodels' has no attribute 'compat'"
有什么建议吗?我只是想通过https://towardsdatascience.com/simple-and-multiple-linear-regression-in-python-c928425168f9 上的多元回归教程。完整的追溯如下。
提前致谢。
>>> import statsmodels.api as sm
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dataylor\AppData\Local\Programs\Python\Python36-32\lib\site-packages\statsmodels\api.py", line 35, in <module>
from .stats import api as stats
File "C:\Users\dataylor\AppData\Local\Programs\Python\Python36-32\lib\site-packages\statsmodels\stats\api.py", line 65, in <module>
from .mediation import Mediation
File "C:\Users\dataylor\AppData\Local\Programs\Python\Python36-32\lib\site-packages\statsmodels\stats\mediation.py", line 22, in <module>
import statsmodels.compat.pandas as pdc # pragma: no cover
AttributeError: module 'statsmodels' has no attribute 'compat'
【问题讨论】:
statsmodels
似乎没有正确安装。可能值得使用pip uninstall statsmodels
卸载它,然后使用pip install statsmodels
再次安装
我想就是这样!谢谢!
如果下面的答案解决了您的问题,请考虑点击帖子旁边的勾号。这让社区知道您的问题已得到解答。
【参考方案1】:
将@Will Kneeling 的评论转化为有类似问题的用户的答案。
Statsmodels
软件包似乎没有正确安装。尝试卸载并重新安装软件包,如下所示:
pip uninstall statsmodels
pip install statsmodels
【讨论】:
【参考方案2】:如果您使用的是jupyter
,请尝试重新启动笔记本服务器。
通常pip install <package>
可以毫无问题地使用该软件包,但对于statsmodels
,我一直收到上述错误,直到我重新启动笔记本服务器。
【讨论】:
【参考方案3】:在我的情况下,也使用 Jupyter Notebook,解决方案是使用:
import statsmodels.api as sm
而不是
import statsmodels as sm
这是推荐的方法(根据documentation),因为 statsmodels.api 是公共访问,而 statsmodels(没有 api)可能会停止工作。在我的例子中,我使用了 GLM 函数。
【讨论】:
使用import statsmodels as sm
而不是 import statsmodels.api as sm
适用于我的情况。以上是关于Python 3.6 AttributeError:模块“statsmodels”没有属性“compat”的主要内容,如果未能解决你的问题,请参考以下文章
初学者 Python:AttributeError:'list' 对象没有属性
Python:AttributeError:'str'对象没有属性'datetime'
Python类继承:AttributeError:'[SubClass]'对象没有属性'xxx'