การค้นหาลำดับของตัวแปร
กระบวนการคัดเลือกตัวแปรแบบ forward stepwise เริ่มต้นด้วยชุดตัวแปรที่ว่างเปล่า แล้วทยอยเพิ่มตัวทำนายทีละตัว ในแต่ละขั้นตอน ตัวทำนายที่ให้ค่า AUC สูงสุดเมื่อรวมกับตัวแปรที่มีอยู่จะถูกเลือก
ในแบบฝึกหัดนี้ คุณจะได้เรียนรู้วิธีสร้างกระบวนการคัดเลือกตัวแปรแบบ forward stepwise โดยใช้ฟังก์ชัน 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)