始める無料で始める

発生率をプロットする

predictor insight graph(予測因子インサイトグラフ)で最も重要な要素は、発生率(incidence)です。ある変数に対して母集団をグループ分けすると、各グループにおけるターゲットの割合を発生率が示します。 この演習では、predictor insight graph テーブルが与えられたときに、指定した変数の発生率をプロットする 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()
コードを編集して実行