forked from eyra/fertility-prediction-challenge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
training.py
171 lines (133 loc) · 6.49 KB
/
training.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
import joblib
import numpy as np
import pandas as pd
from pathlib import Path
from tqdm import tqdm
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.model_selection import StratifiedKFold, StratifiedShuffleSplit
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.ensemble import ExtraTreesClassifier
from catboost import CatBoostClassifier
from lightgbm import LGBMClassifier
from submission import clean_df
def train_save_model(cleaned_df: pd.DataFrame, outcome_df: pd.DataFrame, evaluate: bool=False, tune: bool=False)-> None:
"""
Train and tune a CatBoostClassifier model on the baseline + background features and save it.
Args:
cleaned_df (pd.DataFrame): Cleaned data dataframe.
outcome_df (pd.DataFrame): Dataframe with the outcome variable.
model_name (str, optional): Name of the model. Defaults to 'baseline'.
Returns:
None
"""
# prepare input data
model_df = pd.merge(cleaned_df, outcome_df, on="nomem_encr")
# prepare input data
model_df = pd.merge(cleaned_df, outcome_df, on="nomem_encr")
# define the preprocessing pipeline
features = [c for c in model_df.columns if c not in ['nomem_encr', 'new_child']]
cat_features = [col for col in features if col.endswith('_ds')]
num_features = [col for col in features if not col.endswith('_ds')]
for c in cat_features:
model_df[c] = model_df[c].astype('category')
numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())])
categorical_transformer = Pipeline(steps=[('onehot', OneHotEncoder(handle_unknown='ignore'))])
preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, num_features),
('cat', categorical_transformer, cat_features)])
params_lgb = {
'bagging_fraction': 0.8,
'feature_fraction': 0.9,
'learning_rate': 0.1,
'max_bin': 20,
'max_depth': 30,
'min_data_in_leaf': 20,
'min_sum_hessian_in_leaf': 0.001,
'n_estimators': 3246,
'num_leaves': 24,
'subsample': 1.0
}
models = {
'cb': CatBoostClassifier(cat_features=cat_features, verbose=False, random_state=42),
'et': Pipeline(steps=[('preprocessor', preprocessor), ('clf', ExtraTreesClassifier(n_estimators=500, max_features=0.3, class_weight='balanced', random_state=42))]),
'lgb': LGBMClassifier(boosting_type = 'gbdt', random_state=42, verbose=-1, class_weight = 'balanced', **params_lgb)
}
# fit the model
models['cb'].fit(model_df[features], model_df['new_child'])
models['et'].fit(model_df[features], model_df['new_child'])
models['lgb'].fit(model_df[features], model_df['new_child'], categorical_feature=cat_features)
# save the model and params
joblib.dump(models, f"model.joblib")
if evaluate == True:
print('\nPerforming cross validation...')
# cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
# cv = RepeatedStratifiedKFold(n_splits=2, n_repeats=10, random_state=42)
cv = StratifiedShuffleSplit(n_splits=10, test_size=0.3, random_state=1927)
cv_results = []
for train_index, test_index in tqdm(cv.split(model_df[features], model_df['new_child'])):
X_train, X_test = model_df.iloc[train_index][features], model_df.iloc[test_index][features]
y_train, y_test = model_df.iloc[train_index]['new_child'], model_df.iloc[test_index]['new_child']
# fit models
models['cb'].fit(X_train, y_train)
models['et'].fit(X_train, y_train)
models['lgb'].fit(X_train, y_train, categorical_feature=cat_features)
# predictions
cb_preds = models['cb'].predict_proba(X_test)[:, 1]
et_preds = models['et'].predict_proba(X_test)[:, 1]
lgb_preds = models['lgb'].predict_proba(X_test)[:, 1]
# average prediction for class 1
final_preds = cb_preds*0.5 + et_preds*0.25 + lgb_preds*0.25
# metrics
acc = accuracy_score(y_test, final_preds.round())
prec = precision_score(y_test, final_preds.round())
rec = recall_score(y_test, final_preds.round())
f1 = f1_score(y_test, final_preds.round())
cv_results.append([acc, prec, rec, f1])
# extract metrics from cv_results
accuracy_scores = [result[0] for result in cv_results]
precision_scores = [result[1] for result in cv_results]
recall_scores = [result[2] for result in cv_results]
f1_scores = [result[3] for result in cv_results]
accuracy = np.mean(accuracy_scores)
precision = np.mean(precision_scores)
recall = np.mean(recall_scores)
f1 = np.mean(f1_scores)
results_df = pd.DataFrame({
'model_name': ['prefer'],
'accuracy': [accuracy],
'precision': [precision],
'recall': [recall],
'f1_score': [f1]})
# save results
# results_df.to_csv('metrics.csv', index=False)
# print cv metrics
print('CV metrics:')
print(f"\taccuracy: {accuracy:.4f} ({np.std(accuracy_scores):.4f})")
print(f"\tprecision: {precision:.4f} ({np.std(precision_scores):.4f})")
print(f"\trecall: {recall:.4f} ({np.std(recall_scores):.4f})")
print(f"\tf1 score: {f1:.4f} ({np.std(f1_scores):.4f})")
if __name__ == '__main__':
proj_dir = Path().cwd()
parent_proj_dir = proj_dir.parent
data_dir = parent_proj_dir / 'prefer_data'
evaluate = True
# import data
print('Loading data...')
chunk_list = []
for chunk in tqdm(pd.read_csv(data_dir / 'training_data/PreFer_train_data.csv', chunksize=1000, low_memory=False)):
chunk_list.append(chunk)
raw_df = pd.concat(chunk_list, axis=0)
outcome_df = pd.read_csv(data_dir / 'training_data/PreFer_train_outcome.csv', low_memory=False)
background_df = pd.read_csv(data_dir / 'other_data/PreFer_train_background_data.csv', low_memory=False)
# clean data
print('Cleaning data...')
cleaned_df = clean_df(df=raw_df, background_df=background_df)
# train and save model
print(f'Training model...')
train_save_model(cleaned_df=cleaned_df, outcome_df=outcome_df, evaluate=evaluate)