Ensemble Learning

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

Výhody CART

  • Snadno pochopitelné.

  • Snadno interpretovatelné.

  • Jednoduché na použití.

  • Flexibilita: schopnost popsat nelineární závislosti.

  • Předzpracování: není třeba standardizovat ani normalizovat příznaky, ...

Machine Learning with Tree-Based Models in Python

Omezení CART

  • Klasifikace: produkuje pouze ortogonální rozhodovací hranice.

  • Citlivost na malé změny v trénovací sadě.

  • Vysoký rozptyl: neomezené CART mohou přetrénovat trénovací sadu.

  • Řešení: ensemble learning.

Machine Learning with Tree-Based Models in Python

Ensemble Learning

  • Trénujte různé modely na stejném datasetu.

  • Každý model vytvoří své předpovědi.

  • Meta-model: agreguje předpovědi jednotlivých modelů.

  • Výsledná předpověď: robustnější a méně náchylná k chybám.

  • Nejlepší výsledky: modely se liší ve svých silných stránkách.

Machine Learning with Tree-Based Models in Python

Ensemble Learning: vizuální vysvětlení

vizualizace ensemble

Machine Learning with Tree-Based Models in Python

Ensemble Learning v praxi: Voting Classifier

  • Binární klasifikační úloha.

  • $N$ klasifikátorů vytvoří předpovědi: $P_1$, $P_2$, ..., $P_N$, kde $P_i$ = 0 nebo 1.

  • Předpověď meta-modelu: hard voting.

Machine Learning with Tree-Based Models in Python

Hard Voting

hard-voting

Machine Learning with Tree-Based Models in Python

VotingClassifier v sklearn (dataset 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

VotingClassifier v sklearn (dataset 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

VotingClassifier v sklearn (dataset 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

Lass uns üben!

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...