-
Notifications
You must be signed in to change notification settings - Fork 0
/
porto-seguro-safe-driver-prediction
228 lines (194 loc) · 8.14 KB
/
porto-seguro-safe-driver-prediction
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
import pandas as pd
import numpy as np
from sklearn.preprocessing import Imputer
from sklearn.model_selection import KFold
import xgboost as xgb
import time
start =time.clock()
train_data = pd.read_csv("train.csv")
test_data = pd.read_csv("test.csv")
data = []
for f in train_data.columns:
# Defining the role
if f == 'target':
role = 'target'
elif f == 'id':
role = 'id'
else:
role = 'input'
# Defining the level
if 'bin' in f or f == 'target':
level = 'binary'
elif 'cat' in f :
level = 'categorical'
elif train_data[f].dtype == 'float64':
level = 'continuous'
elif train_data[f].dtype == 'int64':
level = 'ordinal'
# Initialize keep to True for all variables except for id
keep = True
if f == 'id':
keep = False
# Defining the data type
dtype = train_data[f].dtype
# Creating a Dict that contains all the metadata for the variable
f_dict = {
'varname': f,
'role': role,
'level': level,
'keep': keep,
'dtype': dtype
}
data.append(f_dict)
meta = pd.DataFrame(data, columns=['varname', 'role', 'level', 'keep', 'dtype'])
meta.set_index('varname', inplace=True)
print(meta)
pd.DataFrame({'count' : meta.groupby(['role', 'level'])['role'].size()}).reset_index()
vars_with_missing = []
for f in train_data.columns:
missings = train_data[train_data[f] == -1][f].count()
if missings > 0:
vars_with_missing.append(f)
missings_perc = missings/train_data.shape[0]
print('Variable {} has {} records ({:.2%}) with missing values'.format(f, missings, missings_perc))
print('In total, there are {} variables with missing values'.format(len(vars_with_missing)))
# Dropping the variables with too many missing values
vars_to_drop = ['ps_car_03_cat', 'ps_car_05_cat','ps_reg_03']
train_data.drop(vars_to_drop, inplace=True, axis=1)
test_data.drop(vars_to_drop, inplace=True, axis=1)
meta.loc[(vars_to_drop),'keep'] = False # Updating the meta
# Imputing with the mean or mode
vars_with_missing=list(set(vars_with_missing) - set(vars_to_drop))
for f in vars_with_missing:
if meta.loc[f]['level']=='categorical'or meta.loc[f]['level']=='ordinal':
mode_imp = Imputer(missing_values=-1, strategy="most_frequent", axis=0)
train_data[f] = mode_imp.fit_transform(train_data[[f]])
test_data[f] = mode_imp.fit_transform(test_data[[f]])
else:
mean_imp = Imputer(missing_values=-1, strategy='mean', axis=0)
train_data[f] = mean_imp.fit_transform(train_data[[f]])
test_data[f] = mean_imp.transform(test_data[[f]])
def add_noise(series, noise_level):
return series * (1 + noise_level * np.random.randn(len(series)))
def target_encode(trn_series=None,
tst_series=None,
target=None,
min_samples_leaf=1,
smoothing=1,
noise_level=0):
"""
Smoothing is computed like in the following paper by Daniele Micci-Barreca
https://kaggle2.blob.core.windows.net/forum-message-attachments/225952/7441/high%20cardinality%20categoricals.pdf
trn_series : training categorical feature as a pd.Series
tst_series : test categorical feature as a pd.Series
target : target data as a pd.Series
min_samples_leaf (int) : minimum samples to take category average into account
smoothing (int) : smoothing effect to balance categorical average vs prior
"""
assert len(trn_series) == len(target)
assert trn_series.name == tst_series.name
temp = pd.concat([trn_series, target], axis=1)
# Compute target mean
averages = temp.groupby(by=trn_series.name)[target.name].agg(["mean", "count"])
# Compute smoothing
smoothing = 1 / (1 + np.exp(-(averages["count"] - min_samples_leaf) / smoothing))
# Apply average function to all target data
prior = target.mean()
# The bigger the count the less full_avg is taken into account
averages[target.name] = prior * (1 - smoothing) + averages["mean"] * smoothing
averages.drop(["mean", "count"], axis=1, inplace=True)
# Apply averages to trn and tst series
ft_trn_series = pd.merge(
trn_series.to_frame(trn_series.name),
averages.reset_index().rename(columns={'index': target.name, target.name: 'average'}),
on=trn_series.name,
how='left')['average'].rename(trn_series.name + '_mean').fillna(prior)
# pd.merge does not keep the index so restore it
ft_trn_series.index = trn_series.index
ft_tst_series = pd.merge(
tst_series.to_frame(tst_series.name),
averages.reset_index().rename(columns={'index': target.name, target.name: 'average'}),
on=tst_series.name,
how='left')['average'].rename(trn_series.name + '_mean').fillna(prior)
# pd.merge does not keep the index so restore it
ft_tst_series.index = tst_series.index
return add_noise(ft_trn_series, noise_level), add_noise(ft_tst_series, noise_level)
train_features=train_data.columns
f_cats = [f for f in train_features if "_cat" in f]
for f in f_cats:
train_data[f + "_avg"],test_data[f + "_avg"] = target_encode(
train_data[f],
test_data[f],
target=train_data.target,
min_samples_leaf=200,
smoothing=10,
noise_level=0
)
#%%
MAX_ROUNDS = 400
OPTIMIZE_ROUNDS = False
LEARNING_RATE = 0.07
EARLY_STOPPING_ROUNDS = 50
# Note: I set EARLY_STOPPING_ROUNDS high so that (when OPTIMIZE_ROUNDS is set)
# I will get lots of information to make my own judgment. You should probably
# reduce EARLY_STOPPING_ROUNDS if you want to do actual early stopping.
model = xgb.XGBClassifier(
n_estimators=MAX_ROUNDS,
max_depth=4,
objective="binary:logistic",
learning_rate=LEARNING_RATE,
subsample=.8,
min_child_weight=6,
colsample_bytree=.8,
scale_pos_weight=1.6,
gamma=10,
reg_alpha=8,
reg_lambda=1.3,
)
K = 5
kf = KFold(n_splits = K, random_state = 1, shuffle = True)
np.random.seed(0)
y = train_data['target']
X=train_data.drop(['target'], axis=1)
y_valid_pred = 0*y
y_test_pred = 0
def eval_gini(y_true, y_prob):
y_true = np.asarray(y_true)
y_true = y_true[np.argsort(y_prob)]
ntrue = 0
gini = 0
delta = 0
n = len(y_true)
for i in range(n-1, -1, -1):
y_i = y_true[i]
ntrue += y_i
gini += y_i * delta
delta += 1 - y_i
gini = 1 - 2 * gini / (ntrue * (n - ntrue))
return gini
def gini_xgb(preds, dtrain):
labels = dtrain.get_label()
gini_score = -eval_gini(labels, preds)
return [('gini', gini_score)]
for i, (train_index, test_index) in enumerate(kf.split(train_data)):
# Create data for this fold
y_train, y_valid = y.iloc[train_index].copy(), y.iloc[test_index]
X_train, X_valid = X.iloc[train_index,:].copy(), X.iloc[test_index,:].copy()
X_test = test_data.copy()
print( "\nFold ", i)
fit_model = model.fit( X_train, y_train )
# Generate validation predictions for this fold
pred = fit_model.predict_proba(X_valid)[:,1]
print( " Gini = ", eval_gini(y_valid, pred) )
y_valid_pred.iloc[test_index] = pred
# Accumulate test set predictions
y_test_pred += fit_model.predict_proba(X_test)[:,1]
del X_test, X_train, X_valid, y_train
y_test_pred /= K # Average test set predictions
print( "\nGini for full training set:" )
eval_gini(y, y_valid_pred)
sample=pd.read_csv('sample_submission.csv')
sample['target']=y_test_pred
sample.to_csv('submission_clean_xgb.csv',index=False)
end = time.clock()
print('Running time: %s Seconds'%(end-start))