การ deploy โมเดล

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

Dr. Chris Anagnostopoulos

Honorary Associate Professor

ออบเจกต์ pipeline ของโมเดลที่ประกอบด้วยการเลือกฟีเจอร์ การเลือกโมเดล การปรับแต่งโมเดล และการ fit โมเดล ถูกส่งไปยัง production pipeline เพื่อใช้ทำนายผล

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

การ serialize โมเดล

บันทึก classifier ลงไฟล์:

import pickle
clf = RandomForestClassifier().fit(X_train, y_train)
with open('model.pkl', 'wb') as file:
    pickle.dump(clf, file=file)

โหลดกลับจากไฟล์:

with open('model.pkl', 'rb') as file:
    clf2 = pickle.load(file)
การออกแบบ Machine Learning Workflows ด้วย Python

การ serialize pipeline

สภาพแวดล้อม development:

vt = SelectKBest(f_classif).fit(
    X_train, y_train)
clf = RandomForestClassifier().fit(
    vt.transform(X_train), y_train)
with open('vt.pkl', 'wb') as file: 
     pickle.dump(vt)
with open('clf.pkl', 'wb') as file: 
     pickle.dump(clf)

ขั้นตอนการทำงานที่ออบเจกต์ตัวเลือกฟีเจอร์และโมเดลที่ fit แล้วต้องถูกส่งไปยัง production แยกกัน

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

การ serialize pipeline

สภาพแวดล้อม production:

with open('vt.pkl', 'rb') as file: 
    vt = pickle.load(vt)
with open('clf.pkl', 'rb') as file: 
    clf = pickle.load(clf)
clf.predict(vt.transform(X_new))

ขั้นตอนการทำงานที่ออบเจกต์ตัวเลือกฟีเจอร์และโมเดลที่ fit แล้วต้องถูกส่งไปยัง production แยกกัน

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

การ serialize pipeline

สภาพแวดล้อม development:

pipe = Pipeline([
    ('fs', SelectKBest(f_classif)), 
    ('clf', RandomForestClassifier())
])
params = dict(fs__k=[2, 3, 4],
    clf__max_depth=[5, 10, 20])
gs = GridSearchCV(pipe, params)
gs = gs.fit(X_train, y_train)

with open('pipe.pkl', 'wb') as file: pickle.dump(gs, file)

ขั้นตอนการทำงานที่ได้รับการปรับปรุง โดยทั้งการเลือกฟีเจอร์และการ fit โมเดลอยู่ใน pipeline ออบเจกต์เดียว

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

การ serialize pipeline

สภาพแวดล้อม production:

with open('pipe.pkl', 'rb') as file:
   gs = pickle.dump(gs, file)
gs.predict(X_test)

ขั้นตอนการทำงานที่ได้รับการปรับปรุง โดยทั้งการเลือกฟีเจอร์และการ fit โมเดลอยู่ใน pipeline ออบเจกต์เดียว

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

การแปลงฟีเจอร์แบบกำหนดเอง

   checking_status  duration       ...        own_telephone  foreign_worker
0                1         6       ...                    1               1
1                0        48       ...                    0               1
def negate_second_column(X):
    Z = X.copy()
    Z[:,1] = -Z[:,1]
    return Z
pipe = Pipeline([('ft', FunctionTransformer(negate_second_column)), 
    ('clf', RandomForestClassifier())])
การออกแบบ Machine Learning Workflows ด้วย Python

พร้อมสำหรับ production!

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

Preparing Video For Download...