0

I'm trying to use the function export_text from sklearn.tree (version 1.0.2), but when I'm using the parameter feature_names I get an error, this is what I'm trying

# decision_tree is the fitted algorithm
text_representation = export_text(decision_tree=decision_tree, feature_names=decision_tree.feature_names_in_)

enter image description here

If I call the function without the feature_names parameter it works, I already checked and the decision_tree.feature_names_in_ returns the right names, the same I used as input for the algorithm

If I use this other similar function with the same arguments, it works fine

plot_tree(decision_tree=decision_tree, feature_names=decision_tree.feature_names_in_)

any idea of what could it be wrong?

Rodrigo A
  • 657
  • 7
  • 23

1 Answers1

2

It looks like a small bug in the export_text code. Try forcing features_names_in_ to be a python list.

# decision_tree is the fitted algorithm
text_representation = export_text(decision_tree=decision_tree, feature_names=decision_tree.feature_names_in_.tolist())

I assume you used a pandas.DataFrame when generating the DecisionTree so that you get the feature_names_in_?

If that's the case then feature_names_in_ will be a numpy.ndarray.

The export_text code does a simple: if feature_names: test to check for features, but numpy will raise the error you see.

This looks like it's specific to the export_text, so you don't see it for plot_tree

pcoates
  • 2,102
  • 1
  • 9
  • 20
  • Hi, thanks for your answer! You're right, as features_names_in was a NumPy array it was falling, forcing it into a list worked, thanks again – Rodrigo A May 10 '22 at 15:29