การประเมินหลายโมเดล

Supervised Learning ด้วย scikit-learn

George Boorman

Core Curriculum Manager, DataCamp

โมเดลต่างกันสำหรับปัญหาต่างกัน

หลักการเบื้องต้น

  • ขนาดของชุดข้อมูล
    • ฟีเจอร์น้อย = โมเดลเรียบง่าย ฝึกได้เร็วขึ้น
    • บางโมเดลต้องการข้อมูลจำนวนมากเพื่อให้ทำงานได้ดี
  • ความสามารถในการอธิบาย
    • บางโมเดลอธิบายได้ง่ายกว่า ซึ่งสำคัญสำหรับผู้มีส่วนได้ส่วนเสีย
    • Linear regression มีความสามารถในการอธิบายสูง เพราะเข้าใจค่าสัมประสิทธิ์ได้
  • ความยืดหยุ่น
    • ช่วยเพิ่มความแม่นยำโดยตั้งสมมติฐานเกี่ยวกับข้อมูลน้อยลง
    • KNN เป็นโมเดลที่ยืดหยุ่นกว่า ไม่สมมติว่าข้อมูลมีความสัมพันธ์เชิงเส้น
Supervised Learning ด้วย scikit-learn

ทุกอย่างอยู่ที่เมตริก

  • ประสิทธิภาพโมเดล Regression:

    • RMSE
    • R-squared
  • ประสิทธิภาพโมเดล Classification:

    • Accuracy
    • Confusion matrix
    • Precision, recall, F1-score
    • ROC AUC
  • ฝึกหลายโมเดลและประเมินประสิทธิภาพเบื้องต้น

Supervised Learning ด้วย scikit-learn

หมายเหตุเรื่องการ scaling

  • โมเดลที่ได้รับผลจากการ scaling:
    • KNN
    • Linear Regression (รวมถึง Ridge, Lasso)
    • Logistic Regression
    • Artificial Neural Network

 

  • ควร scale ข้อมูลก่อนประเมินโมเดล
Supervised Learning ด้วย scikit-learn

การประเมินโมเดล Classification

import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score, KFold, train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier

X = music.drop("genre", axis=1).values y = music["genre"].values X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
Supervised Learning ด้วย scikit-learn

การประเมินโมเดล Classification

models = {"Logistic Regression": LogisticRegression(), "KNN": KNeighborsClassifier(), 
         "Decision Tree": DecisionTreeClassifier()}
results = []

for model in models.values():
kf = KFold(n_splits=6, random_state=42, shuffle=True)
cv_results = cross_val_score(model, X_train_scaled, y_train, cv=kf)
results.append(cv_results)
plt.boxplot(results, labels=models.keys()) plt.show()
Supervised Learning ด้วย scikit-learn

การแสดงผลลัพธ์

Box plot แสดง accuracy ของแต่ละโมเดล: Logistic Regression, KNN และ Decision Tree

Supervised Learning ด้วย scikit-learn

ประสิทธิภาพบน Test Set

for name, model in models.items():

model.fit(X_train_scaled, y_train)
test_score = model.score(X_test_scaled, y_test)
print("{} Test Set Accuracy: {}".format(name, test_score))
Logistic Regression Test Set Accuracy: 0.844
KNN Test Set Accuracy: 0.82
Decision Tree Test Set Accuracy: 0.832
Supervised Learning ด้วย scikit-learn

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

Supervised Learning ด้วย scikit-learn

Preparing Video For Download...