C.W.K.
Stream
Lesson 08 of 08 · published

scikit-learn으로 짠 데이터 Pipeline

~30 min · pipelines, sklearn, reproducibility

Level 0Scout
0 XP0/48 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

pipeline이 유일한 길

scikit-learn Pipeline이 preprocessing과 estimator를 single fittable / predictable / serializable 객체로 wrap. 디시플린은 raw row → prediction의 유일한 길을 pipeline으로 만드는 거. notebook cell preprocessing 금지, hand-applied transformation 금지, "아 predict 시점에 scale 까먹었네" 금지.

왜 이게 너를 구하나

pipeline은 joblib으로 올바르게 serialize, production에 단일 artifact로 ship, predict 시점에 동일한 transformation 적용. CV가 fold마다 전체 pipeline 실행해서 preprocessing leakage 자동 방지. GridSearchCV가 preprocessing과 estimator를 동시에 튜닝 가능.

디시플린

pipeline을 training과 serving 사이의 계약으로 다뤄. pipeline.predict(raw_row)로 prediction 재현 못 하면, pipeline 미완성이고 ship 대기 중인 버그가 있는 거.

Code

cross-validation 동반한 end-to-end pipeline·python
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score

pre = ColumnTransformer([
    ("num", Pipeline([("impute", SimpleImputer(strategy="median")), ("scale", StandardScaler())]), numeric_cols),
    ("cat", OneHotEncoder(handle_unknown="ignore"), categorical_cols),
])
pipe = Pipeline([("pre", pre), ("clf", LogisticRegression(max_iter=500))])
scores = cross_val_score(pipe, X, y, cv=5, scoring="average_precision")
print("PR-AUC:", scores.mean(), "+/-", scores.std())
pipeline을 단일 artifact로 save / load·python
import joblib

pipe.fit(X_train, y_train)
joblib.dump(pipe, "artifacts/churn_v1.joblib")

loaded = joblib.load("artifacts/churn_v1.joblib")
loaded.predict(X_new_raw_rows)  # transforms + predicts in one call

External links

Exercise

training notebook을 refactor해서 raw DataFrame → prediction의 유일한 길이 joblib에 저장된 한 fitted Pipeline이 되게. 새 kernel에서 pipeline load하고 pipe.predict(raw_row) 돌려서 검증.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.