De workflows a pipelines

Projetando Workflows de Machine Learning em Python

Dr. Chris Anagnostopoulos

Honorary Associate Professor

Revisitando nosso 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))
Projetando Workflows de Machine Learning em Python

O poder do grid search

Otimizar 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']

Uma tabela com todas as combinações possíveis de profundidade e número de estimadores, mostrando que três valores foram testados e um foi o melhor.

Projetando Workflows de Machine Learning em Python

O poder do grid search

Depois, otimizar 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']

Uma tabela com todas as combinações possíveis de profundidade e número de estimadores, mostrando que cinco valores foram testados e outro foi o melhor.

Projetando Workflows de Machine Learning em Python

O poder do grid search

Em conjunto max_depth e 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}

Uma tabela com todas as combinações possíveis de profundidade e número de estimadores, mostrando que todos os valores foram testados e o mesmo foi o melhor.

Projetando Workflows de Machine Learning em Python

Pipelines

Neste diagrama, o random forest, com dois hiperparâmetros, é conectado ao seletor de atributos, que tem um hiperparâmetro, por uma seta.

Projetando Workflows de Machine Learning em Python

Pipelines

Ambos os objetos foram encapsulados por uma única caixa.

Projetando Workflows de Machine Learning em 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}
Projetando Workflows de Machine Learning em Python

Personalizando sua 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)
Projetando Workflows de Machine Learning em Python

Não exagere

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 x 3 x 3 x 10 = 270 ajustes de classificadores!

Projetando Workflows de Machine Learning em Python

Workflows turbinados

Projetando Workflows de Machine Learning em Python

Preparing Video For Download...