워크플로에서 파이프라인으로

Python으로 설계하는 Machine Learning 워크플로

Dr. Chris Anagnostopoulos

Honorary Associate Professor

워크플로 다시 보기

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))
Python으로 설계하는 Machine Learning 워크플로

그리드 서치의 힘

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

깊이와 추정기 수의 모든 조합 표에서, 세 값을 탐색했고 하나가 최선으로 선정되었음을 보여줍니다.

Python으로 설계하는 Machine Learning 워크플로

그리드 서치의 힘

다음으로 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']

깊이와 추정기 수의 모든 조합 표에서, 다섯 값을 탐색했고 다른 값이 최선으로 선정되었음을 보여줍니다.

Python으로 설계하는 Machine Learning 워크플로

그리드 서치의 힘

max_depthn_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}

깊이와 추정기 수의 모든 조합 표에서, 모든 값을 탐색했고 동일한 값이 최선으로 선정되었음을 보여줍니다.

Python으로 설계하는 Machine Learning 워크플로

파이프라인

이 다이어그램에서 두 개의 하이퍼파라미터가 있는 랜덤 포레스트가 하나의 하이퍼파라미터가 있는 특성 선택기와 화살표로 연결되어 있습니다.

Python으로 설계하는 Machine Learning 워크플로

파이프라인

두 객체가 하나의 상자로 함께 감싸져 있습니다.

Python으로 설계하는 Machine Learning 워크플로

파이프라인

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}
Python으로 설계하는 Machine Learning 워크플로

파이프라인 사용자화

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)
Python으로 설계하는 Machine Learning 워크플로

과도하게 시도하지 마십시오

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회 학습!

Python으로 설계하는 Machine Learning 워크플로

강력해진 워크플로

Python으로 설계하는 Machine Learning 워크플로

Preparing Video For Download...