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

การ fit และทดสอบโมเดล

ในแบบฝึกหัดก่อนหน้า คุณได้แบ่งชุดข้อมูลออกเป็น X_train, X_test, y_train และ y_test ซึ่งได้โหลดไว้ให้เรียบร้อยแล้ว คราวนี้จะสร้างโมเดล Support Vector Machine classifier (SVC()) และ fit โมเดลนั้นกับข้อมูล training จากนั้นคำนวณค่าความแม่นยำบนชุดข้อมูล test และ training เพื่อตรวจสอบว่าเกิด overfitting หรือไม่

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

การลดมิติข้อมูลใน Python

ดูคอร์ส

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

  • Import SVC จาก sklearn.svm และ accuracy_score จาก sklearn.metrics
  • สร้าง instance ของคลาส Support Vector Classification (SVC())
  • Fit โมเดลกับข้อมูล training
  • คำนวณค่าความแม่นยำบนชุดข้อมูล train และ test

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

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

# Import SVC from sklearn.svm and accuracy_score from sklearn.metrics
from ____ import ____
from ____ import ____

# Create an instance of the Support Vector Classification class
svc = ____

# Fit the model to the training data
svc.fit(____, ____)

# Calculate accuracy scores on both train and test data
accuracy_train = accuracy_score(____, svc.predict(____))
accuracy_test = accuracy_score(____, svc.predict(____))

print(f"{accuracy_test:.1%} accuracy on test set vs. {accuracy_train:.1%} on training set")
แก้ไขและรันโค้ด