Des workflows aux pipelines

Concevoir des workflows de Machine Learning en Python

Dr. Chris Anagnostopoulos

Honorary Associate Professor

Revoir notre workflow

from sklearn.ensemble import RandomForestClassifier as rf
X_train, X_test, y_train, y_test = train_test_split(X, y)
grid_search = GridSearchCV(rf(), param_grid={'max_depth': [2, 5, 10]})
grid_search.fit(X_train, y_train)
depth = grid_search.best_params_['max_depth']
vt = SelectKBest(f_classif, k=3).fit(X_train, y_train)
clf = rf(max_depth=best_value).fit(vt.transform(X_train), y_train)
accuracy_score(clf.predict(vt.transform(X_test), y_test))
Concevoir des workflows de Machine Learning en Python

La puissance de la recherche par grille

Optimiser max_depth :

pg = {'max_depth': [2,5,10]}
gs = GridSearchCV(rf(),  
   param_grid=pg)
gs.fit(X_train, y_train)
depth = gs.best_params_['max_depth']

Un tableau de toutes les combinaisons possibles de profondeur et du nombre d'estimateurs, montrant que trois valeurs ont été testées et qu'une est la meilleure.

Concevoir des workflows de Machine Learning en Python

La puissance de la recherche par grille

Puis optimiser n_estimators :

pg = {'n_estimators': [10,20,30]}
gs = GridSearchCV(
   rf(max_depth=depth),  
   param_grid=pg)
gs.fit(X_train, y_train)
n_est = gs.best_params_[
    'n_estimators']

Un tableau de toutes les combinaisons possibles de profondeur et du nombre d'estimateurs, montrant que cinq valeurs ont été testées et qu'une autre est la meilleure.

Concevoir des workflows de Machine Learning en Python

La puissance de la recherche par grille

Optimiser conjointement max_depth et n_estimators :

pg = {
   'max_depth': [2,5,10],
   'n_estimators': [10,20,30]
}
gs = GridSearchCV(rf(),  
   param_grid=pg)
gs.fit(X_train, y_train)
print(gs.best_params_) 

{'max_depth': 10, 'n_estimators': 20}

Un tableau de toutes les combinaisons possibles de profondeur et du nombre d'estimateurs, montrant que toutes les valeurs ont été testées et que la même est la meilleure.

Concevoir des workflows de Machine Learning en Python

Pipelines

Dans ce schéma, la forêt aléatoire, avec deux hyperparamètres, est reliée au sélecteur de variables, qui a un hyperparamètre, par une flèche.

Concevoir des workflows de Machine Learning en Python

Pipelines

Les deux objets sont regroupés dans une seule boîte.

Concevoir des workflows de Machine Learning en Python

Pipelines

from sklearn.pipeline import Pipeline
pipe = Pipeline([
    ('feature_selection', SelectKBest(f_classif)), 
    ('classifier', RandomForestClassifier())
])

params = dict( feature_selection__k=[2, 3, 4], classifier__max_depth=[5, 10, 20] )
grid_search = GridSearchCV(pipe, param_grid=params) gs = grid_search.fit(X_train, y_train).best_params_
{'classifier__max_depth': 20, 'feature_selection__k': 4}
Concevoir des workflows de Machine Learning en Python

Personnaliser votre pipeline

from sklearn.metrics import roc_auc_score, make_scorer
auc_scorer = make_scorer(roc_auc_score)

grid_search = GridSearchCV(pipe, param_grid=params, scoring=auc_scorer)
Concevoir des workflows de Machine Learning en Python

N'en faites pas trop

params = dict(
    feature_selection__k=[2, 3, 4], 
    clf__max_depth=[5, 10, 20], 
    clf__n_estimators=[10, 20, 30] 
)
grid_search = GridSearchCV(pipe, params, cv=10)

3 × 3 × 3 × 10 = 270 ajustements de classifieur !

Concevoir des workflows de Machine Learning en Python

Workflows dopés

Concevoir des workflows de Machine Learning en Python

Preparing Video For Download...