我有这段代码是为了可视化每个模型的最重要特征:
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()
使用 RandomForestClassifier 这段代码运行良好,但是当我使用 Decison Trees 分类器尝试它时,我收到以下错误:
std = np.std([trained_model.feature_importances_ for trained_model in trained_model.estimators_], axis=0)
builtins.AttributeError: 'DecisionTreeClassifier' object has no attribute 'estimators_'
我应该使用哪个属性来查看每个模型的最重要特征?