Modelcomplexiteit en overfitting

Machine Learning-workflows ontwerpen in Python

Dr. Chris Anagnostopoulos

Honorary Associate Professor

Wat is modelcomplexiteit?

RandomForestClassifier() accepteert extra argumenten, zoals max_depth:

help(RandomForestClassifier)
Help on class RandomForestClassifier in module sklearn.ensemble.forest:
...
 |  max_depth : integer or None, optional (default=None)
 |      The maximum depth of the tree. If None, then nodes are expanded until
 |      all leaves are pure or until all leaves contain less than
 |      min_samples_split samples.
Machine Learning-workflows ontwerpen in Python
m2 = RandomForestClassifier(
    max_depth=2)
m2.fit(X_train, y_train)

m2.estimators_[0]

Een beslisboom met diepte 2.

m4 = RandomForestClassifier(
    max_depth=4)
m4.fit(X_train, y_train)

m4.estimators_[0]

Een beslisboom met diepte 4.

Machine Learning-workflows ontwerpen in Python

Standaard splits je data in training, test (of development) en validatie (of hold-out).

Machine Learning-workflows ontwerpen in Python

Bij cross-validatie voer je de train-test-split meerdere keren uit. De dataset wordt in N delen gesplitst; elke keer gebruik je N-1 voor training en het resterende deel voor test.

Machine Learning-workflows ontwerpen in Python

Cross-validatie

Beoordeel nauwkeurigheid met cross_val_score():

from sklearn.model_selection import cross_val_score

cross_val_score(RandomForestClassifier(), X, y)
array([0.7218 , 0.7682, 0.7866])
numpy.mean(cross_val_score(RandomForestClassifier(), X, y))
0.7589
Machine Learning-workflows ontwerpen in Python

Modelcomplexiteit afstemmen

Stem de boomdiepte af met GridSearchCV():

from sklearn.model_selection import GridSearchCV
param_grid = {'max_depth':[5,10,20]}
grid = GridSearchCV(RandomForestClassifier(), param_grid)
grid.fit(X,y)
grid._best_params
{'max_depth': 10}
Machine Learning-workflows ontwerpen in Python

In-sample-nauwkeurigheid start op 0,7 bij een maximale diepte van 3 en stijgt bijna tot 1,0 als de diepte van 5 tot 30 gaat.

Machine Learning-workflows ontwerpen in Python

Out-of-sample-nauwkeurigheid start ook op 0,7, piekt op 0,75 bij diepte 10 en daalt terug naar 0,7 bij grotere dieptes.

Machine Learning-workflows ontwerpen in Python

Het bereik vanaf diepte 10 is rood, wat aangeeft dat er overfitting optreedt.

Machine Learning-workflows ontwerpen in Python

Complexer is niet altijd beter!

Machine Learning-workflows ontwerpen in Python

Preparing Video For Download...