构建您的第一个管道:标准化器加分类器
您将把 StandardScaler 和 LogisticRegression 链接到 Pipeline 中,调用 fit 和 predict,并确认标准化器只在训练数据上拟合。
构建您的第一个管道:标准化器加分类器 是 CoddyKit 上的免费 Machine Learning Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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.
常见问题解答
「构建您的第一个管道:标准化器加分类器」课时是免费的吗?
是的 — 「构建您的第一个管道:标准化器加分类器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Machine Learning Academy 课程的其余内容,请升级到 CoddyKit PRO。 Machine Learning Academy 课程共包含 4 节课。
「构建您的第一个管道:标准化器加分类器」这节课中我会学到什么?
您将把 StandardScaler 和 LogisticRegression 链接到 Pipeline 中,调用 fit 和 predict,并确认标准化器只在训练数据上拟合。 你通过在浏览器中直接运行的动手代码来练习 Machine Learning Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Machine Learning Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Machine Learning Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「构建您的第一个管道:标准化器加分类器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Machine Learning Academy 课中编写并运行代码吗?
能。每节 Machine Learning Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 构建您的第一个管道:标准化器加分类器
- 管道中的 ColumnTransformer
- 对完整管道进行交叉验证与网格搜索
- 使用 joblib 保存和加载管道