导入路径和结构¶
我们提供两种从 statsmodels 导入函数和类的方法:
-
允许标签补全
-
避免导入不必要的模块和命令
API导入用于交互使用¶
对于交互式使用,推荐的导入方式是:
import statsmodels.api as sm
导入 statsmodels.api 将会加载 statsmodels 的大部分公共部分。 这使得大多数函数和类在不需要太多层级的情况下就可以方便地使用,而不会使 “sm” 命名空间过于拥挤。
要查看可用的函数和类,您可以输入以下内容(或使用 IPython、Spyder、IDLE 等的命名空间探索功能):
>>> dir(sm)
['GLM', 'GLS', 'GLSAR', 'Logit', 'MNLogit', 'OLS', 'Poisson', 'Probit', 'RLM',
'WLS', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
'add_constant', 'categorical', 'datasets', 'distributions', 'families',
'graphics', 'iolib', 'nonparametric', 'qqplot', 'regression', 'robust',
'stats', 'test', 'tools', 'tsa', 'version']
>>> dir(sm.graphics)
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'abline_plot', 'beanplot', 'fboxplot', 'interaction_plot', 'qqplot',
'rainbow', 'rainbowplot', 'violinplot']
>>> dir(sm.tsa)
['AR', 'ARMA', 'SVAR', 'VAR', '__builtins__', '__doc__',
'__file__', '__name__', '__package__', 'acf', 'acovf', 'add_lag',
'add_trend', 'adfuller', 'ccf', 'ccovf', 'datetools', 'detrend',
'filters', 'grangercausalitytests', 'interp', 'lagmat', 'lagmat2ds', 'kpss',
'pacf', 'pacf_ols', 'pacf_yw', 'periodogram', 'q_stat', 'range_unit_root_test',
'stattools', 'tsatools', 'var']
注释¶
The api 模块可能不包含 statsmodels 的所有公共功能。如果你发现有应该添加到 api 中的内容,请在 github 上提交问题或在邮件列表中报告。
statsmodels的子包包括api.py模块,这些模块主要用于收集这些子包所需的导入。subpackage/api.py文件被导入到statsmodels api中,例如
from .nonparametric import api as nonparametric
用户不需要直接加载subpackage/api.py模块。
程序的直接导入¶
statsmodels 子模块按主题排列(例如,discrete 用于离散选择模型,或 tsa 用于时间序列分析)。我们的目录树(简化后)看起来像这样:
statsmodels/
__init__.py
api.py
discrete/
__init__.py
discrete_model.py
tests/
results/
tsa/
__init__.py
api.py
tsatools.py
stattools.py
arima_process.py
vector_ar/
__init__.py
var_model.py
tests/
results/
tests/
results/
stats/
__init__.py
api.py
stattools.py
tests/
tools/
__init__.py
tools.py
decorators.py
tests/
可以导入的子模块包含一个空的__init__.py,除了一些用于运行子模块测试的测试代码。目的是在下一个版本中将所有目录更改为包含一个api.py和一个空的__init__.py。
导入示例¶
函数和类:
from statsmodels.regression.linear_model import OLS, WLS
from statsmodels.tools.tools import rank, add_constant
模块
from statsmodels.datasets import macrodata
import statsmodels.stats import diagnostic
带有别名的模块
import statsmodels.regression.linear_model as lm
import statsmodels.stats.diagnostic as smsdia
import statsmodels.stats.outliers_influence as oi
我们目前没有为子模块的别名制定惯例。
Last update:
Oct 16, 2024