Skip to content

FAQ, FAQ-D: I want to store (pickle) my model for prediction #1729

Open

Description

@josef-pkt

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

Activity

  1. josef-pkt commented on Jun 3, 2014

    @josef-pkt
    MemberAuthor

    triggered to write the FAQ because I had forgotten about this in #1714 (comment)

  2. josef-pkt commented on Apr 27, 2015

    @josef-pkt
    MemberAuthor

    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.

  3. josef-pkt commented on Oct 2, 2017

    @josef-pkt
    MemberAuthor

    cross-link to comment that is also trying to explain this
    #3290 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions