開始使用免費開始

繪製發生率圖

預測因子洞察圖中最重要的元素是發生率(incidence)值。針對某個變數,族群中每個分組的發生率值代表該分組中目標(target)所佔的百分比。 在這個練習中,你將撰寫一個 Python 函式,根據預測因子洞察圖的資料表,繪出某個變數的發生率值。

本練習屬於課程

Python 預測分析入門

檢視課程

動手互動練習

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

import matplotlib.pyplot as plt
import numpy as np

# The function to plot a predictor insight graph.
def plot_incidence(pig_table,variable):
    
    # Plot the incidence line
    ____["____"].plot()
    
    # Formatting the predictor insight graph
    plt.xticks(np.arange(len(pig_table)), pig_table[variable])
    plt.xlim([-0.5, len(pig_table) - 0.5])
    plt.ylim([0, max(pig_table["Incidence"] * 2)])
    plt.ylabel("Incidence", rotation=0, rotation_mode="anchor", ha="right")
    plt.xlabel(variable)
    
    # Show the graph
    plt.show()
編輯並執行程式碼