注意
转到结尾下载完整示例代码。或通过 JupyterLite 或 Binder 在浏览器中运行此示例
显示管线#
在 Jupyter Notebook 中显示管线的默认配置是 'diagram'
,其中 set_config(display='diagram')
。要禁用 HTML 表示,请使用 set_config(display='text')
。
要查看管线可视化的更详细步骤,请单击管线中的步骤。
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause
显示带有预处理步骤和分类器的管线#
此部分构建一个带有预处理步骤 StandardScaler
和分类器 LogisticRegression
的 Pipeline
,并显示其可视化表示。
from sklearn import set_config
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
steps = [
("preprocessing", StandardScaler()),
("classifier", LogisticRegression()),
]
pipe = Pipeline(steps)
为了可视化图表,默认设置为 display='diagram'
。
set_config(display="diagram")
pipe # click on the diagram below to see the details of each step
要查看文本管线,请更改为 display='text'
。
set_config(display="text")
pipe
Pipeline(steps=[('preprocessing', StandardScaler()),
('classifier', LogisticRegression())])
恢复默认显示
set_config(display="diagram")
显示链式连接多个预处理步骤和分类器的管线#
此部分构建一个带有多个预处理步骤 PolynomialFeatures
和 StandardScaler
以及分类器步骤 LogisticRegression
的 Pipeline
,并显示其可视化表示。
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
steps = [
("standard_scaler", StandardScaler()),
("polynomial", PolynomialFeatures(degree=3)),
("classifier", LogisticRegression(C=2.0)),
]
pipe = Pipeline(steps)
pipe # click on the diagram below to see the details of each step
显示管线、降维和分类器#
此部分构建一个带有降维步骤 PCA
和分类器 SVC
的 Pipeline
,并显示其可视化表示。
显示链式连接 Column Transformer 的复杂管线#
此部分构建一个带有 ColumnTransformer
和分类器 LogisticRegression
的复杂 Pipeline
,并显示其可视化表示。
import numpy as np
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
numeric_preprocessor = Pipeline(
steps=[
("imputation_mean", SimpleImputer(missing_values=np.nan, strategy="mean")),
("scaler", StandardScaler()),
]
)
categorical_preprocessor = Pipeline(
steps=[
(
"imputation_constant",
SimpleImputer(fill_value="missing", strategy="constant"),
),
("onehot", OneHotEncoder(handle_unknown="ignore")),
]
)
preprocessor = ColumnTransformer(
[
("categorical", categorical_preprocessor, ["state", "gender"]),
("numerical", numeric_preprocessor, ["age", "weight"]),
]
)
pipe = make_pipeline(preprocessor, LogisticRegression(max_iter=500))
pipe # click on the diagram below to see the details of each step
在带有分类器的管线上显示网格搜索#
此部分构建一个在带有 RandomForestClassifier
的 Pipeline
上进行 GridSearchCV
,并显示其可视化表示。
import numpy as np
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestClassifier
from sklearn.impute import SimpleImputer
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
numeric_preprocessor = Pipeline(
steps=[
("imputation_mean", SimpleImputer(missing_values=np.nan, strategy="mean")),
("scaler", StandardScaler()),
]
)
categorical_preprocessor = Pipeline(
steps=[
(
"imputation_constant",
SimpleImputer(fill_value="missing", strategy="constant"),
),
("onehot", OneHotEncoder(handle_unknown="ignore")),
]
)
preprocessor = ColumnTransformer(
[
("categorical", categorical_preprocessor, ["state", "gender"]),
("numerical", numeric_preprocessor, ["age", "weight"]),
]
)
pipe = Pipeline(
steps=[("preprocessor", preprocessor), ("classifier", RandomForestClassifier())]
)
param_grid = {
"classifier__n_estimators": [200, 500],
"classifier__max_features": ["auto", "sqrt", "log2"],
"classifier__max_depth": [4, 5, 6, 7, 8],
"classifier__criterion": ["gini", "entropy"],
}
grid_search = GridSearchCV(pipe, param_grid=param_grid, n_jobs=1)
grid_search # click on the diagram below to see the details of each step