Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I import statsmodels directly?

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'
like image 627
Gabriel Avatar asked Aug 06 '15 20:08

Gabriel


1 Answers

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.)

like image 101
Josef Avatar answered Oct 16 '22 19:10

Josef