始める無料で始める

変数を追加する順序を見つける

前進逐次(フォワードステップワイズ)変数選択は、最初は変数を空集合から始め、予測変数を1つずつ追加していく手法です。各ステップでは、現在の変数と組み合わせたときに最も高いAUCを示す予測変数が選ばれます。

この演習では、フォワードステップワイズ変数選択の実装方法を学びます。そのために、あらかじめ用意された next_best 関数を使えます。使い方は次のとおりです。

next_best(current_variables,candidate_variables,target,basetable)

ここで current_variables はすでにモデルに含まれている変数のリスト、candidate_variables は次に追加可能な変数のリストです。

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

Pythonで学ぶ予測分析入門

コースを見る

演習の手順

  • 関数 next_best を使って次に最適な変数を計算し、next_variable に代入します。
  • current_variables リストを更新します。
  • candidate_variables リストを更新します。

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

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

# Find the candidate variables
candidate_variables = list(basetable.columns.values)
candidate_variables.remove("target")

# Initialize the current variables
current_variables = []

# The forward stepwise variable selection procedure
number_iterations = 5
for i in range(0, number_iterations):
    next_variable = ____(____, ____, ["target"], basetable)
    current_variables = current_variables + [____]
    candidate_variables.remove(____)
    print("Variable added in step " + str(i+1)  + " is " + next_variable + ".")
print(current_variables)
コードを編集して実行