rishabh.bhardwaj
rishabh.bhardwaj

Reputation: 378

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).

Upvotes: 6

Views: 1295

Answers (1)

Julia Meshcheryakova
Julia Meshcheryakova

Reputation: 3262

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.

Upvotes: 2

Related Questions