从 sklearn.externals 导入 joblib 时收到导入错误

Recieved import error while importing joblib from sklearn.externals

执行了下面的代码

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

收到以下错误。

错误:导入时 "app",引发了 ImportError:

回溯(最近调用最后):
文件 "d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\flask\cli.py",第 240 行,在 locate_app
导入(module_name)
文件 "D:\dilipan\clg\proj\ML-React-App-Template-master\service\app.py",第 3 行,位于 从 sklearn.externals 导入作业库
导入错误:无法从 'sklearn.externals' (d:\dilipan\clg\proj\ml-react-app-template-master\service\venv\lib\site-packages\sklearn\externals__init__.py)

导入名称 'joblib'

我 2 个月前在另一台电脑上执行了相同的操作,并且运行良好。

无法从 sklearn.externals

导入 joblib

任何人都将不胜感激。提前致谢。

编辑:

我尝试使用: import joblib 代替 from sklearn.externals import joblib 它给了我以下错误

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'

您可以直接导入 joblib 而不是导入 sklearn.externals,因为它在最新版本的 scikitlearn 中已弃用,可以直接导入 joblib -

import joblib