为什么我不能直接导入 statsmodels? [英] Why can't I import statsmodels directly?
问题描述
我当然在这里遗漏了一些非常明显的东西,但为什么这样做:
a = [0.2635,0.654654,0.365,0.4545,1.5465,3.545]将 statsmodels.robust 导入为 rb打印 rb.scale.mad(a)0.356309343367
但这不会:
将 statsmodels 导入为 sm打印 sm.robust.scale.mad(a)---------------------------------------------------------------------------AttributeError 回溯(最近一次调用最后一次)<ipython-input-5-1ce0c872b0be>在 <module>()---->1 打印 statsmodels.robust.scale.mad(a)AttributeError: 'module' 对象没有属性 'robust'
长答案见 http://www.statsmodels.org/stable/importpaths.html
Statsmodels故意大部分是空的__init__.py
,但有一个通过api.py
的并行导入集合.
交互式工作的推荐导入 import statsmodels.api as sm
导入几乎所有的 statsmodels、numpy、pandas 和 patsy,以及大部分 scipy.这在冷启动时很慢.
如果我们只想导入 statsmodels 的特定部分,那么我们不需要导入所有这些额外内容.拥有空的 __init__.py
意味着我们可以只导入一个模块(这当然会导入该模块的依赖项).
例如from statsmodels.robust.scale import mad
或将 statmodels.robust scale 导入为 smscale
smscale.mad(...)
(小警告:如果内部结构发生变化,一些非常低级别的导入可能不会始终保持向后兼容.但是,一般策略是在一两个版本中弃用功能,同时保持旧的访问结构.)>
I'm certainly missing something very obvious here, but why does this work:
a = [0.2635,0.654654,0.365,0.4545,1.5465,3.545]
import statsmodels.robust as rb
print rb.scale.mad(a)
0.356309343367
but this doesn't:
import statsmodels as sm
print sm.robust.scale.mad(a)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-1ce0c872b0be> in <module>()
----> 1 print statsmodels.robust.scale.mad(a)
AttributeError: 'module' object has no attribute 'robust'
Long answer see http://www.statsmodels.org/stable/importpaths.html
Statsmodels has intentionally mostly empty __init__.py
but has a parallel import collection through the api.py
.
The recommended import for interactive work import statsmodels.api as sm
imports almost all of statsmodels, numpy, pandas and patsy, and large parts of scipy. This is slooow on cold start.
If we want to import just a specific part of statsmodels, then we don't need to import all these extras. Having empty __init__.py
means that we can import just a single module (which of course imports the dependencies of that module).
e.g. from statsmodels.robust.scale import mad
or
import statmodels.robust scale as smscale
smscale.mad(...)
(Small caveat: Some of the very low level imports might not remain always backwards compatible if the internal structure changes. However, the general policy is to deprecate functions over one or two releases while maintaining the old access structure.)
这篇关于为什么我不能直接导入 statsmodels?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!