รวมทุกอย่างเข้าด้วยกัน
นอกจาก pipeline การตรวจจับความผิดปกติแบบ distance-based learning ที่สร้างไว้ในแบบฝึกหัดที่แล้ว คุณต้องการเพิ่มการรองรับแบบ feature-based learning ด้วย one-class SVM โดยจะดึงฟีเจอร์ 2 ตัว ได้แก่ ความยาวของ string และการ encode ตัวอักษรแรกของ string เป็นตัวเลข ซึ่งใช้ฟังก์ชัน LabelEncoder() ตามที่อธิบายไว้ในบทที่ 1 เพื่อให้การเปรียบเทียบมีความเป็นธรรม จึงนำคะแนน outlier ไปคำนวณ AUC มีการ import สิ่งต่อไปนี้แล้ว: LabelEncoder(), roc_auc_score() ในชื่อ auc() และ OneClassSVM ข้อมูลอยู่ในรูปแบบ pandas data frame ชื่อ proteins ซึ่งมี 2 คอลัมน์คือ label และ seq และ 2 คลาสคือ IMMUNE SYSTEM และ VIRUS ตัวตรวจจับ LoF ที่ fit แล้วมีชื่อว่า lof_detector
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การออกแบบ Machine Learning Workflows ด้วย Python
คำแนะนำการฝึกหัด
- สำหรับ string
sนั้นlen(s)จะคืนค่าความยาวของ string นั้น ให้ apply ฟังก์ชันนี้กับคอลัมน์seqเพื่อสร้างคอลัมน์ใหม่ชื่อlen - สำหรับ string
sนั้นlist(s)จะคืนค่าเป็น list ของอักขระแต่ละตัว ให้ใช้วิธีนี้ดึงตัวอักษรแรกของแต่ละ sequence แล้ว encode โดยใช้LabelEncoder() - คะแนน LoF อยู่ใน attribute
negative_outlier_factor_ให้คำนวณค่า AUC ของคะแนนเหล่านี้ - Fit 1-class SVM กับ data frame ที่มีเฉพาะคอลัมน์
lenและfirstจากนั้นดึงคะแนนออกมา และประเมินทั้งคะแนน LoF และคะแนน SVM โดยใช้ AUC
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create a feature that contains the length of the string
proteins['len'] = proteins['seq'].apply(____)
# Create a feature encoding the first letter of the string
proteins['first'] = ____.____(
proteins['seq'].apply(____))
# Extract scores from the fitted LoF object, compute its AUC
scores_lof = lof_detector.____
print(____(proteins['label']==____, scores_lof))
# Fit a 1-class SVM, extract its scores, and compute its AUC
svm = ____.____(proteins[['len', 'first']])
scores_svm = svm.____(proteins[['len', 'first']])
print(____(proteins['label']==____, scores_svm))