จาก workflow สู่ pipeline

การออกแบบ Machine Learning Workflows ด้วย Python

Dr. Chris Anagnostopoulos

Honorary Associate Professor

ทบทวน 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))
การออกแบบ Machine Learning Workflows ด้วย Python

ประสิทธิภาพของ grid search

ปรับแต่ง 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']

ตารางแสดงค่าผสมทั้งหมดของ depth และจำนวน estimator โดยมีสามค่าที่ถูกสำรวจแล้วและพบค่าที่ดีที่สุดหนึ่งค่า

การออกแบบ Machine Learning Workflows ด้วย Python

ประสิทธิภาพของ grid search

จากนั้นปรับแต่ง 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']

ตารางแสดงค่าผสมทั้งหมดของ depth และจำนวน estimator โดยมีห้าค่าที่ถูกสำรวจแล้วและพบค่าที่ดีที่สุดอีกหนึ่งค่า

การออกแบบ Machine Learning Workflows ด้วย Python

ประสิทธิภาพของ grid search

ปรับแต่ง max_depth และ 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}

ตารางแสดงค่าผสมทั้งหมดของ depth และจำนวน estimator โดยสำรวจทุกค่าแล้วและพบว่าค่าที่ดีที่สุดเหมือนเดิม

การออกแบบ Machine Learning Workflows ด้วย Python

Pipeline

ในแผนภาพนี้ random forest ซึ่งมีสอง hyperparameter เชื่อมต่อกับ feature selector ที่มีหนึ่ง hyperparameter ด้วยลูกศร

การออกแบบ Machine Learning Workflows ด้วย Python

Pipeline

ออบเจกต์ทั้งสองถูกรวมไว้ในกล่องเดียวกัน

การออกแบบ Machine Learning Workflows ด้วย Python

Pipeline

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}
การออกแบบ Machine Learning Workflows ด้วย Python

ปรับแต่ง 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)
การออกแบบ Machine Learning Workflows ด้วย Python

อย่าใช้มากเกินไป

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 classifier fits!

การออกแบบ Machine Learning Workflows ด้วย Python

Workflow ที่ทรงพลังยิ่งขึ้น

การออกแบบ Machine Learning Workflows ด้วย Python

Preparing Video For Download...