Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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

Solution 2:

You can, you just have to import robust as well:

import statsmodels as sm
import statsmodels.robust

Then:

>>>sm.robust.scale.mad(a)
0.35630934336679576

robust is a subpackage of statsmodels, and importing a package does not in general automatically import subpackages (unless the package is written to do so explicitly).

Post a Comment for "Why Can't I Import Statsmodels Directly?"