Question

I created a model and I saved it in a pickle file using the Algorithm SVR(Support Vector Regression)

import pickle
pickle.dump(model,open('carb patients data/Pickles/svr.pickle', 'wb'))

In jupyter notebook it gives an error

can't pickle _thread.RLock objects

So I converted that jupyter file in to a .py file and downloaded it and executed using the Python Idle. Then it got saved in that particular location. But when I try to load my pickle file from another Jupyter Notebook it gives an error called,

No module named 'sklearn.svm._classes'

The code how i tried to load the pickle file,

from flask import Flask, request, redirect, url_for, flash, jsonify
import numpy as np
import pickle as p
import json
from sklearn import ensemble
app = Flask(__name__)


@app.route('/api/', methods=['POST'])
def makecalc():
  data = request.get_json()
  prediction = np.array2string(model.predict(data))

  return jsonify(prediction)

if __name__ == '__main__':
   modelfile = 'Clustering Patients/carb patients data/Pickles/svr.pickle'
   model = p.load(open(modelfile, 'rb'))
   app.run(debug=True, host='0.0.0.0')

Any thing I have done wrong or is it an issue in the Jupyter Notebook? How can I load my saved pickle file?

Was it helpful?

Solution

According to the scikit-learn model persistence docs, it may be better to use joblib instead:

Save model

from joblib import dump

dump(model, 'filename.joblib')

Load model

from joblib import load

model = load('filename.joblib') 
Licensed under: CC-BY-SA with attribution
Not affiliated with datascience.stackexchange
scroll top