Skip to content Skip to sidebar Skip to footer

Modulenotfounderror: No Module Named 'sklearn.tree.tree'

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

Solution 1:

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

Solution 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 moduleis  deprecated in version 0.22and 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.treeimportBaseDecisionTree

Solution 3:

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

Post a Comment for "Modulenotfounderror: No Module Named 'sklearn.tree.tree'"