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
Pickled file size issues even with remove_data = True #4174
Wasn't able to comment on the original ticket (#3563), but wondering if there's been any additional troubleshooting to just saving the attributes necessary for the .predict() method. remove_data still only reduces my filesize by ~50%, so not sure if there are other arrays I should be setting to zero (i.e. residuals) in order to extract only the data I need.
Code:
q = pd.DataFrame({'x': range(1,50000000)})
q['y']=2*q.x
q['z']=q.x**2
j = smf.ols('y ~ x + z', data = q).fit()
j.save('test2.pickle', remove_data = True)
the unit tests have this comment
"...\statsmodels\base\tests\test_shrink_pickle.py" TestPickleFormula cls.l_max = 900000 # have to pickle endo/exog to unpickle form.
This seems to part of the hack to work around that the formula information cannot be pickled.
Hey Josef, thanks for the quick response here and for digging in to the unit tests! You're absolutely right on the large arrays within the j.model.data attr. I actually did manage to reduce the pickle file size while preserving the predict method with the following addition:
Predict fails if these additional attributes are set to None, so I'm just truncating them to empty dataframes. .summary() method breaks but .predict() is still accurate, and this reduced the pickled file size from 6.5GB to 4KB.
This is a pretty hacky way of doing it, but I'm mostly just trying to fit a model for later productionalization so in this specific case I'm good.
You need to check that the formula information is correctly recreated in your case.
The main point are that categorical and other transformation might depend on the exog to get the "parameters" for those transforms, e.g. the set of levels and encoding in categorical variables or mean and scale if zscoring. Those are "stateful transforms" in patsy, that need to hold on to the state or parameters of the transformation and that unpickling should recreate, but might not always be able to do so correctly.
If you don't have any of these stateful transforms, then the content of the truncated orig_exog doesn't matter, just names and number of columns will be relevant.
about summary: I don't think I ever checked this, the unit tests call summary before remove_data and pickling to create most of the default cached arrays.
My guess is that summary would work for other Models other than linear models (and outside of tsa) because they don't have the diagnostic third table. Those are created in summary, and there is currently no safeguard to remove that part of the summary tables if not available or cache the statistics.
Ack you're right on the categorical vars, good call on summary failing after .remove_data(). Replacing a truncate with a .drop_duplicates seems to solve, still very hacky but should hopefully still mitigate datasize assuming not too many categories/categorical features.
import pickle, statsmodels.formula.api as sm
def predict_pickle(mod, fname):
#Reducing size
mod.remove_data()
mod.model.data.orig_endog = mod.model.data.orig_endog.truncate(after = 0)
mod.model.data.orig_exog = mod.model.data.orig_endog.truncate(after = 0)
#Identify columns that can be categorical (can expand to other dtypes as needed)
categs = list(mod.model.data.frame.select_dtypes(include=['object','category']).columns)
mod.model.data.frame = mod.model.data.frame.drop_duplicates(subset=categs)
mod.save(fname)
q = pd.DataFrame({'x': range(1,500001), 'y':np.random.rand(500000,1)})
q['z']=q.x**2
q['a']=['A','B']*(500000/2)
j = smf.ols('y ~ x + z + a', data = q).fit()
test = pd.DataFrame({'x':[0,1],'z':[1,2],'a':['A','B']})
j.predict(test)
predict_pickle(j,'test.pickle')
#Testing pickled object on predict method
fh = open('test.pickle', 'rb')
results2 = pickle.load(fh)
fh.close()
results2.predict(test)
Activity
josef-pkt commentedon Dec 21, 2017
My guess is that model.data might have some extra arrays, that are only there with pandas and formulas.
It's possible or likely that I never looked at that case. I will have to check the code and unit test to see what is covered.
Check
vars(j.model.datato see what large arrays or dataframes are left, and maybe use a smaller x to investigate.josef-pkt commentedon Dec 21, 2017
Unfortunately, that's intentional
the unit tests have this comment
"...\statsmodels\base\tests\test_shrink_pickle.py" TestPickleFormula
cls.l_max = 900000 # have to pickle endo/exog to unpickle form.This seems to part of the hack to work around that the formula information cannot be pickled.
bsaunders23 commentedon Dec 21, 2017
Hey Josef, thanks for the quick response here and for digging in to the unit tests! You're absolutely right on the large arrays within the j.model.data attr. I actually did manage to reduce the pickle file size while preserving the predict method with the following addition:
Predict fails if these additional attributes are set to None, so I'm just truncating them to empty dataframes. .summary() method breaks but .predict() is still accurate, and this reduced the pickled file size from 6.5GB to 4KB.
This is a pretty hacky way of doing it, but I'm mostly just trying to fit a model for later productionalization so in this specific case I'm good.
josef-pkt commentedon Dec 21, 2017
You need to check that the formula information is correctly recreated in your case.
The main point are that categorical and other transformation might depend on the exog to get the "parameters" for those transforms, e.g. the set of levels and encoding in categorical variables or mean and scale if zscoring. Those are "stateful transforms" in patsy, that need to hold on to the state or parameters of the transformation and that unpickling should recreate, but might not always be able to do so correctly.
If you don't have any of these stateful transforms, then the content of the truncated orig_exog doesn't matter, just names and number of columns will be relevant.
about summary: I don't think I ever checked this, the unit tests call summary before remove_data and pickling to create most of the default cached arrays.
My guess is that summary would work for other Models other than linear models (and outside of tsa) because they don't have the diagnostic third table. Those are created in summary, and there is currently no safeguard to remove that part of the summary tables if not available or cache the statistics.
bsaunders23 commentedon Dec 21, 2017
Ack you're right on the categorical vars, good call on summary failing after .remove_data(). Replacing a truncate with a .drop_duplicates seems to solve, still very hacky but should hopefully still mitigate datasize assuming not too many categories/categorical features.