開始使用免費開始

使用決策樹計算特徵重要度

你已使用 heart disease 資料集建立一個決策樹分類器,用來識別有心臟疾病風險的病患。現在你需要透過分析特徵重要度來解釋模型,找出用於預測心臟疾病的關鍵因素,從而設計更精準的醫療介入。

matplotlib.pyplot 已以 plt 匯入。X_trainy_train 已為你預先載入。

本練習屬於課程

Python 的 Explainable AI

檢視課程

練習說明

  • model 擷取特徵重要度。
  • 針對給定的 feature_names 繪製 feature_importances

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

model = DecisionTreeClassifier(random_state=42)
model.fit(X_train, y_train)

# Derive feature importances
feature_importances = ____
feature_names = X_train.columns

# Plot the feature importances
____
plt.show()
編輯並執行程式碼