-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathplot_visualize_pipeline.py
More file actions
242 lines (174 loc) · 5.51 KB
/
plot_visualize_pipeline.py
File metadata and controls
242 lines (174 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
.. _l-visualize-pipeline-example:
Visualize a scikit-learn pipeline
=================================
Pipeline can be big with *scikit-learn*, let's dig into a visual way to
look a them.
Simple model
------------
Let's vizualize a simple pipeline, a single model not even trained.
"""
from numpy.random import randn
import pandas
from PIL import Image
from sphinx_runpython.runpython import run_cmd
from sklearn import datasets
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.preprocessing import (
OneHotEncoder,
StandardScaler,
MinMaxScaler,
PolynomialFeatures,
)
from mlinsights.helpers.pipeline import (
alter_pipeline_for_debugging,
enumerate_pipeline_models,
)
from mlinsights.plotting import pipeline2dot, pipeline2str
iris = datasets.load_iris()
X = iris.data[:, :4]
df = pandas.DataFrame(X)
df.columns = ["X1", "X2", "X3", "X4"]
clf = LogisticRegression()
clf
######################################################################
# The trick consists in converting the pipeline in a graph through the
# `DOT <https://en.wikipedia.org/wiki/DOT_(graph_description_language)>`_
# language.
dot = pipeline2dot(clf, df)
print(dot)
######################################################################
# It is lot better with an image.
dot_file = "graph.dot"
with open(dot_file, "w", encoding="utf-8") as f:
f.write(dot)
########################################
#
cmd = "dot -G=300 -Tpng {0} -o{0}.png".format(dot_file)
run_cmd(cmd, wait=True)
img = Image.open("graph.dot.png")
img
######################################################################
# Complex pipeline
# ----------------
#
# *scikit-learn* instroduced a couple of transform to play with features
# in a single pipeline. The following example is taken from `Column
# Transformer with Mixed
# Types <https://scikit-learn.org/stable/auto_examples/compose/plot_column_transformer_mixed_types.html#sphx-glr-auto-examples-compose-plot-column-transformer-mixed-types-py>`_.
columns = [
"pclass",
"name",
"sex",
"age",
"sibsp",
"parch",
"ticket",
"fare",
"cabin",
"embarked",
"boat",
"body",
"home.dest",
]
numeric_features = ["age", "fare"]
numeric_transformer = Pipeline(
steps=[("imputer", SimpleImputer(strategy="median")), ("scaler", StandardScaler())]
)
categorical_features = ["embarked", "sex", "pclass"]
categorical_transformer = Pipeline(
steps=[
("imputer", SimpleImputer(strategy="constant", fill_value="missing")),
("onehot", OneHotEncoder(handle_unknown="ignore")),
]
)
preprocessor = ColumnTransformer(
transformers=[
("num", numeric_transformer, numeric_features),
("cat", categorical_transformer, categorical_features),
]
)
clf = Pipeline(
steps=[
("preprocessor", preprocessor),
("classifier", LogisticRegression(solver="lbfgs")),
]
)
clf
######################################################################
# Let's see it first as a simplified text.
print(pipeline2str(clf))
########################################
#
dot = pipeline2dot(clf, columns)
dot_file = "graph2.dot"
with open(dot_file, "w", encoding="utf-8") as f:
f.write(dot)
cmd = "dot -G=300 -Tpng {0} -o{0}.png".format(dot_file)
run_cmd(cmd, wait=True)
img = Image.open("graph2.dot.png")
img
######################################################################
# Example with FeatureUnion
# -------------------------
model = Pipeline(
[
("poly", PolynomialFeatures()),
(
"union",
FeatureUnion([("scaler2", MinMaxScaler()), ("scaler3", StandardScaler())]),
),
]
)
dot = pipeline2dot(model, columns)
dot_file = "graph3.dot"
with open(dot_file, "w", encoding="utf-8") as f:
f.write(dot)
cmd = "dot -G=300 -Tpng {0} -o{0}.png".format(dot_file)
run_cmd(cmd, wait=True)
img = Image.open("graph3.dot.png")
img
######################################################################
# Compute intermediate outputs
# ----------------------------
# It is difficult to access intermediate outputs with *scikit-learn* but
# it may be interesting to do so. The method
# `alter_pipeline_for_debugging <find://alter_pipeline_for_debugging>`_
# modifies the pipeline to intercept intermediate outputs.
model = Pipeline(
[
("scaler1", StandardScaler()),
(
"union",
FeatureUnion([("scaler2", StandardScaler()), ("scaler3", MinMaxScaler())]),
),
("lr", LinearRegression()),
]
)
X = randn(4, 5)
y = randn(4)
model.fit(X, y)
########################################
#
print(pipeline2str(model))
######################################################################
# Let's now modify the pipeline to get the intermediate outputs.
alter_pipeline_for_debugging(model)
######################################################################
# The function adds a member ``_debug`` which stores inputs and outputs in
# every piece of the pipeline.
model.steps[0][1]._debug
########################################
#
model.predict(X)
######################################################################
# The member was populated with inputs and outputs.
model.steps[0][1]._debug
######################################################################
# Every piece behaves the same way.
for coor, m, _vars in enumerate_pipeline_models(model):
print(coor)
print(m._debug)