-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
183 lines (152 loc) · 5.55 KB
/
train.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
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
import click, logging, gzip
import pandas as pd
from joblib import dump
from features.utils import train_test_split_per_column
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn import metrics
from uuid import uuid4
from datetime import datetime
from constants import (
EXCL_FEATURES,
TARGET,
USER_COL,
FEATURE_STEPS,
PREP_STEPS,
)
from typing import Dict, Tuple, Union
from pathlib import Path
# set up logger
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger(__name__)
def get_training_pipeline(class_weight: Dict[int, int] = "balanced") -> Pipeline:
model_steps = [
(
"random_forest_clf",
RandomForestClassifier(
random_state=123, n_estimators=2000, n_jobs=-1, class_weight=class_weight
),
)
]
return Pipeline(PREP_STEPS + FEATURE_STEPS + model_steps)
def evaluate_model_cv(
clf: Union[RandomForestClassifier],
X_train: pd.DataFrame,
y_train: pd.DataFrame,
num_folds: int = 5,
):
scv = StratifiedKFold(n_splits=num_folds)
metric_names = ["roc_auc", "f1", "precision", "recall"]
scores_df = pd.DataFrame(index=metric_names, columns=["Stratified-CV"])
for metric in metric_names:
logging.info(f"Starting CV for metric: {metric}")
score = cross_val_score(clf, X_train, y_train, scoring=metric, cv=scv).mean()
scores_df.loc[metric] = [score]
print(scores_df)
def evaluate_model(
clf: Union[RandomForestClassifier], X_test: pd.DataFrame, y_test: pd.DataFrame
):
logging.info(f"Evaluating model on test...")
# predict on new data
predictions = clf.predict(X_test)
# predict proba
y_pred_proba = clf.predict_proba(X_test)[:, 1]
# calculate metrics
accuracy = round(metrics.accuracy_score(y_test, predictions), 4)
fpr, tpr, _ = metrics.roc_curve(y_test, y_pred_proba)
auc = round(metrics.roc_auc_score(y_test, y_pred_proba), 4)
precision, recall, _ = metrics.precision_recall_curve(y_test, y_pred_proba)
pr_auc = round(metrics.auc(recall, precision), 4)
f1_score = round(metrics.f1_score(y_test, predictions, average="binary"), 4)
precision_score = round(
metrics.precision_score(y_test, predictions, average="binary"), 4
)
recall_score = round(metrics.recall_score(y_test, predictions, average="binary"), 4)
score_df = pd.DataFrame(
{
"accuracy": accuracy,
"auc": auc,
"pr_auc": pr_auc,
"precision": precision_score,
"recall": recall_score,
"f1_score": f1_score,
},
index=[0],
)
print(score_df)
def get_training_data(
dataset: pd.DataFrame,
) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]:
df_train, df_test = train_test_split_per_column(dataset, col=USER_COL)
X_train, X_test = df_train.drop([TARGET, USER_COL], axis=1), df_test.drop(
[TARGET, USER_COL], axis=1
)
y_train, y_test = df_train[TARGET], df_test[TARGET]
return X_train, y_train, X_test, y_test
@click.command()
@click.option(
"--train-data-path",
default="data/dataset.csv",
show_default=True,
help="Path to training data",
type=str,
)
@click.option(
"--save-model-path",
default="./model/",
show_default=True,
help="Path where to save model",
type=str,
)
@click.option(
"--cross-validate",
is_flag=True,
show_default=True,
default=False,
help="To evaluate model using cross-validation",
)
def main(train_data_path: str, save_model_path: str, cross_validate: bool) -> None:
"""Main script for running model training"""
logging.info(f"Loading data from {str(Path(train_data_path))}")
if Path(train_data_path).exists():
dataset = pd.read_csv(Path(train_data_path), sep=";")
else:
raise FileExistsError(f"No file found at this path: {train_data_path}...")
logging.info(f"Inspect data...")
print(f"Number of rows {len(dataset)}")
print(f"Number of uuid {dataset['uuid'].nunique()}")
if len(EXCL_FEATURES) > 0:
dataset = dataset.drop(EXCL_FEATURES, axis=1)
# exclude prediction data
pred_dataset = dataset[dataset[TARGET].isna()]
train_dataset = dataset[~dataset[TARGET].isna()]
logging.info("Get training data...")
X_train, y_train, X_test, y_test = get_training_data(train_dataset)
model_pipeline = get_training_pipeline()
logging.info(f"Start training...")
model = model_pipeline.fit(X_train, y_train)
logging.info("Evaluate model...")
if cross_validate:
evaluate_model_cv(model, X_train, y_train, num_folds=5)
else:
evaluate_model(model, X_test, y_test)
logging.info(f"Make prediction on new data...")
input = pred_dataset.drop([TARGET, USER_COL], axis=1)
pred_dataset = pred_dataset[[USER_COL, TARGET]]
pred_dataset[TARGET] = model.predict_proba(input)[:, 1] # # only positive class
logging.info(f"Saving predictions...")
Path(save_model_path).mkdir(parents=True, exist_ok=True)
pred_dataset.to_csv(Path(save_model_path) / "predictions.csv")
version = str(uuid4())
logging.info(f"Save model with version {version}...")
model.version = version
model.timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
dump(model, gzip.open(Path(save_model_path) / "model.joblib.gz", "wb"))
logging.info("Done!✅")
if __name__ == "__main__":
main()