可视化随机森林的特征重要性
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
iris = datasets.load_iris()
features = iris.data
target = iris.target
randomforest = RandomForestClassifier(random_state=0)
model = randomforest.fit(features, target)
importances = model.feature_importances_
print("model.feature_importances_: {}".format(importances))
indices = np.argsort(importances)[::-1]
print(indices)
names = [iris.feature_names[i] for i in indices]
plt.figure()
plt.title("feature importance")
plt.bar(range(features.shape[1]), importances[indices])
plt.xticks(range(features.shape[1]), names, rotation=90)
plt.show()
model.feature_importances_: [0.09090795 0.02453104 0.46044474 0.42411627]
[2 3 0 1]
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/36b176624f49499778016067f7d455f9.png)