-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBanking_Classifier_pipeline_kuberflow.yaml
663 lines (608 loc) · 32.1 KB
/
Banking_Classifier_pipeline_kuberflow.yaml
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: banking-term-deposit-classifier-pipeline-with-kuberflow-
annotations: {pipelines.kubeflow.org/kfp_sdk_version: 1.8.18, pipelines.kubeflow.org/pipeline_compilation_time: '2023-07-20T15:24:47.154759',
pipelines.kubeflow.org/pipeline_spec: '{"description": "A sample pipeline that
performs Ramdom Classifer classifier task", "inputs": [{"name": "data_path",
"type": "String"}], "name": "Banking Term Deposit classifier pipeline with kuberflow"}'}
labels: {pipelines.kubeflow.org/kfp_sdk_version: 1.8.18}
spec:
entrypoint: banking-term-deposit-classifier-pipeline-with-kuberflow
templates:
- name: banking-term-deposit-classifier-pipeline-with-kuberflow
inputs:
parameters:
- {name: data_path}
dag:
tasks:
- name: get-metrics
template: get-metrics
dependencies: [predict-prob-on-test-data, t-vol]
arguments:
parameters:
- {name: data_path, value: '{{inputs.parameters.data_path}}'}
- {name: t-vol-name, value: '{{tasks.t-vol.outputs.parameters.t-vol-name}}'}
- name: load-and-clean-data
template: load-and-clean-data
dependencies: [t-vol]
arguments:
parameters:
- {name: data_path, value: '{{inputs.parameters.data_path}}'}
- {name: t-vol-name, value: '{{tasks.t-vol.outputs.parameters.t-vol-name}}'}
- name: predict-on-test-data
template: predict-on-test-data
dependencies: [t-vol, training-basic-classifier]
arguments:
parameters:
- {name: data_path, value: '{{inputs.parameters.data_path}}'}
- {name: t-vol-name, value: '{{tasks.t-vol.outputs.parameters.t-vol-name}}'}
- name: predict-prob-on-test-data
template: predict-prob-on-test-data
dependencies: [predict-on-test-data, t-vol]
arguments:
parameters:
- {name: data_path, value: '{{inputs.parameters.data_path}}'}
- {name: t-vol-name, value: '{{tasks.t-vol.outputs.parameters.t-vol-name}}'}
- name: preprocessing
template: preprocessing
dependencies: [load-and-clean-data, t-vol]
arguments:
parameters:
- {name: data_path, value: '{{inputs.parameters.data_path}}'}
- {name: t-vol-name, value: '{{tasks.t-vol.outputs.parameters.t-vol-name}}'}
- {name: t-vol, template: t-vol}
- name: train-test-split
template: train-test-split
dependencies: [preprocessing, t-vol]
arguments:
parameters:
- {name: data_path, value: '{{inputs.parameters.data_path}}'}
- {name: t-vol-name, value: '{{tasks.t-vol.outputs.parameters.t-vol-name}}'}
- name: training-basic-classifier
template: training-basic-classifier
dependencies: [t-vol, train-test-split]
arguments:
parameters:
- {name: data_path, value: '{{inputs.parameters.data_path}}'}
- {name: t-vol-name, value: '{{tasks.t-vol.outputs.parameters.t-vol-name}}'}
- name: get-metrics
container:
args: []
command:
- sh
- -c
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
'pandas==1.2.4' 'numpy==1.21.0' 'scikit-learn==0.24.2' || PIP_DISABLE_PIP_VERSION_CHECK=1
python3 -m pip install --quiet --no-warn-script-location 'pandas==1.2.4' 'numpy==1.21.0'
'scikit-learn==0.24.2' --user) && "$0" "$@"
- sh
- -ec
- |
program_path=$(mktemp)
printf "%s" "$0" > "$program_path"
python3 -u "$program_path" "$@"
- |
def get_metrics():
import mlflow
from sklearn.metrics import accuracy_score, precision_score, recall_score, log_loss
import pandas as pd
import numpy as np
from sklearn import metrics
with mlflow.start_run():
y_true = np.load(f'data/y_test.npy', allow_pickle=True)
y_pred = np.load(f'data/y_pred.npy', allow_pickle=True)
y_pred_prob = np.load(f'data/y_pred_prob.npy', allow_pickle=True)
acc = accuracy_score(y_true, y_pred)
prec = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
entropy = log_loss(y_true, y_pred_prob)
metrics_dict = {'accuracy': round(acc, 2), 'precision': round(prec, 2), 'recall': round(recall, 2), 'entropy': round(entropy, 2)}
print("\n Model Metrics:", metrics_dict)
for metric, value in metrics_dict.items():
mlflow.log_metric(metric, value)
print("\n Model Metrics:", metrics_dict)
import argparse
_parser = argparse.ArgumentParser(prog='Get metrics', description='')
_parsed_args = vars(_parser.parse_args())
_outputs = get_metrics(**_parsed_args)
image: python:3.7
volumeMounts:
- {mountPath: '{{inputs.parameters.data_path}}', name: t-vol}
inputs:
parameters:
- {name: data_path}
- {name: t-vol-name}
metadata:
labels:
pipelines.kubeflow.org/kfp_sdk_version: 1.8.18
pipelines.kubeflow.org/pipeline-sdk-type: kfp
pipelines.kubeflow.org/enable_caching: "true"
annotations: {pipelines.kubeflow.org/component_spec: '{"implementation": {"container":
{"args": [], "command": ["sh", "-c", "(PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip
install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' --user) && \"$0\" \"$@\"", "sh", "-ec", "program_path=$(mktemp)\nprintf
\"%s\" \"$0\" > \"$program_path\"\npython3 -u \"$program_path\" \"$@\"\n",
"def get_metrics():\n import mlflow\n from sklearn.metrics import
accuracy_score, precision_score, recall_score, log_loss\n import pandas
as pd\n import numpy as np\n from sklearn import metrics\n\n with
mlflow.start_run():\n y_true = np.load(f''data/y_test.npy'', allow_pickle=True)\n y_pred
= np.load(f''data/y_pred.npy'', allow_pickle=True)\n y_pred_prob
= np.load(f''data/y_pred_prob.npy'', allow_pickle=True)\n\n acc =
accuracy_score(y_true, y_pred)\n prec = precision_score(y_true, y_pred)\n recall
= recall_score(y_true, y_pred)\n entropy = log_loss(y_true, y_pred_prob)\n\n metrics_dict
= {''accuracy'': round(acc, 2), ''precision'': round(prec, 2), ''recall'':
round(recall, 2), ''entropy'': round(entropy, 2)}\n print(\"\\n Model
Metrics:\", metrics_dict)\n\n for metric, value in metrics_dict.items():\n mlflow.log_metric(metric,
value)\n\n print(\"\\n Model Metrics:\", metrics_dict)\n\nimport argparse\n_parser
= argparse.ArgumentParser(prog=''Get metrics'', description='''')\n_parsed_args
= vars(_parser.parse_args())\n\n_outputs = get_metrics(**_parsed_args)\n"],
"image": "python:3.7"}}, "name": "Get metrics"}', pipelines.kubeflow.org/component_ref: '{}',
pipelines.kubeflow.org/max_cache_staleness: P0D}
volumes:
- name: t-vol
persistentVolumeClaim: {claimName: '{{inputs.parameters.t-vol-name}}'}
- name: load-and-clean-data
container:
args: []
command:
- sh
- -c
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
'pandas==1.2.4' 'numpy==1.21.0' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location 'pandas==1.2.4' 'numpy==1.21.0'
--user) && "$0" "$@"
- sh
- -ec
- |
program_path=$(mktemp)
printf "%s" "$0" > "$program_path"
python3 -u "$program_path" "$@"
- |
def load_and_clean_data():
import pandas as pd
import numpy as np
data = pd.read_csv("https://raw.githubusercontent.com/TripathiAshutosh/dataset/main/banking.csv")
print("Null/missingalues available in the data: \n")
print(data.isna().sum())
data = data.dropna()
print("The data after dropping the na values are: \n")
print(data.isna().sum())
data.to_csv(f'data/initial_data.csv', index = False)
print("--------data imported and cleaned----------")
import argparse
_parser = argparse.ArgumentParser(prog='Load and clean data', description='')
_parsed_args = vars(_parser.parse_args())
_outputs = load_and_clean_data(**_parsed_args)
image: python:3.7
volumeMounts:
- {mountPath: '{{inputs.parameters.data_path}}', name: t-vol}
inputs:
parameters:
- {name: data_path}
- {name: t-vol-name}
metadata:
labels:
pipelines.kubeflow.org/kfp_sdk_version: 1.8.18
pipelines.kubeflow.org/pipeline-sdk-type: kfp
pipelines.kubeflow.org/enable_caching: "true"
annotations: {pipelines.kubeflow.org/component_spec: '{"implementation": {"container":
{"args": [], "command": ["sh", "-c", "(PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
|| PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
''pandas==1.2.4'' ''numpy==1.21.0'' --user) && \"$0\" \"$@\"", "sh", "-ec",
"program_path=$(mktemp)\nprintf \"%s\" \"$0\" > \"$program_path\"\npython3
-u \"$program_path\" \"$@\"\n", "def load_and_clean_data():\n\n import
pandas as pd\n import numpy as np\n\n data = pd.read_csv(\"https://raw.githubusercontent.com/TripathiAshutosh/dataset/main/banking.csv\")\n\n print(\"Null/missingalues
available in the data: \\n\")\n print(data.isna().sum())\n data =
data.dropna()\n print(\"The data after dropping the na values are: \\n\")\n print(data.isna().sum())\n\n data.to_csv(f''data/initial_data.csv'',
index = False)\n print(\"--------data imported and cleaned----------\")\n\nimport
argparse\n_parser = argparse.ArgumentParser(prog=''Load and clean data'',
description='''')\n_parsed_args = vars(_parser.parse_args())\n\n_outputs
= load_and_clean_data(**_parsed_args)\n"], "image": "python:3.7"}}, "name":
"Load and clean data"}', pipelines.kubeflow.org/component_ref: '{}', pipelines.kubeflow.org/max_cache_staleness: P0D}
volumes:
- name: t-vol
persistentVolumeClaim: {claimName: '{{inputs.parameters.t-vol-name}}'}
- name: predict-on-test-data
container:
args: []
command:
- sh
- -c
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
'pandas==1.2.4' 'numpy==1.21.0' 'scikit-learn==0.24.2' || PIP_DISABLE_PIP_VERSION_CHECK=1
python3 -m pip install --quiet --no-warn-script-location 'pandas==1.2.4' 'numpy==1.21.0'
'scikit-learn==0.24.2' --user) && "$0" "$@"
- sh
- -ec
- |
program_path=$(mktemp)
printf "%s" "$0" > "$program_path"
python3 -u "$program_path" "$@"
- |
def predict_on_test_data():
import mlflow
import pandas as pd
import numpy as np
import sklearn
with mlflow.start_run():
mlflow.sklearn.load_model(f'data/model.pkl')
X_test = np.load(f'data/X_test.npy', allow_pickle=True)
y_pred = model.predict(X_test)
np.save(f'data/y_pred.npy', y_pred)
mlflow.log_artifact(f'data/y_pred.npy', "predictions")
print("\nPredicted classes ----")
print("\n")
print(y_pred)
import argparse
_parser = argparse.ArgumentParser(prog='Predict on test data', description='')
_parsed_args = vars(_parser.parse_args())
_outputs = predict_on_test_data(**_parsed_args)
image: python:3.7
volumeMounts:
- {mountPath: '{{inputs.parameters.data_path}}', name: t-vol}
inputs:
parameters:
- {name: data_path}
- {name: t-vol-name}
metadata:
labels:
pipelines.kubeflow.org/kfp_sdk_version: 1.8.18
pipelines.kubeflow.org/pipeline-sdk-type: kfp
pipelines.kubeflow.org/enable_caching: "true"
annotations: {pipelines.kubeflow.org/component_spec: '{"implementation": {"container":
{"args": [], "command": ["sh", "-c", "(PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip
install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' --user) && \"$0\" \"$@\"", "sh", "-ec", "program_path=$(mktemp)\nprintf
\"%s\" \"$0\" > \"$program_path\"\npython3 -u \"$program_path\" \"$@\"\n",
"def predict_on_test_data():\n import mlflow\n import pandas as pd\n import
numpy as np\n import sklearn\n\n with mlflow.start_run():\n mlflow.sklearn.load_model(f''data/model.pkl'')\n\n X_test
= np.load(f''data/X_test.npy'', allow_pickle=True)\n y_pred = model.predict(X_test)\n np.save(f''data/y_pred.npy'',
y_pred)\n\n mlflow.log_artifact(f''data/y_pred.npy'', \"predictions\")\n\n print(\"\\nPredicted
classes ----\")\n print(\"\\n\")\n print(y_pred)\n\nimport argparse\n_parser
= argparse.ArgumentParser(prog=''Predict on test data'', description='''')\n_parsed_args
= vars(_parser.parse_args())\n\n_outputs = predict_on_test_data(**_parsed_args)\n"],
"image": "python:3.7"}}, "name": "Predict on test data"}', pipelines.kubeflow.org/component_ref: '{}',
pipelines.kubeflow.org/max_cache_staleness: P0D}
volumes:
- name: t-vol
persistentVolumeClaim: {claimName: '{{inputs.parameters.t-vol-name}}'}
- name: predict-prob-on-test-data
container:
args: []
command:
- sh
- -c
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
'pandas==1.2.4' 'numpy==1.21.0' 'scikit-learn==0.24.2' || PIP_DISABLE_PIP_VERSION_CHECK=1
python3 -m pip install --quiet --no-warn-script-location 'pandas==1.2.4' 'numpy==1.21.0'
'scikit-learn==0.24.2' --user) && "$0" "$@"
- sh
- -ec
- |
program_path=$(mktemp)
printf "%s" "$0" > "$program_path"
python3 -u "$program_path" "$@"
- |
def predict_prob_on_test_data():
import mlflow
import pandas as pd
import numpy as np
import sklearn
with mlflow.start_run():
model = mlflow.sklearn.load_model(f'data/model.pkl')
X_test = np.load(f'data/X_test.npy', allow_pickle=True)
y_pred_prob = model.predict_proba(X_test)
np.save(f'data/y_pred_prob.npy', y_pred_prob)
mlflow.log_artifact(f'data/y_pred_prob.npy', "predicted_probabilities")
print("\nPredicted Probabilities ----")
print("\n")
print(y_pred_prob)
import argparse
_parser = argparse.ArgumentParser(prog='Predict prob on test data', description='')
_parsed_args = vars(_parser.parse_args())
_outputs = predict_prob_on_test_data(**_parsed_args)
image: python:3.7
volumeMounts:
- {mountPath: '{{inputs.parameters.data_path}}', name: t-vol}
inputs:
parameters:
- {name: data_path}
- {name: t-vol-name}
metadata:
labels:
pipelines.kubeflow.org/kfp_sdk_version: 1.8.18
pipelines.kubeflow.org/pipeline-sdk-type: kfp
pipelines.kubeflow.org/enable_caching: "true"
annotations: {pipelines.kubeflow.org/component_spec: '{"implementation": {"container":
{"args": [], "command": ["sh", "-c", "(PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip
install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' --user) && \"$0\" \"$@\"", "sh", "-ec", "program_path=$(mktemp)\nprintf
\"%s\" \"$0\" > \"$program_path\"\npython3 -u \"$program_path\" \"$@\"\n",
"def predict_prob_on_test_data():\n import mlflow\n import pandas
as pd\n import numpy as np\n import sklearn\n\n with mlflow.start_run():\n model
= mlflow.sklearn.load_model(f''data/model.pkl'')\n\n X_test = np.load(f''data/X_test.npy'',
allow_pickle=True)\n y_pred_prob = model.predict_proba(X_test)\n np.save(f''data/y_pred_prob.npy'',
y_pred_prob)\n\n mlflow.log_artifact(f''data/y_pred_prob.npy'', \"predicted_probabilities\")\n\n print(\"\\nPredicted
Probabilities ----\")\n print(\"\\n\")\n print(y_pred_prob)\n\nimport
argparse\n_parser = argparse.ArgumentParser(prog=''Predict prob on test
data'', description='''')\n_parsed_args = vars(_parser.parse_args())\n\n_outputs
= predict_prob_on_test_data(**_parsed_args)\n"], "image": "python:3.7"}},
"name": "Predict prob on test data"}', pipelines.kubeflow.org/component_ref: '{}',
pipelines.kubeflow.org/max_cache_staleness: P0D}
volumes:
- name: t-vol
persistentVolumeClaim: {claimName: '{{inputs.parameters.t-vol-name}}'}
- name: preprocessing
container:
args: []
command:
- sh
- -c
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
'pandas==1.2.4' 'numpy==1.21.0' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location 'pandas==1.2.4' 'numpy==1.21.0'
--user) && "$0" "$@"
- sh
- -ec
- |
program_path=$(mktemp)
printf "%s" "$0" > "$program_path"
python3 -u "$program_path" "$@"
- |
def preprocessing():
import pandas as pd
import numpy as np
data = pd.read_csv(f'data/initial_data.csv')
data['education'] = np.where(data['education'] == 'basic.9y', 'Basic', data['education'])
data['education'] = np.where(data['education'] == 'basic.6y', 'Basic', data['education'])
data['education'] = np.where(data['education'] == 'basic.4y', 'Basic', data['education'])
categorical_vars = ['job','marital','education', 'default', 'housing', 'loan', 'contact', 'month', 'day_of_week', 'poutcome']
for var in categorical_vars:
cat_list = 'var' + '_' + var
cat_list = pd.get_dummies(data[var], prefix = var) # one hot encoding
data_new = data.join(cat_list)
data = data_new
categorical_vars = ['job','marital','education', 'default', 'housing', 'loan', 'contact', 'month', 'day_of_week', 'poutcome']
data_vars = data.columns.values.tolist()
keeping = [i for i in data_vars if i not in categorical_vars]
final_df = data[keeping]
final_df.columns = final_df.columns.str.replace(".", "_")
final_df.columns = final_df.columns.str.replace(" ", "_")
print(final_df.head())
final_df.to_csv(f'data/preprocessed_df.csv', index = False)
print("Education column pre-processed, categorical variables one-hot encoded. Ready to input data to model")
import argparse
_parser = argparse.ArgumentParser(prog='Preprocessing', description='')
_parsed_args = vars(_parser.parse_args())
_outputs = preprocessing(**_parsed_args)
image: python:3.7
volumeMounts:
- {mountPath: '{{inputs.parameters.data_path}}', name: t-vol}
inputs:
parameters:
- {name: data_path}
- {name: t-vol-name}
metadata:
labels:
pipelines.kubeflow.org/kfp_sdk_version: 1.8.18
pipelines.kubeflow.org/pipeline-sdk-type: kfp
pipelines.kubeflow.org/enable_caching: "true"
annotations: {pipelines.kubeflow.org/component_spec: '{"implementation": {"container":
{"args": [], "command": ["sh", "-c", "(PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
|| PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
''pandas==1.2.4'' ''numpy==1.21.0'' --user) && \"$0\" \"$@\"", "sh", "-ec",
"program_path=$(mktemp)\nprintf \"%s\" \"$0\" > \"$program_path\"\npython3
-u \"$program_path\" \"$@\"\n", "def preprocessing():\n\n import pandas
as pd\n import numpy as np\n\n data = pd.read_csv(f''data/initial_data.csv'')\n\n data[''education'']
= np.where(data[''education''] == ''basic.9y'', ''Basic'', data[''education''])\n data[''education'']
= np.where(data[''education''] == ''basic.6y'', ''Basic'', data[''education''])\n data[''education'']
= np.where(data[''education''] == ''basic.4y'', ''Basic'', data[''education''])\n\n categorical_vars
= [''job'',''marital'',''education'', ''default'', ''housing'', ''loan'',
''contact'', ''month'', ''day_of_week'', ''poutcome'']\n for var in categorical_vars:\n cat_list
= ''var'' + ''_'' + var\n cat_list = pd.get_dummies(data[var], prefix
= var) # one hot encoding\n data_new = data.join(cat_list)\n data
= data_new\n\n categorical_vars = [''job'',''marital'',''education'',
''default'', ''housing'', ''loan'', ''contact'', ''month'', ''day_of_week'',
''poutcome'']\n\n data_vars = data.columns.values.tolist()\n\n keeping
= [i for i in data_vars if i not in categorical_vars]\n\n final_df =
data[keeping]\n\n final_df.columns = final_df.columns.str.replace(\".\",
\"_\")\n final_df.columns = final_df.columns.str.replace(\" \", \"_\")\n\n print(final_df.head())\n\n final_df.to_csv(f''data/preprocessed_df.csv'',
index = False)\n print(\"Education column pre-processed, categorical
variables one-hot encoded. Ready to input data to model\")\n\nimport argparse\n_parser
= argparse.ArgumentParser(prog=''Preprocessing'', description='''')\n_parsed_args
= vars(_parser.parse_args())\n\n_outputs = preprocessing(**_parsed_args)\n"],
"image": "python:3.7"}}, "name": "Preprocessing"}', pipelines.kubeflow.org/component_ref: '{}',
pipelines.kubeflow.org/max_cache_staleness: P0D}
volumes:
- name: t-vol
persistentVolumeClaim: {claimName: '{{inputs.parameters.t-vol-name}}'}
- name: t-vol
resource:
action: create
manifest: |
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: '{{workflow.name}}-t-vol'
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
outputs:
parameters:
- name: t-vol-manifest
valueFrom: {jsonPath: '{}'}
- name: t-vol-name
valueFrom: {jsonPath: '{.metadata.name}'}
- name: t-vol-size
valueFrom: {jsonPath: '{.status.capacity.storage}'}
metadata:
labels:
pipelines.kubeflow.org/kfp_sdk_version: 1.8.18
pipelines.kubeflow.org/pipeline-sdk-type: kfp
pipelines.kubeflow.org/enable_caching: "true"
- name: train-test-split
container:
args: []
command:
- sh
- -c
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
'pandas==1.2.4' 'numpy==1.21.0' 'scikit-learn==0.24.2' || PIP_DISABLE_PIP_VERSION_CHECK=1
python3 -m pip install --quiet --no-warn-script-location 'pandas==1.2.4' 'numpy==1.21.0'
'scikit-learn==0.24.2' --user) && "$0" "$@"
- sh
- -ec
- |
program_path=$(mktemp)
printf "%s" "$0" > "$program_path"
python3 -u "$program_path" "$@"
- |
def train_test_split():
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
final_df = pd.read_csv(f'data/preprocessed_df.csv')
X = final_df.loc[:, final_df.columns != 'y']
y = final_df.loc[:, final_df.columns == 'y']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, stratify = y, random_state = 47)
np.save(f'data/X_train.npy', X_train) # saved as a numpy binary file (efficient to save and load)
np.save(f'data/X_test.npy', X_test)
np.save(f'data/y_train.npy', y_train)
np.save(f'data/y_test.npy', y_test)
print("\n---- X_train ----")
print("\n")
print(X_train.head())
print("\n---- X_test ----")
print("\n")
print(X_test.head())
print("\n---- y_test ----")
print("\n")
print(y_test.head())
import argparse
_parser = argparse.ArgumentParser(prog='Train test split', description='')
_parsed_args = vars(_parser.parse_args())
_outputs = train_test_split(**_parsed_args)
image: python:3.7
volumeMounts:
- {mountPath: '{{inputs.parameters.data_path}}', name: t-vol}
inputs:
parameters:
- {name: data_path}
- {name: t-vol-name}
metadata:
labels:
pipelines.kubeflow.org/kfp_sdk_version: 1.8.18
pipelines.kubeflow.org/pipeline-sdk-type: kfp
pipelines.kubeflow.org/enable_caching: "true"
annotations: {pipelines.kubeflow.org/component_spec: '{"implementation": {"container":
{"args": [], "command": ["sh", "-c", "(PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip
install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' --user) && \"$0\" \"$@\"", "sh", "-ec", "program_path=$(mktemp)\nprintf
\"%s\" \"$0\" > \"$program_path\"\npython3 -u \"$program_path\" \"$@\"\n",
"def train_test_split():\n import pandas as pd\n import numpy as np\n from
sklearn.model_selection import train_test_split\n\n final_df = pd.read_csv(f''data/preprocessed_df.csv'')\n\n X
= final_df.loc[:, final_df.columns != ''y'']\n y = final_df.loc[:, final_df.columns
== ''y'']\n\n X_train, X_test, y_train, y_test = train_test_split(X,
y, test_size = 0.3, stratify = y, random_state = 47)\n\n np.save(f''data/X_train.npy'',
X_train) # saved as a numpy binary file (efficient to save and load)\n np.save(f''data/X_test.npy'',
X_test)\n np.save(f''data/y_train.npy'', y_train)\n np.save(f''data/y_test.npy'',
y_test)\n\n print(\"\\n---- X_train ----\")\n print(\"\\n\")\n print(X_train.head())\n\n print(\"\\n----
X_test ----\")\n print(\"\\n\")\n print(X_test.head())\n\n print(\"\\n----
y_test ----\")\n print(\"\\n\")\n print(y_test.head())\n\nimport argparse\n_parser
= argparse.ArgumentParser(prog=''Train test split'', description='''')\n_parsed_args
= vars(_parser.parse_args())\n\n_outputs = train_test_split(**_parsed_args)\n"],
"image": "python:3.7"}}, "name": "Train test split"}', pipelines.kubeflow.org/component_ref: '{}',
pipelines.kubeflow.org/max_cache_staleness: P0D}
volumes:
- name: t-vol
persistentVolumeClaim: {claimName: '{{inputs.parameters.t-vol-name}}'}
- name: training-basic-classifier
container:
args: []
command:
- sh
- -c
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
'pandas==1.2.4' 'numpy==1.21.0' 'scikit-learn==0.24.2' || PIP_DISABLE_PIP_VERSION_CHECK=1
python3 -m pip install --quiet --no-warn-script-location 'pandas==1.2.4' 'numpy==1.21.0'
'scikit-learn==0.24.2' --user) && "$0" "$@"
- sh
- -ec
- |
program_path=$(mktemp)
printf "%s" "$0" > "$program_path"
python3 -u "$program_path" "$@"
- |
def training_basic_classifier():
import mlflow
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
X_train = np.load(f'data/X_train.npy', allow_pickle=True)
y_train = np.load(f'data/y_train.npy', allow_pickle=True)
model = RandomForestClassifier(n_estimators=150)
model.fit(X_train, y_train)
with mlflow.start_run():
mlflow.log_param("n_estimators", 150)
# Log any other hyperparameters you want to track
mlflow.sklearn.log_model(model, "model")
with open(f'data/model.pkl', 'wb') as f:
pickle.dump(model, f)
print("\nRandomForest classifier is trained on banking data and saved to PV location /data/model.pkl ----")
import argparse
_parser = argparse.ArgumentParser(prog='Training basic classifier', description='')
_parsed_args = vars(_parser.parse_args())
_outputs = training_basic_classifier(**_parsed_args)
image: python:3.7
volumeMounts:
- {mountPath: '{{inputs.parameters.data_path}}', name: t-vol}
inputs:
parameters:
- {name: data_path}
- {name: t-vol-name}
metadata:
labels:
pipelines.kubeflow.org/kfp_sdk_version: 1.8.18
pipelines.kubeflow.org/pipeline-sdk-type: kfp
pipelines.kubeflow.org/enable_caching: "true"
annotations: {pipelines.kubeflow.org/component_spec: '{"implementation": {"container":
{"args": [], "command": ["sh", "-c", "(PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip
install --quiet --no-warn-script-location ''pandas==1.2.4'' ''numpy==1.21.0''
''scikit-learn==0.24.2'' --user) && \"$0\" \"$@\"", "sh", "-ec", "program_path=$(mktemp)\nprintf
\"%s\" \"$0\" > \"$program_path\"\npython3 -u \"$program_path\" \"$@\"\n",
"def training_basic_classifier():\n import mlflow\n from sklearn.ensemble
import RandomForestClassifier\n import pandas as pd\n import numpy
as np\n\n X_train = np.load(f''data/X_train.npy'', allow_pickle=True)\n y_train
= np.load(f''data/y_train.npy'', allow_pickle=True)\n\n model = RandomForestClassifier(n_estimators=150)\n model.fit(X_train,
y_train)\n\n with mlflow.start_run():\n mlflow.log_param(\"n_estimators\",
150)\n # Log any other hyperparameters you want to track\n\n mlflow.sklearn.log_model(model,
\"model\")\n\n with open(f''data/model.pkl'', ''wb'') as f:\n pickle.dump(model,
f)\n\n print(\"\\nRandomForest classifier is trained on banking data
and saved to PV location /data/model.pkl ----\")\n\nimport argparse\n_parser
= argparse.ArgumentParser(prog=''Training basic classifier'', description='''')\n_parsed_args
= vars(_parser.parse_args())\n\n_outputs = training_basic_classifier(**_parsed_args)\n"],
"image": "python:3.7"}}, "name": "Training basic classifier"}', pipelines.kubeflow.org/component_ref: '{}',
pipelines.kubeflow.org/max_cache_staleness: P0D}
volumes:
- name: t-vol
persistentVolumeClaim: {claimName: '{{inputs.parameters.t-vol-name}}'}
arguments:
parameters:
- {name: data_path}
serviceAccountName: pipeline-runner