0

I have a generator object obtained by sklearn http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingClassifier.html

df_fit = clf.fit(df_training, df_label.values.ravel())
generator=clf.staged_decision_function(df_training)

However, I wish to persist the Generator object in generator variable in some file type so I can retrieve them by reading the file back and save into a variable. I had tried using pickle with

pickle.dumps(generator)

The approach failed and from Google I learnt that we cannot serialize generator object in pickle. I am trying to save it in txt but I don't think it is the proper way to do. Any idea?

Mervyn Lee
  • 1,957
  • 4
  • 28
  • 54
  • The only way to preserve a Python object is to serialize it. So, you just answered your question. – DYZ Aug 03 '17 at 05:39
  • Is there a way you can extract the state from the generator (and then have a mechanism to set the exact same state again)? `pickle` relies on `__getstate__` and `__setstate__` methods, which are probably not implemented for generators. – Zizouz212 Aug 03 '17 at 05:40
  • @DYZ Except for generators... https://stackoverflow.com/a/7180424/4293417 – Zizouz212 Aug 03 '17 at 05:40
  • I think I solved it with convert the generator into list first. ... WIll get back to you after I try it. Thank you for the reply – Mervyn Lee Aug 03 '17 at 05:41
  • @MervynLee If you want to pickle a generator, you can use `dill`. Install it first with `pip install dill`. Then use `dill.dump(...)`. – cs95 Aug 03 '17 at 05:42
  • One workaround here is that maybe you can pickle the `clf` with it and call the `staged_decision_function()` again when loading. Scikit-learn docs recommend using [joblib for its object](http://scikit-learn.org/stable/modules/model_persistence.html). – Vivek Kumar Aug 03 '17 at 05:45

0 Answers0