-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextratrees_inference.py
138 lines (108 loc) · 4.4 KB
/
extratrees_inference.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
"""Inference on Higgs ML Data with random forest"""
import os
import argparse
import pickle
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from sklearn.ensemble import ExtraTreesClassifier
import numpy as np
from hyperopt import space_eval
import pandas as pd
from sklearn.impute import SimpleImputer
from extratrees_hyperopt import PARAMS_SPACE
from utils import *
from plot_utils import *
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, help='random generators seed (default: None)')
parser.add_argument('--logdir', type=str, default='./', help='save directory')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
if args.seed is not None:
# random seed for reproducibility
np.random.seed(42)
df_train, df_lead, df_test, _, = get_cern_datasets()
# prepare datasets
imputer = SimpleImputer(strategy='median')
train_data, train_target, train_weights = prepare_cern_dataset(df_train, True, imputer)
test_data, test_target, test_weights = prepare_cern_dataset(df_test, False, imputer)
lead_data, lead_target, lead_weights = prepare_cern_dataset(df_lead, False, imputer)
# load Trials object from hyperopt search in log dir
with open(os.path.join(args.logdir, 'Trials-extratrees.pkl'), 'rb') as file:
trials = pickle.load(file)
all_ams = [-ams for ams in trials.losses()]
# extract the best Trial among all
bidx = np.argmax(all_ams)
bresult = trials.results[bidx]
ams, ams_var = -bresult['loss'], bresult['loss_variance'],
threshold, threshold_var = bresult['threshold'], bresult['threshold_variance']
print('Hyperopt best results:\n'
'\tMean AMS: {:.6f}\n'
'\tVar AMS: {:.6f}\n'
'\tMean threshold: {:.6f}\n'
'\tVar threshold: {:.6f}\n'
'\tTrial: {}\n'
.format(ams, ams_var, threshold, threshold_var, bidx))
#pprint(trials.trials)
# # plot ams with trials
# fig, ax = plt.subplots(figsize=(9,6))
# plot_ams_with_trials(trials, ax=ax)
# plt.savefig('figures/extratrees/AMS Score with Trials.svg')
# plt.show()
# # plot ams curves for each fold
# fig, ax = plt.subplots(figsize=(9,6))
# plot_cv_ams_curves_for_trial(bresult['cv_ams_curves'], ax=ax)
# plt.savefig('figures/extratrees/CV AMS Curves.svg')
# plt.show()
bparams = trials.argmin
bparams = space_eval(PARAMS_SPACE, bparams)
# retrain the model with best set of parameters on full training set
print('Training on train set...')
clf = ExtraTreesClassifier(**bparams)
clf.fit(train_data, train_target)
print('Done\n')
# predictions with best threshold
print("Scores with best threshold in CV:")
train_preds = clf.predict_proba(train_data)[:, 1]
train_ams = ams_score(train_target, round_predictions(train_preds, threshold),
train_weights)
print(f'Train AMS: {train_ams:.6f}')
test_preds = clf.predict_proba(test_data)[:, 1]
test_ams = ams_score(test_target, round_predictions(test_preds, threshold),
test_weights)
print(f'Test AMS: {test_ams:.6f}')
lead_preds = clf.predict_proba(lead_data)[:, 1]
lead_ams = ams_score(lead_target, round_predictions(lead_preds, threshold),
lead_weights)
print(f'Leaderboard AMS: {lead_ams:.6f}')
## WITH MAX_DEPTH UP TO 25 ##
# Hyperopt best results:
# Mean AMS: 3.733631
# Var AMS: 0.141557
# Mean threshold: 0.871343
# Var threshold: 0.002815
# Trial: 60
# RESULTS, WITH BEST CV THRESHOLD
# Train AMS: 8.474507
# Test AMS: 3.477927
# Leaderboard AMS: 3.401259
# * Real difference between mean CV AMS of 3.73 and test AMS of 3.47: too much
# randomization and variance in extra trees?
# * Very high score in train (8.47) way above 5: big overfitting caused by too much
# depth in the trees?
## WITH MAX_DEPTH UP TO 15 ##
# Hyperopt best results:
# Mean AMS: 3.478727
# Var AMS: 0.013807
# Mean threshold: 0.753908
# Var threshold: 0.003377
# Trial: 116
# RESULTS, WITH BEST CV THRESHOLD
# Scores with best threshold in CV:
# Train AMS: 3.647394
# Test AMS: 3.345966
# Leaderboard AMS: 3.314367
# * Same problem as 1. with depth 25, difference between CV and test AMS
# * No noticeable overfitting though, but lower test scores compared to depth 25.