वर्कफ़्लो से पाइपलाइन तक

Python में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

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 में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

ग्रिड सर्च की शक्ति

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 और number of estimators के सभी संभावित संयोजनों की तालिका, दिखा रही है कि तीन मान जाँचे गए और एक सर्वोत्तम मिला।

Python में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

ग्रिड सर्च की शक्ति

अब 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 और number of estimators के सभी संभावित संयोजनों की तालिका, दिखा रही है कि पाँच मान जाँचे गए और एक नया सर्वोत्तम मिला।

Python में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

ग्रिड सर्च की शक्ति

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 और number of estimators के सभी संभावित संयोजनों की तालिका, दिखा रही है कि सभी मान जाँचे गए और वही सर्वोत्तम मिला।

Python में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

पाइपलाइन

इस आरेख में दो हाइपरपैरामीटर वाला random forest, एक हाइपरपैरामीटर वाले फीचर सेलेक्टर से तीर द्वारा जुड़ा है।

Python में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

पाइपलाइन

दोनों ऑब्जेक्ट्स को एक ही बॉक्स में लपेटा गया है।

Python में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

पाइपलाइन

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 में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

अपनी पाइपलाइन कस्टमाइज़ करें

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 में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

हद से ज़्यादा न करें

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 फिट्स!

Python में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

सुपरचार्ज्ड वर्कफ़्लो

Python में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना

Preparing Video For Download...