改善圖表
為了讓圖表更易讀,我們需要達成兩個目標:
- 依遞增順序重新排列長條。
- 在圖上加上對應特徵名稱的標籤。
為此,我們會運用 NumPy 的索引。.argsort() 方法會對陣列排序並回傳索引。我們將利用這些索引來同時達成兩個目標。
本練習屬於課程
行銷分析:用 Python 預測客戶流失
練習說明
- 使用
np.argsort()作用在importances上,計算importances的排序索引。 - 取用
X的欄位,並用sorted_index進行索引,建立排序後的標籤。 - 透過以
sorted_index索引importances來建立圖表,並指定參數tick_label=labels。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Sort importances
sorted_index = ____(____)
# Create labels
labels = X.columns[____]
# Clear current plot
plt.clf()
# Create plot
plt.barh(range(X.shape[1]), importances[____], tick_label=____)
plt.show()