简体   繁体   中英

Model persistence in Scikit-Learn?

I am trying to save and load scikit-learn model but facing issues when the save and load are happening on different python versions. Here what I have tried:

  1. Using pickle to save a model in python3 and deserialize in python2.This works for some of the models like LR,SVM but it fails for KNN.

     >>> pickle.load(open("inPy3.pkl", 'rb')) #KNN model ValueError: non-string names in Numpy dtype unpickling
  2. Also, I tried to serialize and deserialize in json using jsonpickle but getting the following error.

     data = jsonpickle.encode(lr) #lr = logisticRegression Model jsonpickle.decode(data) AttributeError: 'dict' object has no attribute '__name__'

Also, I want to know if there is some utility which I can use to serialize and deserialize scikit-learn model objects to human readable format (json,xml,protobuf etc).

Instead of pickling whole models, you can extract and store their coefficients. Then load coefficients and init models with them.

Related to sklearn upgrade question . Similar approach will be valid for python versions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM