Why can't I import statsmodels directly?
Asked Answered
P

2

6

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'
Perfectible answered 6/8, 2015 at 20:1 Comment(2)
The library is setup to be imported as : import statsmodels.api as smCattail
I think this would make a very good answer.Perfectible
R
5

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

Roadblock answered 6/8, 2015 at 20:38 Comment(1)
I believe this is the actual answer as to why I can't directly import the library as I intended. Thanks!Perfectible
A
3

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

Anissaanita answered 6/8, 2015 at 20:4 Comment(2)
I see. But I still don't get why I can do import numpy as np; np.mean(a) but I can't do the same with statsmodels? Edit: Paul H seems to have the answer to that.Perfectible
numpy.mean is a function; statsmodel.robust is a (sub)package, so different rules will apply.Piddle

© 2022 - 2024 — McMap. All rights reserved.