You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FAQ, FAQ-D: I want to store (pickle) my model for prediction #1729
After we have estimated a model and obtained the results, we might want to pickle the results and model to use it later for prediction. To avoid storing all the data that was used in the estimation and in the generation of the results, we have added a remove_data method to the results instance, additional to save and load methods that do the pickling and unpickling.
remove_data will (is supposed to) remove all arrays that have a length of nobs, and in some cases additional attributes. The resulting "data-less" results and model instance will not be able to calculate any new results that need those arrays. However, predict and most inference only needs the stored parameters and covariance matrix, and will still be functioning correctly.
Note:
FAQ-D: How does it work, and what do we need to do to support it?
remove_data only removes attributes and attached arrays if they have been registered and does not try to determine automatically which attributes to delete. We have arrays or attributes in three main parts
model._data_attr registers a list of names for attributes of the model and the model.data. The default are endog and exog and the corresponding references in .data
results.._data_attr registers a list of names for attributes of the model instance
results.data_in_cache registers cached attributes of the results instance, that are stored in the cache and not directly as attributes.
Removing attributes or cache keys in remove_data is protected against non-existence and doesn't raise an error if a registered name or key cannot be found.
The main methods are in base.model.Model. The registered lists are added in the __init__ of model and results classes.
Unit tests for one case of each group of classes and where new models can be added are in /base/tests/test_shrink_pickle.py
this needs additional information about pickling with formulas, see #1263 and related PRs
main point:
Pickling a model with formula uses currently a work around because formula information cannot be pickled. We need to save the original data and recreate the design matrix from it and the formula.
Getting the correct eval environment is a bit fragile.
Activity
josef-pkt commentedon Jun 3, 2014
triggered to write the FAQ because I had forgotten about this in #1714 (comment)
josef-pkt commentedon Apr 27, 2015
this needs additional information about pickling with formulas, see #1263 and related PRs
main point:
Pickling a model with formula uses currently a work around because formula information cannot be pickled. We need to save the original data and recreate the design matrix from it and the formula.
Getting the correct eval environment is a bit fragile.
josef-pkt commentedon Oct 2, 2017
cross-link to comment that is also trying to explain this
#3290 (comment)