Python 统计模型 OLS : how to save learned model to file

标签 python least-squares statsmodels

我正在尝试使用 Python 的 statsmodels 库学习普通的最小二乘模型,如所述 here .

sm.OLS.fit() 返回学习模型。有没有办法将其保存到文件并重新加载?我的训练数据很大,学习模型大约需要半分钟。所以我想知道 OLS 模型中是否存在任何保存/加载功能。

我在模型对象上尝试了 repr() 方法,但它没有返回任何有用的信息。

最佳答案

models和results实例都有save和load方法,不需要直接使用pickle模块。

编辑以添加示例:

import statsmodels.api as sm

data = sm.datasets.longley.load_pandas()

data.exog['constant'] = 1

results = sm.OLS(data.endog, data.exog).fit()
results.save("longley_results.pickle")

# we should probably add a generic load to the main namespace
from statsmodels.regression.linear_model import OLSResults
new_results = OLSResults.load("longley_results.pickle")

# or more generally
from statsmodels.iolib.smpickle import load_pickle
new_results = load_pickle("longley_results.pickle")

编辑 2 我们现在在 master 的主要 statsmodels API 中添加了一个 load 方法,所以你可以这样做

new_results = sm.load('longley_results.pickle')

关于Python 统计模型 OLS : how to save learned model to file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16420407/

相关文章:

python - numpy.linalg.lstsq 和 scipy.linalg.lstsq 有什么区别?

python - SciPy:使用 b 向量数组的逐元素非负最小二乘法

python - 三次贝塞尔曲线上的最小二乘拟合

python - ValueError : numpy. dtype 大小错误,尝试重新编译

python - 交互: and * term for formulas in StatsModels OLS regression之间的区别

python - 在子类中强制使用类变量

python - 有几个间隔的切片?

python-3.x - 使用 statsmodel 中的 arma_order_select_ic 选择 ARMA 模型订单

python - torch 服务加载模型失败

python - numpy 从 3d 数组中提取 3d 立方体