【问题标题】:Python 3.6 AttributeError: module 'statsmodels' has no attribute 'compat'Python 3.6 AttributeError:模块“statsmodels”没有属性“compat”
【发布时间】:2018-11-09 11:21:19
【问题描述】:

对 Python 完全陌生。在这个错误上没有看到任何命中。这是我所做的,几乎是击键:

  1. 干净安装 Python 3.6。
  2. pip install -U statsmodels
  3. pip install scipy
  4. pip install numpy
  5. pip install statsmodels --upgrade
  6. (在 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 再次安装
  • 我想就是这样!谢谢!
  • 如果下面的答案解决了您的问题,请考虑点击帖子旁边的勾号。这让社区知道您的问题已得到解答。

标签: python


【解决方案1】:

将@Will Kneeling 的评论转化为有类似问题的用户的答案。

Statsmodels 软件包似乎没有正确安装。尝试卸载并重新安装软件包,如下所示:

pip uninstall statsmodels 
pip install statsmodels

【讨论】:

    【解决方案2】:

    如果您使用的是jupyter,请尝试重新启动笔记本服务器

    通常pip install &lt;package&gt; 可以毫无问题地使用该软件包,但对于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 适用于我的情况。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2020-05-16
      • 2019-07-17
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多