I have a trained model which I have exported as a pickle file. I am trying to use the pickle file in my python file which is running on flask. However, I cannot pass parameters as the file is giving an error. When I use the same code in my jupyter notebook, the parameters are passed and a prediction is given. However when it comes to running it on flask, it gives the following error:
Traceback (most recent call last):
File "C:\model1\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\model1\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\model1\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\model1\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\model1\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\model1\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\model1\hello.py", line 26, in predict
model = pickle.load(open("prediction.pkl","rb"))
File "sklearn\neighbors\binary_tree.pxi", line 1152, in sklearn.neighbors.kd_tree.BinaryTree.__setstate__
File "sklearn\neighbors\binary_tree.pxi", line 235, in sklearn.neighbors.kd_tree.get_memview_ITYPE_1D
ValueError: Buffer dtype mismatch, expected 'ITYPE_t' but got 'long long'
My hello.py file is as follows:
import pickle
import numpy as numpy
from decimal import Decimal
from flask import Flask, request, json
from sklearn.externals import joblib
app = Flask(__name__)
@app.route('/predict', methods=['POST'])
def predict():
print(request.form)
features = request.form["features"]
features = json.loads(features)
features = numpy.array(features)
features = features.reshape(1, -1)
model = pickle.load(open("prediction.pkl","rb"))
prediction = model.predict(features).tolist()
print(features)
print(prediction)
return json.dumps({"Prediction":prediction})
if __name__ == '__main__':
app.run(host='127.0.0.1')
Can anyone suggest how to solve the error?
Hi i think your model has been trained on different sklearn library version and the loading model sklearn library a different version. or python version issue.