-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvectorization05b.py
More file actions
215 lines (162 loc) · 5.77 KB
/
vectorization05b.py
File metadata and controls
215 lines (162 loc) · 5.77 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
import os
import pandas as pd
import pydiverse.transform as pdt
import xgboost
import xgboost as xgb
from pydiverse.pipedag import Blob, Flow, Stage, Table, materialize
from pydiverse.transform import aligned, λ
from pydiverse.transform.core.dtypes import String
from pydiverse.transform.core.verbs import (
alias,
collect,
filter,
left_join,
mutate,
select,
build_query,
)
from pydiverse.transform.eager import PandasTableImpl
from pydiverse.transform.lazy import SQLTableImpl
@pdt.verb
def transmute(tbl, **kwargs):
return tbl >> select() >> mutate(**kwargs)
@pdt.verb
def trim_all_str(tbl):
for col in tbl:
if isinstance(col._.dtype, String):
tbl[col] = col.strip()
return tbl
def pk(x: pdt.Table):
# This is just a placeholder.
# Ideally there would be a global function in pydiverse transform to
# get the primary key (and another one to get the table / col name)
return x.pk
def pk_match(x: pdt.Table, y: pdt.Table):
return pk(x) == pk(y)
def get_named_tables(tables: list[pdt.Table]) -> dict[str, pdt.Table]:
return {tbl._impl.name: tbl for tbl in tables}
@materialize(version="1.0.0")
def read_input_data(src_dir="data/pipedag_example_data"):
return [
Table(pd.read_csv(os.path.join(src_dir, file)), name=file.removesuffix(".csv.gz"))
for file in os.listdir(src_dir)
if file.endswith(".csv.gz")
]
@materialize(input_type=SQLTableImpl, lazy=True)
def clean(src_tbls: list[pdt.Table]):
return [tbl >> trim_all_str() for tbl in src_tbls]
@materialize(input_type=SQLTableImpl, lazy=True, nout=3)
def transform(src_tbls: list[pdt.Table]):
named_tbls = get_named_tables(src_tbls)
a = named_tbls["a"]
b = named_tbls["b"]
c = named_tbls["c"]
def join_b(a):
return a >> left_join(b >> select(), pk_match(a, b))
new_a = join_b(a) >> mutate(x=b.x)
new_b = b
new_c = c
return new_a, new_b, new_c
@materialize(input_type=SQLTableImpl, lazy=True)
def lazy_features(a: pdt.Table, src_tbls: list[pdt.Table]):
named_tbls = get_named_tables(src_tbls)
b = named_tbls["b"]
return (
a
>> left_join(b, pk_match(a, b))
>> transmute(pk=pk(a), aiige=a.age, y=b.y, z=b.z * 2)
>> alias("lazy_features")
)
@materialize(input_type=PandasTableImpl, version="2.3.5")
def eager_features(a: pdt.Table, src_tbls: list[pdt.Table]):
named_tbls = get_named_tables(src_tbls)
c = named_tbls["c"]
return (
a
>> left_join(c, pk_match(a, c))
>> transmute(pk=pk(a), xx=c.x, yy=c.y * 2, zz=c.z + 3)
>> alias("eager_features")
)
@materialize(input_type=SQLTableImpl, lazy=True)
def combine_features(features1: pdt.Table, features2: pdt.Table):
return (
features1
>> left_join(
features2 >> select(-pk(features2)), pk_match(features1, features2)
)
>> alias("features")
)
@materialize(input_type=SQLTableImpl, lazy=True, nout=2)
def train_and_test_set(flat_table: pdt.Table, features: pdt.Table):
tbl = (
flat_table
>> left_join(features, pk_match(flat_table, features))
>> mutate(row_num=pdt.functions.row_number(arrange=[pk(flat_table)]))
>> select(-pk(flat_table), -pk(features))
)
training_set = (
tbl
>> filter(λ.row_num % 10 != 0)
>> select(-λ.row_num)
>> alias("training_set")
)
test_set = (
tbl >> filter(λ.row_num % 10 == 0) >> select(-λ.row_num) >> alias("test_set")
)
return (training_set, test_set)
@materialize(input_type=pd.DataFrame, version="4.5.8")
def model_training(train_set: pd.DataFrame):
x = train_set.drop("target", axis=1)
y = train_set["target"]
dtrain = xgb.DMatrix(x, label=y)
params = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"}
model = xgb.train(params, dtrain)
return Blob(model, "model")
@aligned(with_="test_set")
def predict(model: xgboost.Booster, test_set: pdt.Table):
x = test_set >> select(-λ.target) >> collect()
# Ugly hack to convert new pandas dtypes to numpy dtypes, because xgboost
# requires numpy dtypes.
x = x.astype(
x.dtypes.map(lambda d: d.numpy_dtype if hasattr(d, "numpy_dtype") else d)
)
dx = xgb.DMatrix(x)
predict_col = model.predict(dx)
return pdt.Table(
PandasTableImpl("prediction", pd.DataFrame({"prediction": predict_col}))
).prediction
@materialize(input_type=PandasTableImpl, version="3.4.5")
def model_evaluation(model: xgboost.Booster, test_set: pdt.Table):
prediction = predict(model, test_set) # produces an aligned vector with input
return (
test_set
>> select(λ.target)
>> mutate(prediction=prediction)
>> mutate(abs_error=abs(λ.target - λ.prediction))
>> alias("evaluation")
)
def get_pipeline():
with Flow("flow") as flow:
with Stage("1_raw_input"):
raw_tbls = read_input_data()
with Stage("2_clean_input"):
clean_tbls = clean(raw_tbls)
with Stage("3_transformed_data"):
a, b, c = transform(clean_tbls)
with Stage("4_features"):
features1 = lazy_features(a, [a, b, c]) # s3.tbls
features2 = eager_features(a, [a, b, c]) # s3.tbls
features = combine_features(features1, features2)
with Stage("5_model"):
train_set, test_set = train_and_test_set(a, features)
model = model_training(train_set)
with Stage("6_evaluation"):
_ = model_evaluation(model, test_set)
return flow
if __name__ == "__main__":
import logging
from pydiverse.pipedag.util.structlog import setup_logging
setup_logging(log_level=logging.INFO)
flow = get_pipeline()
result = flow.run()
assert result.successful