-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.py
executable file
·80 lines (53 loc) · 2.43 KB
/
helper_functions.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created on Wed Apr 1 19:54:56 2020
# Copyright © Enrico Gandini <[email protected]>
#
# Distributed under terms of the MIT License.
"""
"""
from typing import Dict
from typing import List
from itertools import product
import pandas as pd
from sklearn.base import clone
def various_metrics_binary_classification(model,
metrics: List,
X: pd.DataFrame,
y_true: pd.Series,
identifier: str = None) -> Dict:
if not (isinstance(metrics, List) and any(metrics)):
raise ValueError("First argument should be a non-empty list!")
pred_proba = model.predict_proba(X)[:, 1]
results = {}
for metric in metrics:
result = metric(y_true, pred_proba)
label_metric = metric.__name__ if not identifier else f"{metric.__name__}_{identifier}"
results[label_metric] = result
return results
def add_identifier_to_names_pipelines_dict(pipelines: Dict, identifier: str=None):
if not (isinstance(pipelines, Dict) and any(pipelines)):
raise ValueError("First argument should be a non-empty dictionary!")
old_names = list(pipelines.keys()) #(!)otherwise, each key will be changed twice!
for old_name in old_names:
new_name = f"{old_name}_{identifier}"
pipelines[new_name] = pipelines.pop(old_name) #inplace!
return None
def insert_steps_to_pipelines_dict(steps: List,
pipelines: Dict,
identifier: str = None,
):
if not (isinstance(steps, List) and any(steps)):
raise ValueError("First argument should be a non-empty list!")
elif not (isinstance(pipelines, Dict) and any(pipelines)):
raise ValueError("Second argument should be a non-empty dictionary!")
new_pipelines = {name: clone(pipeline)
for name, pipeline in pipelines.items()}
reverse_steps = steps.copy()
reverse_steps.reverse() #remember, list.reverse() is inplace!
for step, new_pipeline in product(reverse_steps, new_pipelines.values()):
new_pipeline.steps.insert(0, step)
if identifier:
add_identifier_to_names_pipelines_dict(new_pipelines, identifier) #this step is inplace
return new_pipelines