4
$\begingroup$

I've this code in order to visualize the most important feature of each model:

dtc = DecisionTreeClassifier(min_samples_split=7, random_state=111)
rfc = RandomForestClassifier(n_estimators=31, random_state=111)
trained_model = dtc.fit(features_train, labels_train)
trained_model.fit(features_train, labels_train)
predictions = trained_model.predict(features_test)
importances = trained_model.feature_importances_
    std = np.std([trained_model.feature_importances_ for trained_model in 
 trained_model.estimators_], axis=0)
 indices = np.argsort(importances)[::-1]
    for f in range(features_train.shape[1]):
        print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
        plt.figure()
        plt.title("Feature importances")
        plt.bar(range(features_train.shape[1]), importances[indices], color="r", yerr=std[indices], align="center")
        plt.xticks(range(features_train.shape[1]), indices)
        plt.xlim([-1, features_train.shape[1]])
        plt.show()

Using RandomForestClassifier this code runs good but when I try it using Decison Trees classifier I get the following error:

std = np.std([trained_model.feature_importances_ for trained_model in trained_model.estimators_], axis=0)

builtins.AttributeError: 'DecisionTreeClassifier' object has no attribute 'estimators_'

Which attribute should I use see the most important feature of each model?

$\endgroup$

1 Answer 1

4
$\begingroup$

Do Checkout this Link

To Visualise The Tree Itself

from sklearn.tree import export_graphviz
import graphviz

export_graphviz(tree, out_file="mytree.dot")
with open("mytree.dot") as f:
    dot_graph = f.read()
graphviz.Source(dot_graph)

OR

from sklearn.tree import convert_to_graphviz
convert_to_graphviz(tree)

OR

from sklearn.tree import convert_to_graphviz
import graphviz

graphviz.Source(export_graphviz(tree))

The Visualisation You Can Get Will be Whole Tree Itself.. RF

To Display Feature Importances

from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier()
classifier.fit(features, labels)
for name, importance in zip(features.columns, classifier.feature_importances_):
    print(name, importance)
    ## Now You Can Do Whatever You Want(plot them using a Barplot etc)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.