我正在研究决策树分类。 我试图发挥重要作用,但是我的代码似乎有问题。
正如stackoverflow之一所示,我加载了库。
# Load libraries
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt
然后
# Sort feature importances in descending order
indices = np.argsort(importances)[::-1]
# Rearrange feature names so they match the sorted feature importances
names = [clf.feature_importances_[i] for i in indices]
# Create plot
plt.figure()
# Create plot title
plt.title("Feature Importance")
# Add bars
plt.bar(range(X.shape[1]), importances[indices])
# Add feature names as x-axis labels
plt.xticks(range(X.shape[1]), names, rotation=90)
# Show plot
plt.show()
显示
没有错误,但是x轴缺少标签。
似乎plt.xticks(range(X.shape[1]), names, rotation=90)
的这一部分必须更改,但我不知道在哪里。
感谢您的阅读!