始める無料で始める

信頼区間のブートストラップ

データのばらつきを評価するうえで便利な手法がブートストラップです。この演習では、ブートストラップした信頼区間を返す独自の関数を作成します。

この関数は3つの引数を取ります。数値の2次元配列(data)、計算するパーセンタイルのリスト(percentiles)、使用するブートストラップ反復回数(n_boots)です。resample 関数を使ってブートストラップサンプルを生成し、これを複数回繰り返して信頼区間を計算します。

この演習はコースの一部です

Pythonで学ぶMachine Learningによる時系列データ解析

コースを見る

演習の手順

  • 関数は、ブートストラップの回数(パラメータ n_boots)だけループし、次を実行します。
    • データから置換ありでランダムサンプルを取り、そのサンプルの平均を計算する
    • bootstrap_means のパーセンタイルを計算し、それを返す

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

from sklearn.utils import ____

def bootstrap_interval(data, percentiles=(2.5, 97.5), n_boots=100):
    """Bootstrap a confidence interval for the mean of columns of a 2-D dataset."""
    # Create our empty array to fill the results
    bootstrap_means = np.zeros([n_boots, data.shape[-1]])
    for ii in range(____):
        # Generate random indices for our data *with* replacement, then take the sample mean
        random_sample = ____
        bootstrap_means[ii] = random_sample.mean(axis=0)
        
    # Compute the percentiles of choice for the bootstrapped means
    percentiles = ____(bootstrap_means, percentiles, axis=0)
    return percentiles
コードを編集して実行