Issue
Exectued the followwing code
from flask import Flask, request, jsonify, make_response
from flask_restplus import Api, Resource, fields
from sklearn.externals import joblib
import numpy as np
import sys
flask_app = Flask(__name__)
app = Api(app = flask_app,
version = "1.0",
title = "ML React App",
description = "Predict results using a trained model")
name_space = app.namespace('prediction', description='Prediction APIs')
model = app.model('Prediction params',
{'Recency': fields.Float(required = True,
description="Recency Value",
help="Recency Value cannot be blank"),
'Frequency': fields.Float(required = True,
description="Frequency Value",
help="Frequency Value cannot be blank"),
'Monetary': fields.Float(required = True,
description="Monetary Value",
help="Monetary Value cannot be blank"),
})
classifier = joblib.load('classifier.joblib')
@name_space.route("/")
class MainClass(Resource):
def options(self):
response = make_response()
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add('Access-Control-Allow-Headers', "*")
response.headers.add('Access-Control-Allow-Methods', "*")
return response
@app.expect(model)
def post(self):
try:
formData = request.json
data = [val for val in formData.values()]
prediction = classifier.predict(np.array(data).reshape(1, -1))
types = { 0: "Loyal", 1: "NotLoyal"}
response = jsonify({
"statusCode": 200,
"status": "Prediction made",
"result": "The type of Loyalty is: " + types[prediction[0]]
})
response.headers.add('Access-Control-Allow-Origin', '*')
return response
except Exception as error:
return jsonify({
"statusCode": 500,
"status": "Could not make prediction",
"error": str(error)
})
Recieved the following error.
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\flask\cli.py", line 240, in locate_app
import(module_name)
File "D:\dilipan\clg\proj\ML-React-App-Template-master\service\app.py", line 3, in
from sklearn.externals import joblib
ImportError: cannot import name 'joblib' from 'sklearn.externals' (d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\sklearn\externals__init__.py)
I executed the same in a different pc 2 months ago and it was working fine.
Could not import joblib from sklearn.externals
Any would be appreciated. Thanks in advance.
Edit :
I tried using :
import joblib
instead of
from sklearn.externals import joblib
it gave me the following errors
File "c:\users\dilipan\appdata\local\programs\python\python38-32\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\dilipan\appdata\local\programs\python\python38-32\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "D:\dilipan\clg\proj\ML-React-App-Template-master\service\venv\Scripts\flask.exe\__main__.py", line 7, in <module>
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\flask\cli.py", line 967, in main
cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\flask\cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\click\core.py", line 782, in main
rv = self.invoke(ctx)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\click\core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\click\core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\click\core.py", line 610, in invoke
return callback(*args, **kwargs)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\click\decorators.py", line 73, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\click\core.py", line 610, in invoke
return callback(*args, **kwargs)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\flask\cli.py", line 848, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\flask\cli.py", line 305, in __init__
self._load_unlocked()
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\flask\cli.py", line 330, in _load_unlocked
self._app = rv = self.loader()
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\flask\cli.py", line 392, in load_app
app = locate_app(self, import_name, None, raise_if_not_found=False)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "D:\dilipan\clg\proj\ML-React-App-Template-master\service\app.py", line 27, in <module>
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\joblib\numpy_pickle.py", line 585, in load
obj = _unpickle(fobj, filename, mmap_mode)
File "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\joblib\numpy_pickle.py", line 504, in _unpickle
obj = unpickler.load()
File "c:\users\dilipan\appdata\local\programs\python\python38-32\lib\pickle.py", line 1210, in load
dispatch[key[0]](self)
File "c:\users\dilipan\appdata\local\programs\python\python38-32\lib\pickle.py", line 1587, in load_reduce
stack[-1] = func(*args)
File "sklearn\tree\_tree.pyx", line 607, in sklearn.tree._tree.Tree.__cinit__
ValueError: Buffer dtype mismatch, expected 'SIZE_t' but got 'long long'
Solution
You can import joblib directly instead of importing sklearn.externals, since it is deprecated in latest version of scikitlearn, and joblib can be imported directly -
import joblib
Answered By - Rohit upadhyay
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.