สร้างโมเดลจำแนกโรคเบาหวาน
ในแบบฝึกหัดนี้จะใช้ชุดข้อมูล Pima Indians diabetes เพื่อทำนายว่าบุคคลนั้นเป็นโรคเบาหวานหรือไม่ โดยใช้ logistic regression ชุดข้อมูลนี้มี 8 features และ 1 target โดยข้อมูลถูกแบ่งเป็นชุดฝึกและชุดทดสอบ และโหลดไว้ให้แล้วในชื่อ X_train, y_train, X_test และ X_test
ได้กำหนด StandardScaler() ไว้ล่วงหน้าในชื่อ scaler และ LogisticRegression() ในชื่อ lr
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การลดมิติข้อมูลใน Python
คำแนะนำการฝึกหัด
- Fit scaler บน features ชุดฝึก และ transform ข้อมูลในขั้นตอนเดียว
- Fit โมเดล logistic regression บนข้อมูลชุดฝึกที่ปรับสเกลแล้ว
- ปรับสเกล features ของชุดทดสอบ
- ทำนายการมีโรคเบาหวานบนชุดทดสอบที่ปรับสเกลแล้ว
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Fit the scaler on the training features and transform these in one go
X_train_std = scaler.____(____)
# Fit the logistic regression model on the scaled training data
lr.____(____, ____)
# Scale the test features
X_test_std = scaler.____(____)
# Predict diabetes presence on the scaled test set
y_pred = lr.____(____)
# Prints accuracy metrics and feature coefficients
print(f"{accuracy_score(y_test, y_pred):.1%} accuracy on test set.")
print(dict(zip(X.columns, abs(lr.coef_[0]).round(2))))