1

I'm trying to learn how to create a machine learning API with Flask, however, following this tutorial, the following error appears when I type the command python app.py:

Traceback (most recent call last):   
File "C:\Users\Breno\Desktop\flask-api\app.py", line 24, in <module>
        model = p.load(open(modelfile, 'rb'))
ModuleNotFoundError: No module named 'sklearn.tree.tree'

My code:

from flask import Flask, request, redirect, url_for, flash, jsonify
import numpy as np
import pickle as p
import pandas as pd
import json
#from sklearn.tree import DecisionTreeClassifier

app = Flask(__name__)


@app.route('/api/', methods=['POST'])
def makecalc():
    j_data = request.get_json()

    prediction = np.array2string(model.predict(j_data))

    return jsonify(prediction)


if __name__ == '__main__':

    modelfile = 'models/final_prediction.pickle'    

    model = p.load(open(modelfile, 'rb'))

    app.run(debug=True,host='0.0.0.0')

Could someone help me please?

davidism
  • 121,510
  • 29
  • 395
  • 339
Breno
  • 39
  • 1
  • 5
  • 1
    How did you create/save `models/final_prediction.pickle`? – Yevhen Kuzmovych Feb 03 '21 at 14:45
  • well, this site has a link to the repository of the original project, so I downloaded the project clone and used this "models / final_prediction.pickle", focusing only on the creation part of the api with Flask. – Breno Feb 03 '21 at 15:00

3 Answers3

2

Pickles are not necessarily compatible across scikit-learn versions so this behavior is expected (and the use case is not supported). For more details, see https://scikit-learn.org/dev/modules/model_persistence.html#model-persistence. Replace pickle by joblib. by example :

>>> from sklearn import svm
>>> from sklearn import datasets
>>> clf = svm.SVC()
>>> X, y= datasets.load_iris(return_X_y=True)
>>> clf.fit(X, y)
SVC()

>>> from joblib import dump, load
>>> dump(clf, open('filename.joblib','wb'))
>>> clf2 = load(open('filename.joblib','rb'))
>>> clf2.predict(X[0:1])
array([0])
>>> y[0]
0
Bienvenu
  • 21
  • 3
2

For anyone coming across this issue (perhaps dealing with code written long ago), sklearn.tree.tree is now under sklearn.tree (as from v0.24). This can be see from the import error warning:

from sklearn.tree.tree import BaseDecisionTree
/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.tree.tree module is  deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.tree. Anything that cannot be imported from sklearn.tree is now part of the private API.
  warnings.warn(message, FutureWarning)

Instead, use:

from sklearn.tree import BaseDecisionTree
arilwan
  • 3,374
  • 5
  • 26
  • 62
0

The problem is with the version of sklearn. Module sklearn.tree.tree is removed since version 0.24. Most probably, your model has been generated with the older version. Try installing an older version of sklearn:

pip uninstall scikit-learn
pip install scikit-learn==0.20.4
Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
  • I highly doubt that there ever was such a module, and certainly it does not seem to exist in v0.20: https://scikit-learn.org/0.20/modules/classes.html#module-sklearn.tree – desertnaut Feb 03 '21 at 17:12
  • @desertnaut I couldn't find this module myself. But per [this question](https://stackoverflow.com/questions/60598050/the-sklearn-tree-tree-module-is-deprecated-in-version-0-22-and-will-be-removed-i?noredirect=1&lq=1), there was a warning about its deprecation. It might be some internal module that wasn't documented. And was deprecated and now removed. – Yevhen Kuzmovych Feb 04 '21 at 09:01
  • @desertnaut And I guess, [this](https://github.com/scikit-learn/scikit-learn/blob/0.20.X/sklearn/tree/tree.py) is the module. Which is not present in the [latest](https://github.com/scikit-learn/scikit-learn/tree/main/sklearn/tree) version. – Yevhen Kuzmovych Feb 04 '21 at 09:06