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

การประเมินโมเดลบน test set และ train set

ฟังก์ชัน auc_train_test ใช้คำนวณค่า AUC ของโมเดลที่สร้างจาก train set และประเมินผลบน test set ดังนี้

auc_train, auc_test = auc_train_test(variables, target, train, test)

โดยที่ variables คือลิสต์ของชื่อตัวแปรที่ใช้ในโมเดล

ในแบบฝึกหัดนี้ จะได้ลองใช้ฟังก์ชันนี้ และตรวจสอบว่าค่า AUC ของ train set และ test set ใกล้เคียงกันหรือไม่

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การวิเคราะห์เชิงพยากรณ์เบื้องต้นด้วย Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • โหลด basetable ไว้แล้ว ให้แบ่ง basetable โดยให้ train set มีข้อมูล 70% ของทั้งหมด และตรวจสอบให้แน่ใจว่า train set และ test set มี target incidence ที่เท่ากัน
  • คำนวณค่า AUC ของ train set และ test set โดยใช้ "age" และ "gender_F" เป็น predictor ผ่านฟังก์ชัน auc_train_test

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Load the partitioning module
from sklearn.model_selection import train_test_split

# Create DataFrames with variables and target
X = basetable.drop('target', 1)
y = basetable["target"]

# Carry out 70-30 partititioning with stratification
X_train, X_test, y_train, y_test = ____(X, y, test_size = ____, stratify = ____)

# Create the final train and test basetables
train = pd.concat([X_train, y_train], axis=1)
test = pd.concat([X_test, y_test], axis=1)

 # Apply the auc_train_test function
auc_train, auc_test = ____([____, ____], ["target"], ____, ____)
print(round(auc_train,2))
print(round(auc_test,2))
แก้ไขและรันโค้ด