Python statsmodels OLS: how to save a learned model to a file

I am trying to learn the usual least squares model using the Python statsmodels library, as described here .

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

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

+7
source share
2 answers

Models and result instances have a save and load method, so you don't need to use the brine module directly.

Edit to add an example:

 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 Now we have added the load method to the main statsmodels APIs in master, so you can just do

 new_results = sm.load('longley_results.pickle') 
+15
source

I installed the statsmodels library and found that you can save values ​​using pickle module in python.

Models and results are selected using save / load, saving model data if necessary. [a source]

As an example:

Given that you have results stored in the results of a variable:

To save the file:

 import pickle with open('learned_model.pkl','w') as f: pickle.dump(results,f) 

To read the file:

 import pickle with open('learned_model.pkl','r') as f: model_results = pickle.load(f) 
+5
source

All Articles