-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_dot.py
More file actions
365 lines (336 loc) · 14.2 KB
/
test_dot.py
File metadata and controls
365 lines (336 loc) · 14.2 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import unittest
from io import StringIO
from textwrap import dedent
import pandas
from sklearn import datasets
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline, FeatureUnion, make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from mlinsights.ext_test_case import ExtTestCase
from mlinsights.plotting import pipeline2dot, pipeline2str
class TestDot(ExtTestCase):
def test_dot_df(self):
iris = datasets.load_iris()
X = iris.data[:, :4]
df = pandas.DataFrame(X)
df.columns = ["X1", "X2", "X3", "X4"]
clf = LogisticRegression()
dot = pipeline2dot(clf, df)
self.assertIn("digraph{", dot)
self.assertIn("PredictedLabel|", dot)
def test_dot_array(self):
iris = datasets.load_iris()
X = iris.data[:, :4]
clf = LogisticRegression()
dot = pipeline2dot(clf, X)
self.assertIn("digraph{", dot)
self.assertIn("PredictedLabel|", dot)
def test_dot_list(self):
clf = LogisticRegression()
dot = pipeline2dot(clf, ["X1", "X2"])
self.assertIn("digraph{", dot)
self.assertIn("PredictedLabel|", dot)
def test_dot_list_reg(self):
clf = LinearRegression()
dot = pipeline2dot(clf, ["X1", "X2"])
self.assertIn("digraph{", dot)
self.assertIn("Prediction", dot)
self.assertIn("LinearRegression", dot)
def test_dot_list_tr(self):
clf = StandardScaler()
dot = pipeline2dot(clf, ["X1", "X2"])
self.assertIn("digraph{", dot)
self.assertIn("StandardScaler", dot)
def test_pipeline(self):
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")),
]
)
dot = pipeline2dot(clf, columns)
self.assertIn("digraph{", dot)
self.assertIn("StandardScaler", dot)
def test_union_features(self):
columns = ["X", "Y"]
model = Pipeline(
[
("scaler1", StandardScaler()),
(
"union",
FeatureUnion(
[("scaler2", StandardScaler()), ("scaler3", MinMaxScaler())]
),
),
]
)
dot = pipeline2dot(model, columns)
self.assertIn("digraph{", dot)
self.assertIn("StandardScaler", dot)
self.assertIn("MinMaxScaler", dot)
def test_onehotencoder_dot(self):
data = dedent("""
date,value,notrend,trend,weekday,lag1,lag2,lag3,lag4,lag5,lag6,lag7,lag8
2017-07-10 13:27:04.669830,0.003463591425601385,0.0004596547917981044,0.0030039366338032807,
###0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
2017-07-11 13:27:04.669830,0.004411953385609647,0.001342107238927262,0.003069846146682385,1,
###0.003463591425601385,0.0,0.0,0.0,0.0,0.0,0.0,0.0
2017-07-12 13:27:04.669830,0.004277700876279705,0.0011426168863444912,0.0031350839899352135,2,
###0.004411953385609647,0.003463591425601385,0.0,0.0,0.0,0.0,0.0,0.0
2017-07-13 13:27:04.669830,0.006078151848127084,0.0028784976490072987,0.003199654199119785,3,
###0.004277700876279705,0.004411953385609647,0.003463591425601385,0.0,0.0,0.0,0.0,0.0
2017-07-14 13:27:04.669830,0.006336617719481035,0.003073056920386795,0.0032635607990942395,
###4,0.006078151848127084,0.004277700876279705,0.004411953385609647,0.003463591425601385,0.0,0.0,0.0,0.0
2017-07-15 13:27:04.669830,0.008716378909294038,0.0053895711052771985,0.0033268078040168394,5,
###0.006336617719481035,0.006078151848127084,0.004277700876279705,0.004411953385609647,0.003463591425601385,0.0,0.0,0.0
2017-07-17 13:27:04.669830,0.0035533180858140765,0.00010197905397394454,0.003451339031840132,0,
###0.008716378909294038,0.006336617719481035,0.006078151848127084,0.004277700876279705,0.004411953385609647,0.003463591425601385,0.0,0.0
2017-07-18 13:27:04.669830,0.0038464710972236286,0.0003338398676656705,0.003512631229557958,
###1,0.0035533180858140765,0.008716378909294038,0.006336617719481035,0.006078151848127084,0.004277700876279705,
###0.004411953385609647,0.003463591425601385,0.0
2017-07-19 13:27:04.669830,0.004200435956007872,0.0006271561741496745,0.003573279781858197,2,0.0038464710972236286,
###0.0035533180858140765,0.008716378909294038,0.006336617719481035,0.006078151848127084,0.004277700876279705,
###0.004411953385609647,0.003463591425601385
2017-07-20 13:27:04.669830,0.004773874566436903,0.0011405859170371827,0.00363328864939972,3,0.004200435956007872,
###0.0038464710972236286,0.0035533180858140765,0.008716378909294038,0.006336617719481035,0.006078151848127084,
###0.004277700876279705,0.004411953385609647
2017-07-21 13:27:04.669830,0.005866058541412791,0.00217339675927127,0.0036926617821415207,4,0.004773874566436903,
###0.004200435956007872,0.0038464710972236286,0.0035533180858140765,0.008716378909294038,0.006336617719481035,
###0.006078151848127084,0.004277700876279705
""").replace("\n###", "")
df = pandas.read_csv(StringIO(data))
cols = ["lag1", "lag2", "lag3", "lag4", "lag5", "lag6", "lag7", "lag8"]
model = make_pipeline(
make_pipeline(
ColumnTransformer(
[
("pass", "passthrough", cols),
("dummies", OneHotEncoder(), ["weekday"]),
]
),
PCA(n_components=2),
),
LinearRegression(),
)
train_cols = [*cols, "weekday"]
model.fit(df, df[train_cols])
dot = pipeline2dot(model, df)
self.assertIn('label="Identity"', dot)
def test_pipeline_tr_small(self):
buffer = """
fixed_acidity,volatile_acidity,citric_acid,residual_sugar,chlorides,free_sulfur_dioxide,total_sulfur_dioxide,density,pH,sulphates,alcohol,quality,color
7.4,0.7,0.0,1.9,0.076,11.0,34.0,0.9978,3.51,0.56,9.4,5,red
7.8,0.88,0.0,2.6,0.098,25.0,67.0,0.9968,3.2,0.68,9.8,5,red
7.8,0.76,0.04,2.3,0.092,15.0,54.0,0.997,3.26,0.65,9.8,5,red
11.2,0.28,0.56,1.9,0.075,17.0,60.0,0.998,3.16,0.58,9.8,6,white
7.4,0.7,0.0,1.9,0.076,11.0,34.0,0.9978,3.51,0.56,9.4,5,red
""".replace(" ", "")
X_train = pandas.read_csv(StringIO(buffer)).drop("quality", axis=1)
pipe = Pipeline(
[
(
"prep",
ColumnTransformer(
[
(
"color",
Pipeline(
[
("one", "passthrough"),
(
"select",
ColumnTransformer(
[("sel1", "passthrough", [0])]
),
),
]
),
["color"],
),
]
),
),
]
)
pipe.fit(X_train)
dot = pipeline2dot(pipe, X_train)
self.assertNotIn("i -> node2;", dot)
def test_pipeline_tr(self):
buffer = """
fixed_acidity,volatile_acidity,citric_acid,residual_sugar,chlorides,free_sulfur_dioxide,total_sulfur_dioxide,density,pH,sulphates,alcohol,quality,color
7.4,0.7,0.0,1.9,0.076,11.0,34.0,0.9978,3.51,0.56,9.4,5,red
7.8,0.88,0.0,2.6,0.098,25.0,67.0,0.9968,3.2,0.68,9.8,5,red
7.8,0.76,0.04,2.3,0.092,15.0,54.0,0.997,3.26,0.65,9.8,5,red
11.2,0.28,0.56,1.9,0.075,17.0,60.0,0.998,3.16,0.58,9.8,6,white
7.4,0.7,0.0,1.9,0.076,11.0,34.0,0.9978,3.51,0.56,9.4,5,red
""".replace(" ", "")
X_train = pandas.read_csv(StringIO(buffer)).drop("quality", axis=1)
numeric_features = [c for c in X_train if c != "color"]
pipe = Pipeline(
[
(
"prep",
ColumnTransformer(
[
(
"color",
Pipeline(
[
("one", OneHotEncoder()),
(
"select",
ColumnTransformer(
[("sel1", "passthrough", [0])]
),
),
]
),
["color"],
),
("others", "passthrough", numeric_features),
]
),
),
]
)
pipe.fit(X_train)
dot = pipeline2dot(pipe, X_train)
self.assertIn("OneHotEncoder", dot)
# self.assertIn("sch3:f10 -> node4;", dot)
dots = pipeline2str(pipe)
self.assertIn("OneHotEncoder", dots)
self.assertIn("PassThrough(0)", dots)
def test_pipeline_bug(self):
iris = datasets.load_iris()
X = iris.data
y = iris.target
pipe2 = Pipeline(
[
(
"multi",
ColumnTransformer(
[
("c01", Normalizer(), [0, 1]),
("c23", MinMaxScaler(), [2, 3]),
]
),
),
("pca", PCA()),
("lr", LogisticRegression()),
]
)
pipe2.fit(X, y)
dot = pipeline2dot(pipe2, X)
self.assertIn("MinMaxScaler", dot)
self.assertNotIn(" 0 -> node1;", dot)
self.assertIn(" sch0:f0 -> node1;", dot)
def test_pipeline_bug2(self):
iris = datasets.load_iris()
X = iris.data
y = iris.target
pipe2 = Pipeline(
[
(
"multi",
ColumnTransformer(
[
("c01a", Normalizer(), [0, 1]),
("c23a", MinMaxScaler(), [2, 3]),
]
),
),
(
"multi2",
ColumnTransformer(
[
("c01b", Normalizer(), [0, 1]),
("c23b", MinMaxScaler(), [2, 3]),
]
),
),
("pca", PCA()),
("lr", LogisticRegression()),
]
)
pipe2.fit(X, y)
dot = pipeline2dot(pipe2, X)
self.assertIn("MinMaxScaler", dot)
self.assertNotIn(" 0 -> node1;", dot)
# self.assertNotIn("sch1:f0 -> node2;", dot)
def test_pipeline_passthrough(self):
data = pandas.DataFrame(
[
dict(CAT1="a", CAT2="c", num1=0.5, num2=0.6, y=0),
dict(CAT1="b", CAT2="d", num1=0.4, num2=0.8, y=1),
dict(CAT1="a", CAT2="d", num1=0.5, num2=0.56, y=0),
dict(CAT1="a", CAT2="d", num1=0.55, num2=0.56, y=1),
dict(CAT1="a", CAT2="c", num1=0.35, num2=0.86, y=0),
dict(CAT1="a", CAT2="c", num1=0.5, num2=0.68, y=1),
]
)
cat_cols = ["CAT1", "CAT2"]
train_data = data.drop("y", axis=1)
# numeric_transformer = Pipeline(steps=[('scaler', StandardScaler())])
categorical_transformer = Pipeline(
[("onehot", OneHotEncoder(sparse_output=False, handle_unknown="ignore"))]
)
preprocessor = ColumnTransformer(
transformers=[("cat", categorical_transformer, cat_cols)],
remainder="passthrough",
)
pipe = Pipeline(
[
("preprocess", preprocessor),
("rf", RandomForestClassifier(n_estimators=2)),
]
)
pipe.fit(train_data, data["y"])
dot = pipeline2dot(pipe, train_data)
self.assertIn("sch0:f2 ->", dot)
self.assertNotIn("node3 -> sch3:f34;", dot)
self.assertIn("node3 -> sch3:f0;", dot)
self.assertIn("node3 -> sch3:f1;", dot)
self.assertNotIn("node3 -> sch3:f2;", dot)
if __name__ == "__main__":
# TestDot().test_pipeline_passthrough()
unittest.main()