Skip to content

Instantly share code, notes, and snippets.

@Keycatowo
Created March 3, 2022 09:49
Show Gist options
  • Save Keycatowo/eb042f1fdd5dd323e0a81a0670249bfb to your computer and use it in GitHub Desktop.
Save Keycatowo/eb042f1fdd5dd323e0a81a0670249bfb to your computer and use it in GitHub Desktop.
查看特徵的重要程度(以RF為例)
from sklearn.ensemble import RandomForestClassifier
import matplotlib.pyplot as plt
feature_labels = list(train_X.columns) # 欄位名稱存下來等下顯示用
forest = RandomForestClassifier().fit(train_X, train_Y) # 送進模型fit
# 用`.feature_importances_`取得重要性
importances = forest.feature_importances_
# 取得對應的index,等下顯示用
indices = np.argsort(importances)[::-1]
# 印出所有重要性特徵
for f in range(train_X.shape[1]):
print("%2d) %-*s %f" % (f + 1, 30,
feature_labels[indices[f]],
importances[indices[f]]))
# 圖形顯示
plt.title('Feature Importance')
plt.bar(range(train_X.shape[1]),
importances[indices],
align='center')
plt.xticks(range(train_X.shape[1]),
[feature_labels[x] for x in indices], rotation=45)
plt.xlim([-1, train_X.shape[1]])
plt.tight_layout()
plt.show()
@Keycatowo
Copy link
Author

欄位名稱:
image

重要程度:
image

print出來顯示
image

圖形顯示
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment