การประเมินและการแสดงผลโมเดล

Machine Learning แบบ End-to-End

Joshua Stapleton

Machine Learning Engineer

ความแม่นยำ

  • เมตริกความแม่นยำที่เหมาะสมมีความสำคัญต่อการประเมินโมเดลที่แข็งแกร่ง
  • ตีความหรืออ่านผลลัพธ์ผิดได้ง่าย

ความแม่นยำแบบมาตรฐาน:

  • ความแม่นยำแบบมาตรฐาน = จำนวนคำตอบที่ถูกต้อง / จำนวนคำตอบทั้งหมด
  • ความแม่นยำแบบมาตรฐานอาจไม่มีประโยชน์

ตัวอย่าง:

# achieves ~99% accuracy for imbalanced dataset of 99 positive and 1 negative
for patient_datapoint in heart_disease_dataset:
    model.prediction(patient_datapoint) = 'positive'
Machine Learning แบบ End-to-End

Confusion matrix

True positives (TP)

  • ผลการทำนายของโมเดล = การจัดประเภทจริง = บวก
  • โมเดลทำนายว่ามีโรคหัวใจ และผู้ป่วยมีโรคหัวใจจริง

False positives (FP)

  • ผลการทำนายของโมเดล = บวก, การจัดประเภทจริง = ลบ
  • โมเดลทำนายว่ามีโรคหัวใจ แต่ผู้ป่วยไม่มีโรคหัวใจ

False negatives (FN)

  • ผลการทำนายของโมเดล = ลบ, การจัดประเภทจริง = บวก
  • โมเดลทำนายว่าไม่มีโรคหัวใจ แต่ผู้ป่วยมีโรคหัวใจจริง

True negatives (TN)

  • ผลการทำนายของโมเดล = การจัดประเภทจริง = ลบ
  • โมเดลทำนายว่าไม่มีโรคหัวใจ และผู้ป่วยไม่มีโรคหัวใจจริง
Machine Learning แบบ End-to-End

Balanced accuracy

  • เมตริกที่ดีกว่าความแม่นยำแบบธรรมดาสำหรับโมเดล binary classification ส่วนใหญ่
  • ให้ค่าเฉลี่ยถ่วงน้ำหนักระหว่างทั้งสองคลาส
  • Balanced accuracy = (TP + TN) / 2
from sklearn.metrics import balanced_accuracy_score

# Assume y_test is the true labels and y_pred are the predicted labels
y_pred = model.predict(X_test)
bal_accuracy = balanced_accuracy_score(y_test, y_pred)
print(f"Balanced Accuracy: {bal_accuracy:.2f}")
Balanced Accuracy: 0.85
Machine Learning แบบ End-to-End

การใช้งาน Confusion matrix

Confusion Matrix

Machine Learning แบบ End-to-End

Cross validation

Cross-validation

  • กระบวนการ resampling
  • ช่วยให้ผลลัพธ์มีความแข็งแกร่ง

 

k-fold cross-validation

  • พารามิเตอร์ 'k' = จำนวนการแบ่งชุดข้อมูล
  • สุ่ม train/test split ใหม่ในแต่ละรอบการสร้างโมเดล

ไดอะแกรมแสดง cross validation

Machine Learning แบบ End-to-End

การใช้งาน Cross validation

  • การใช้งาน k-fold cross validation ด้วย sklearn ที่ตรงไปตรงมา
  • การให้คะแนนที่ไม่ขึ้นกับโมเดล

การใช้งาน:

from sklearn.model_selection import cross_val_score, KFold

# split the data into 10 equal parts
kfold = KFold(n_splits=5, shuffle=True, random_state=42)

# get the cross validation accuracy for a given model cv_results = cross_val_score(model, heart_disease_X, heart_disease_y, cv=kfold, scoring='balanced_accuracy')
Machine Learning แบบ End-to-End

Hyperparameter tuning

Hyperparameter:

  • พารามิเตอร์ระดับ global ของโมเดล (ไม่เปลี่ยนแปลงระหว่างการเทรน)
  • ปรับเพื่อเพิ่มประสิทธิภาพของโมเดล
# Hyperparameters to test
C_values = [0.001, 0.01, 0.1, 1, 10, 100, 1000]

# Manually iterate over the hyperparameters
for C in C_values:
    model = LogisticRegression(max_iter=200, C=C)
    model.fit(X_train, y_train)
    accuracy = cross_val_score(model, X, y, cv=kfold, scoring='balanced_accuracy')
    print(f"C = {C}: Bal Acc: {accuracy.mean():.4f} (+/- {accuracy.std():.4f})")
Machine Learning แบบ End-to-End

ตัวอย่าง Hyperparameter tuning

ตัวอย่างผลลัพธ์ของ hyperparameter tuning:

 

C = 0.001: Bal Acc: 0.6200 (+/- 0.0215)
C = 0.01: Bal Acc: 0.7325 (+/- 0.0234)
C = 0.1: Bal Acc: 0.7923 (+/- 0.0202)
C = 1: Bal Acc: 0.8050 (+/- 0.0191)
C = 10: Bal Acc: 0.8034 (+/- 0.0185)
C = 100: Bal Acc: 0.8021 (+/- 0.0187)
C = 1000: Bal Acc: 0.8017 (+/- 0.0188)
Machine Learning แบบ End-to-End

มาฝึกกันเถอะ!

Machine Learning แบบ End-to-End

Preparing Video For Download...