開始使用免費開始

繪製群組大小

預測因子洞察圖會提供關於預測變數的資訊。每個變數會將整體樣本分成數個群組。預測因子洞察圖包含一條線,顯示各群組的平均目標發生率,以及一組長條,顯示各群組的大小。 在這個練習中,你會學到如何撰寫並套用一個函式,根據預測因子洞察圖的資料表,繪製對應的洞察圖。

本練習屬於課程

Python 預測分析入門

檢視課程

動手互動練習

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

# The function to plot a predictor insight graph
def plot_pig(pig_table, variable):
    
    # Plot formatting
    plt.ylabel("Size", rotation=0, rotation_mode="anchor", ha="right")
    
    # Plot the bars with sizes 
    pig_table["____"].plot(kind="bar", width=0.5, color="lightgray", edgecolor="none") 
    
    # Plot the incidence line on secondary axis
    pig_table["____"].plot(secondary_y=True)
    
    # Plot formatting
    plt.xticks(np.arange(len(pig_table)), pig_table[variable])
    plt.xlim([-0.5, len(pig_table) - 0.5])
    plt.ylabel("Incidence", rotation=0, rotation_mode="anchor", ha="left")
    
    # Show the graph
    plt.show()
編輯並執行程式碼