Ensemble Learning

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

ข้อดีของ CARTs

  • เข้าใจง่าย

  • ตีความได้ง่าย

  • ใช้งานสะดวก

  • ความยืดหยุ่น: รองรับความสัมพันธ์แบบไม่เชิงเส้น

  • การเตรียมข้อมูล: ไม่จำเป็นต้องทำ standardize หรือ normalize features, ...

Machine Learning with Tree-Based Models in Python

ข้อจำกัดของ CARTs

  • การจำแนกประเภท: สร้างได้เฉพาะขอบเขตการตัดสินใจแบบตั้งฉาก

  • ไวต่อการเปลี่ยนแปลงเล็กน้อยในชุดข้อมูลฝึก

  • Variance สูง: CARTs ที่ไม่ถูกจำกัดอาจ overfit ชุดข้อมูลฝึก

  • แนวทางแก้ไข: Ensemble Learning

Machine Learning with Tree-Based Models in Python

Ensemble Learning

  • ฝึกโมเดลหลายตัวบนชุดข้อมูลเดียวกัน

  • ให้แต่ละโมเดลทำนายผลของตัวเอง

  • Meta-model: รวบรวมผลทำนายจากโมเดลแต่ละตัว

  • ผลทำนายสุดท้าย: แม่นยำและทนทานต่อข้อผิดพลาดมากขึ้น

  • ผลดีที่สุด: เมื่อแต่ละโมเดลมีจุดเด่นต่างกัน

Machine Learning with Tree-Based Models in Python

Ensemble Learning: ภาพอธิบาย

ภาพประกอบ Ensemble

Machine Learning with Tree-Based Models in Python

Ensemble Learning ในทางปฏิบัติ: Voting Classifier

  • งานจำแนกประเภทแบบ Binary

  • Classifier $N$ ตัวทำนายผล: $P_1$, $P_2$, ..., $P_N$ โดย $P_i$ = 0 หรือ 1

  • การทำนายของ Meta-model: ใช้ hard voting

Machine Learning with Tree-Based Models in Python

Hard Voting

hard voting

Machine Learning with Tree-Based Models in Python

Voting Classifier ใน sklearn (ชุดข้อมูล Breast-Cancer)

# Import functions to compute accuracy and split data
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

# Import models, including VotingClassifier meta-model
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.ensemble import VotingClassifier

# Set seed for reproducibility
SEED = 1
Machine Learning with Tree-Based Models in Python

Voting Classifier ใน sklearn (ชุดข้อมูล Breast-Cancer)

# Split data into 70% train and 30% test
X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                    test_size= 0.3,
                                                    random_state= SEED)
# Instantiate individual classifiers
lr = LogisticRegression(random_state=SEED)
knn = KNN()
dt = DecisionTreeClassifier(random_state=SEED)

# Define a list called classifier that contains the tuples (classifier_name, classifier) classifiers = [('Logistic Regression', lr), ('K Nearest Neighbours', knn), ('Classification Tree', dt)]
Machine Learning with Tree-Based Models in Python
# Iterate over the defined list of tuples containing the classifiers
for clf_name, clf in classifiers:
    #fit clf to the training set
    clf.fit(X_train, y_train)

    # Predict the labels of the test set
    y_pred = clf.predict(X_test)

    # Evaluate the accuracy of clf on the test set
    print('{:s} : {:.3f}'.format(clf_name, accuracy_score(y_test, y_pred)))
Logistic Regression: 0.947
K Nearest Neighbours: 0.930
Classification Tree: 0.930
Machine Learning with Tree-Based Models in Python

Voting Classifier ใน sklearn (ชุดข้อมูล Breast-Cancer)

# Instantiate a VotingClassifier 'vc'
vc = VotingClassifier(estimators=classifiers) 

# Fit 'vc' to the traing set and predict test set labels
vc.fit(X_train, y_train)   
y_pred = vc.predict(X_test)

# Evaluate the test-set accuracy of 'vc'
print('Voting Classifier: {.3f}'.format(accuracy_score(y_test, y_pred)))
Voting Classifier: 0.953
Machine Learning with Tree-Based Models in Python

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

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...