Search code examples
pythontreetagsextractrules

How to get the features names for the data?


I used the following instructions with iris dataset that included with python environment

iris_data=load_iris()
feature_names = iris_data.feature_names 
k= tree.export_text(model.estimators_[i],feature_names)

I get the rules by this shape

"""\
|--- petal length (cm) <= 2.35
|   |--- class: 0.0
|--- petal length (cm) >  2.35
|   |--- petal width (cm) <= 1.65
|   |   |--- class: 1.0
|   |--- petal width (cm) >  1.65
|   |   |--- petal width (cm) <= 1.75
|   |   |   |--- sepal length (cm) <= 5.80
|   |   |   |   |--- class: 2.0"

but when I tried to use the same instructions with outer dataset I get this error AttributeError: 'DataFrame' object has no attribute 'feature_names'
also when I tried to get the features names by this instruction

fnm = list(dataset.columns.values.tolist())
k= tree.export_text(model.estimators_[i],fnm)

and use the same line to expert_text i get the following errorFile "C:\Anaconda3\lib\site-packages\sklearn\tree_export.py", line 886, in export_textlen(feature_names))) ValueError: feature_names must contain 3 elements, got 53


Solution

  • The method sklearn.datasets.load_iris returns a sklearn.utils.Bunch object which has a feature_names attribute.

    Your new dataset is a pandas.DataFrame object which has a columns attribute.