找出變數加入的順序
前向逐步變數選擇會從空的變數集合開始,並一次加入一個預測變數。每一步都會選出與目前變數組合起來、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)