MicrosoftDocs/ml-basics

cannot import name 'export_text' in sklearn.tree

WGabriel opened this issue · 1 comments

I am getting an error in this code snippet:

from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import export_text

# Train the model
model = DecisionTreeRegressor().fit(X_train, y_train)
print (model, "\n")

# Visualize the model tree
tree = export_text(model)
print(tree)

Error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-17-58c166972779> in <module>
      1 from sklearn.tree import DecisionTreeRegressor
----> 2 from sklearn.tree import export_text
      3 
      4 # Train the model
      5 model = DecisionTreeRegressor().fit(X_train, y_train)

ImportError: cannot import name 'export_text'

After updating scikit-learn to the current version (scikit-learn 0.24.1) it worked!

pip install -U scikit-learn

Verify version:
python -m pip show scikit-learn

Don't forget to restart the Kernel afterwards.