เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การสร้างกราฟ 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 โดย DataFrame train และ 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()
แก้ไขและรันโค้ด