'DecisionTreeClassifier' object has no attribute 'importances_'
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?
Topic estimators decision-trees feature-selection python predictive-modeling
Category Data Science