การสร้างกราฟ AUC curves
กระบวนการคัดเลือกตัวแปรแบบ forward stepwise จะกำหนดลำดับการเพิ่มตัวแปรเข้าไปในชุดตัวทำนายอย่างเหมาะสมที่สุด เพื่อตัดสินใจว่าควรตัดตัวแปรที่จุดใด สามารถสร้างกราฟ AUC curves สำหรับข้อมูล train และ test ได้ กราฟเหล่านี้แสดงค่า AUC โดยใช้ตัวแปรตัวแรก สองตัวแรก สามตัวแรก … ตามลำดับในโมเดล
ในแบบฝึกหัดนี้ จะได้เรียนรู้วิธีพล็อตกราฟ AUC curves เหล่านี้ เมธอด auc_train_test สำหรับคำนวณค่า AUC ได้ถูกเตรียมไว้ให้แล้ว และใช้งานได้ดังนี้:
auc_train, auc_test = auc_train_test(variables, target, train, test)
โดย variables คือชุดตัวแปรที่ใช้ในโมเดล logistic regression, target คือลิสต์ที่มีชื่อ target และ train กับ test คือ basetable สำหรับข้อมูล train และ test ตามลำดับ
ตัวแปรที่เรียงลำดับตามกระบวนการ forward stepwise จะอยู่ในลิสต์ variables ซึ่งสามารถสำรวจได้ใน console นอกจากนี้ยังมีลิสต์ว่างสามรายการที่กำหนดไว้ให้แล้ว ได้แก่:
auc_values_trainสำหรับเก็บค่า AUC ของข้อมูล train ในแต่ละรอบการวนซ้ำauc_values_testสำหรับเก็บค่า AUC ของข้อมูล test ในแต่ละรอบการวนซ้ำvariables_evaluateสำหรับเก็บตัวแปรที่ประเมินในแต่ละรอบการวนซ้ำ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์เชิงพยากรณ์เบื้องต้นด้วย Python
คำแนะนำการฝึกหัด
- วนซ้ำผ่านตัวแปรต่าง ๆ
- ในแต่ละรอบ ให้เพิ่มตัวแปรถัดไปใน
variablesเข้าไปในvariables_evaluate - ในแต่ละรอบ ให้คำนวณค่า AUC ของข้อมูล train และ test โดยใช้เมธอด
auc_train_testโดย DataFrametrainและtestมีข้อมูล train และ test ตามลำดับ - ในแต่ละรอบ ให้เพิ่มค่าที่คำนวณได้เข้าไปใน
auc_values_trainและauc_values_test
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Keep track of train and test AUC values
auc_values_train = []
auc_values_test = []
variables_evaluate = []
# Iterate over the variables in variables
for v in ____:
# Add the variable
variables_evaluate.append(____)
# Calculate the train and test AUC of this set of variables
auc_train, auc_test = ____(____, ["target"], ____, ____)
# Append the values to the lists
auc_values_train.append(____)
auc_values_test.append(____)
# Make plot of the AUC values
import matplotlib.pyplot as plt
import numpy as np
x = np.array(range(0,len(auc_values_train)))
y_train = np.array(auc_values_train)
y_test = np.array(auc_values_test)
plt.xticks(x, variables, rotation = 90)
plt.plot(x,y_train)
plt.plot(x,y_test)
plt.ylim((0.6, 0.8))
plt.show()