การใช้ Pipeline
เมื่อกำหนด pipeline เรียบร้อยแล้ว ซึ่งก็คือการรวม logistic regression เข้ากับวิธี SMOTE ก็ถึงเวลารันกับข้อมูลจริง คุณสามารถใช้งาน pipeline ได้เหมือนกับโมเดล machine learning ตัวเดียว ข้อมูล X และ y ถูกกำหนดไว้แล้ว และ pipeline ก็ถูกสร้างไว้จากแบบฝึกหัดก่อนหน้า มาดูกันว่าผลลัพธ์ของโมเดลจะออกมาเป็นอย่างไร!
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การตรวจจับการฉ้อโกงด้วย Python
คำแนะนำการฝึกหัด
- แบ่งข้อมูล
Xและyออกเป็นชุดฝึกและชุดทดสอบ โดยสำรองข้อมูล 30% ไว้สำหรับชุดทดสอบ และกำหนดrandom_stateเป็นศูนย์ - ฝึก pipeline ด้วยข้อมูลชุดฝึก จากนั้นรับผลการพยากรณ์โดยเรียกใช้ฟังก์ชัน
pipeline.predict()กับชุดข้อมูลX_test
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Split your data X and y, into a training and a test set and fit the pipeline onto the training data
X_train, X_test, y_train, y_test = ____
# Fit your pipeline onto your training set and obtain predictions by fitting the model onto the test data
pipeline.fit(____, ____)
predicted = pipeline.____(____)
# Obtain the results from the classification report and confusion matrix
print('Classifcation report:\n', classification_report(y_test, predicted))
conf_mat = confusion_matrix(y_true=y_test, y_pred=predicted)
print('Confusion matrix:\n', conf_mat)