Python statsmodels OLS:如何将学习到的模型保存到文件 [英] Python statsmodels OLS: how to save learned model to file

查看:64
本文介绍了Python statsmodels OLS:如何将学习到的模型保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 的 statsmodels 库学习一个普通的最小二乘模型,如 此处.

I am trying to learn an ordinary least squares model using Python's statsmodels library, as described here.

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

sm.OLS.fit() returns the learned model. Is there a way to save it to the file and reload it? My training data is huge and it takes around half a minute to learn the model. So I was wondering if any save/load capability exists in OLS model.

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

I tried the repr() method on the model object but it does not return any useful information.

推荐答案

模型和结果实例都有保存和加载方法,所以不需要直接使用pickle模块.

The models and results instances all have a save and load method, so you don't need to use the pickle module directly.

编辑以添加示例:

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

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

Edit 2 We've now added a load method to main statsmodels API in master, so you can just do

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

这篇关于Python statsmodels OLS:如何将学习到的模型保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆