การแบ่งพาร์ติชันข้อมูล
การประเมินโมเดลอย่างถูกต้องสามารถทำได้โดยแบ่งข้อมูลออกเป็นชุด train และชุดทดสอบ ชุด train ใช้สำหรับสร้างโมเดล ส่วนชุดทดสอบใช้สำหรับประเมินผล การแบ่งนี้ทำแบบสุ่ม แต่เมื่อ target incidence มีค่าต่ำ อาจจำเป็นต้องใช้การ stratify เพื่อให้แน่ใจว่าทั้งสองชุดมีสัดส่วนของ target เท่ากัน
ในแบบฝึกหัดนี้ คุณจะแบ่งข้อมูลโดยใช้การ stratify และตรวจสอบว่าชุด train และชุดทดสอบมี target incidence เท่ากัน โดย method train_test_split ถูก import มาแล้ว และ DataFrame X กับ y พร้อมใช้งานใน workspace ของคุณ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์เชิงพยากรณ์เบื้องต้นด้วย Python
คำแนะนำการฝึกหัด
- แบ่ง DataFrame เหล่านี้โดยใช้ method
train_test_splitพร้อมการ stratify ให้ชุด train และชุดทดสอบมีขนาดเท่ากันและมี target incidence เท่ากัน - คำนวณ target incidence ของชุด train โดยหารจำนวน target ในชุด train ด้วยจำนวน observation ทั้งหมดในชุด train
- คำนวณ target incidence ของชุดทดสอบ
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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 50-50 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)
# Check whether train and test have same percentage targets
print(round(sum(train[____])/len(____), 2))
print(round(sum(test[____])/len(____), 2))