0Pricing
Machine Learning Academy · 강의

첫 파이프라인 만들기: 스케일러와 분류기

학습자는 StandardScaler와 LogisticRegression을 Pipeline으로 연결하고 fit과 predict를 호출한 뒤, 스케일러가 학습 데이터에만 적합되었는지 확인합니다.

첫 파이프라인 만들기: 스케일러와 분류기은(는) CoddyKit의 무료 Machine Learning Academy 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Machine Learning Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Machine Learning Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What Is a scikit-learn Pipeline?

A Pipeline chains multiple processing steps into a single estimator object. Each step except the last must implement fit and transform; the final step only needs fit and predict. Calling pipeline.fit(X, y) runs all steps in sequence, and pipeline.predict(X) passes data through all transforms before classifying. This eliminates bookkeeping errors and prevents data leakage.

Why Pipelines Prevent Leakage

If you fit a StandardScaler on the full dataset and then split into train/test, the scaler has seen test-set statistics — this is data leakage. A Pipeline solves this automatically: when you pass a Pipeline to cross_val_score or GridSearchCV, the entire pipeline (including the scaler) is re-fitted from scratch on each training fold, so the test fold never influences the scaler parameters.

Constructing Your First Pipeline

Create a Pipeline by passing a list of (name, estimator) tuples. The names are arbitrary strings you choose — they are used to reference steps later (e.g., for grid-search hyperparameters). The most common first pipeline is a scaler followed by a classifier.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

pipe = Pipeline([
    ('scaler', StandardScaler()),
    ('clf', LogisticRegression(C=1.0, max_iter=200))
])

print('Steps:', [name for name, _ in pipe.steps])
print('Named steps:', list(pipe.named_steps.keys()))

Fitting and Predicting

After construction, use the Pipeline exactly like any sklearn estimator: fit on training data, predict on test data, score for accuracy. Internally, fit calls fit_transform on all intermediate steps and fit on the final estimator.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

pipe = Pipeline([
    ('scaler', StandardScaler()),
    ('clf', LogisticRegression(max_iter=300))
])

pipe.fit(X_train, y_train)
print('Test accuracy:', pipe.score(X_test, y_test).round(4))

y_pred = pipe.predict(X_test)
print('Predictions[:5]:', y_pred[:5])

Confirming Scaler Fitted Only on Train Data

After fitting the Pipeline on training data, you can inspect each step's fitted parameters. The scaler inside the pipeline will have mean_ and scale_ attributes computed from the training set only — not the full dataset. This confirms the pipeline is doing the right thing.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
import numpy as np

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

pipe = Pipeline([('sc', StandardScaler()), ('lr', LogisticRegression(max_iter=300))])
pipe.fit(X_train, y_train)

# Scaler mean from pipeline vs computed from X_train
print('Pipeline scaler mean[0]:', pipe.named_steps['sc'].mean_[0].round(4))
print('Direct train mean[0]:   ', X_train[:, 0].mean().round(4))

Accessing Intermediate Outputs

To get the transformed output of a specific step, use pipeline[:-1].transform(X) or access individual steps via pipeline.named_steps['step_name']. You can also call pipeline[:'step_name'] with Python slice notation to get a sub-pipeline up to and including that step. This is useful for debugging or inspecting what the data looks like after scaling.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)

pipe = Pipeline([('sc', StandardScaler()), ('lr', LogisticRegression())])
pipe.fit(X, y)

# Get scaled output (all steps except the last)
X_scaled = pipe[:-1].transform(X)
print('After scaling — mean per feature:', X_scaled.mean(axis=0).round(4))
print('After scaling — std per feature:', X_scaled.std(axis=0).round(4))

make_pipeline: A Shortcut

make_pipeline creates a Pipeline without requiring you to specify step names manually — it generates names from the class names in lowercase. This is convenient for quick experiments but less readable in production code where explicit names help identify steps in grid-search parameter strings.

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
import numpy as np

X, y = load_iris(return_X_y=True)

# Auto-names: 'minmaxscaler' and 'kneighborsclassifier'
pipe = make_pipeline(MinMaxScaler(), KNeighborsClassifier(n_neighbors=5))
print('Step names:', list(pipe.named_steps.keys()))
print('CV accuracy:', cross_val_score(pipe, X, y, cv=5).mean().round(4))

Pipeline with Probability Predictions

If the final estimator supports predict_proba, the Pipeline exposes it too. This allows you to use a Pipeline with any function that expects probability outputs — like ROC-AUC scoring, calibration curves, or threshold tuning — without needing to manually apply preprocessing before calling the model.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

pipe = Pipeline([('sc', StandardScaler()), ('lr', LogisticRegression(max_iter=300))])
pipe.fit(X_train, y_train)

proba = pipe.predict_proba(X_test)[:, 1]
print('ROC-AUC:', roc_auc_score(y_test, proba).round(4))

Setting Parameters After Construction

You can update any step parameter after building the Pipeline using set_params(step__param=value) with the double-underscore separator. This is the same syntax used in GridSearchCV. It is useful when you want to experiment with different settings without rebuilding the entire pipeline from scratch.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

pipe = Pipeline([('sc', StandardScaler()), ('lr', LogisticRegression())])

# Change C and max_iter after construction
pipe.set_params(lr__C=10.0, lr__max_iter=500)
print('C after set_params:', pipe.named_steps['lr'].C)

Pickling and Sharing Pipelines

A fitted Pipeline is a single Python object that can be serialised with pickle or joblib. Sharing the pipeline as one file ensures that the exact same preprocessing steps — with the exact same scaler parameters — are applied at prediction time, eliminating consistency bugs between training and serving environments.

import joblib
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)
pipe = Pipeline([('sc', StandardScaler()), ('lr', LogisticRegression())])
pipe.fit(X, y)

# Save and reload
joblib.dump(pipe, '/tmp/iris_pipeline.pkl')
loaded_pipe = joblib.load('/tmp/iris_pipeline.pkl')

print('Predictions match:', (pipe.predict(X) == loaded_pipe.predict(X)).all())

Pipeline Cross-Validation Best Practice

Always wrap your Pipeline in cross_val_score rather than manually looping over folds. This ensures that the scaler is re-fitted on each training fold and never touches the validation fold, giving you an honest estimate of generalisation performance.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.datasets import load_digits
from sklearn.model_selection import cross_val_score
import numpy as np

X, y = load_digits(return_X_y=True)

pipe = Pipeline([
    ('sc', StandardScaler()),
    ('svm', SVC(kernel='rbf', C=5.0, gamma='scale'))
])

scores = cross_val_score(pipe, X, y, cv=5, n_jobs=-1)
print(f'CV accuracy: {np.mean(scores):.4f} +/- {np.std(scores):.4f}')

Quick Check

Test your understanding of scikit-learn Pipelines from this lesson.

Lesson Recap

In this lesson you learned: a Pipeline chains steps into one estimator, preventing leakage by fitting each step only on the training data it sees, make_pipeline provides auto-named shortcuts while explicit names improve grid-search readability, and a fitted Pipeline can be pickled and shared as a single artefact for consistent preprocessing at serving time. Next up we add ColumnTransformer inside a Pipeline to handle mixed numeric and categorical data.

자주 묻는 질문

“첫 파이프라인 만들기: 스케일러와 분류기” 강의는 무료인가요?

네 — “첫 파이프라인 만들기: 스케일러와 분류기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Machine Learning Academy 강의 전체를 잠금 해제할 수 있습니다. Machine Learning Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“첫 파이프라인 만들기: 스케일러와 분류기”에서 뭘 배우나요?

학습자는 StandardScaler와 LogisticRegression을 Pipeline으로 연결하고 fit과 predict를 호출한 뒤, 스케일러가 학습 데이터에만 적합되었는지 확인합니다. 브라우저에서 직접 실행하는 실습 코드로 Machine Learning Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Machine Learning Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Machine Learning Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“첫 파이프라인 만들기: 스케일러와 분류기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Machine Learning Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Machine Learning Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 첫 파이프라인 만들기: 스케일러와 분류기
  2. 파이프라인 안의 ColumnTransformer
  3. 전체 파이프라인의 교차 검증 및 그리드 탐색
  4. joblib를 이용한 파이프라인 저장 및 불러오기
← Machine Learning Academy(으)로 돌아가기