Skip to content

Instantly share code, notes, and snippets.

@meisa233
Last active April 21, 2023 06:48
Show Gist options
  • Save meisa233/0ce2b68ee508961082fe628c3fd9bf01 to your computer and use it in GitHub Desktop.
Save meisa233/0ce2b68ee508961082fe628c3fd9bf01 to your computer and use it in GitHub Desktop.
Convert old pkl trained by old sklearn to new pkl

old sklearn version

scikit-learn           0.18.1

new sklearn version

scikit-learn           1.0.2
# use Python3.6 with old scikit-learn

import pickle
from sklearn.externals import joblib
def rename_attribute(obj, old_name, new_name):
    obj.__dict__[new_name] = obj.__dict__.pop(old_name)
# with open('/path/to/old.pkl', 'rb') as f:
#     pkl = pickle.load(f)
pkl = joblib.load('/path/to/old.pkl')
rename_attribute(pkl, 'n_support_', '_n_support')
rename_attribute(pkl, 'probA_', '_probA')
rename_attribute(pkl, 'probB_', '_probB')
setattr(pkl, 'break_ties', False)
with open('/path/to/new.pkl', 'wb') as f:
    pickle.dump(pkl, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment