[英]Python statsmodels OLS: how to save learned model to file
我想學習使用Python的statsmodels庫中的普通最小二乘模型,描述在這里 。
sm.OLS.fit()返回學習的模型。 有沒有辦法將其保存到文件並重新加載? 我的訓練數據很大,學習模型大約需要半分鍾。 所以我想知道OLS模型中是否存在任何保存/加載功能。
我在模型對象上嘗試了repr()
方法,但它沒有返回任何有用的信息。
模型和結果實例都有一個保存和加載方法,因此您不需要直接使用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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.