Kaggle competition: Porto Seguro’s Safe Driver Prediction
<SOS> metric: NormalizedGini Kaggle data source: porto-seguro’s-safe-driver-prediction<compute_train_metric>
warnings.filterwarnings("ignore")
 df_train = pd.read_csv('.. /input/porto-seguro-safe-driver-prediction/train.csv')
 df_test = pd.read_csv('.. /input/porto-seguro-safe-driver-prediction/test.csv')
 print('train data size', df_train.shape)
 print('test data size', df_test.shape)
 display(df_train.head())
 display(df_test.head() )
def CEParamSearch(y_true, multi_predictions, classifier_names,
groupby = 'new_group_id', generations = 20,
seed = 1, mutation_rate = 0.35):
np.random.seed(seed)
 weights = np.random.rand(len(classifiers))
classifier_weights = {classifier:weight for classifier,weight in zip(classifier_names,weights)}
ensemble_prediction = classifier_ensemble(multi_predictions,classifier_weights,groupby=groupby)
 y_pred = ensemble_prediction.surface
score = sum(y_pred == y_true)/len(y_pred)
 scores = [score]
start = time.time()
gene = random.choice(classifier_names)
 orig_mut_rate = mutation_rate
for generation in range(1,generations+1):
new_weights = classifier_weights
mutation =(np.random.rand(1)-0.5)*mutation_rate
new_weights[gene] += mutation
if new_weights[gene] < 0:
new_weights[gene] = 0
if new_weights[gene] > 1:
new_weights[gene] = 1
ensemble_prediction = classifier_ensemble(multi_predictions,new_weights,groupby=groupby)
 y_pred = ensemble_prediction.surface
new_score = sum(y_pred == y_true)/len(y_pred)
 change = new_score - score
if change < 0:
scores.append(score)
 gene = random.choice(classifier_names)
 mutation_rate = orig_mut_rate
elif change == 0:
scores.append(score)
 classifier_weights = new_weights
elif change > 0:
mutation_rate /= 0.75
score = new_score
scores.append(score)
 classifier_weights = new_weights
progress = generation/generations
time_elapsed =(time.time() -start)
 ETA =(time_elapsed/progress-time_elapsed)/60
print('Score:{0:1.5f}\t|\tChange:{1:1.5f}\t|\tTime Remaining: {2:1.2f} min'.format(score,change,ETA), end = '\r')
 plt.plot(scores)
 return classifier_weights<choose_model_class>
X_train = df_train.drop(['id', 'target'], axis=1)
 y_train = df_train['target']
classifier_weights = CEParamSearch(train_filtered.surface,
multi_predictions,
classifier_names,
generations = 25,
mutation_rate = 0.5,
seed = 40 )<init_hyperparams>
def metrics(true, preds):
accuracy = accuracy_score(true, preds)
 recall = recall_score(true, preds)
 f1score = f1_score(true, preds)
 cf = confusion_matrix(true, preds)
 print('accuracy: {}, recall: {}, f1-score: {}'.format(accuracy, recall, f1score))
print('Confusion matrix', cf)
 def gini(true, preds):
res = 2* roc_auc_score(true, preds)- 1
return res
X_tr, X_te, y_tr, y_te = train_test_split(X_train, y_train, random_state=42, test_size=0.2)
 def test_clfs(clfs):
for clf in clfs:
print('------------------------------------------')
 start = time()
clf = clf(random_state=42)
 clf.fit(X_tr, y_tr)
 y1_pred = clf.predict(X_te)
 y1_pred_prob = clf.predict_proba(X_te)
 print(str(clf), '
results:')
 metrics(y_te, y1_pred)
 print('gini score', gini(y_te, y1_pred_prob[:, 1]))
end = time()
print('Processing time', end-start,'s')
 classifiers = [LGBMClassifier, XGBClassifier, RandomForestClassifier]
test_clfs(classifiers )
classifier_weights = {'RFC': 1,
'SVM': 1,
'KNN': 0,
'DT': 0.22006725,
'XGB': 0.38377276}<predict_on_test>
sm_methods = [SMOTE,RandomUnderSampler,RandomOverSampler]
clf = LGBMClassifier(random_state=42)
 for sm in sm_methods:
print('------------------------------------------')
 sm = sm(random_state=42)
 start = time()
X_train_resampled, y_train_resampled = sm.fit_sample(X_tr, y_tr)
 clf.fit(X_train_resampled, y_train_resampled)
 y_pred = clf.predict(X_te)
 y_pred_prob = clf.predict_proba(X_te)
 print(str(sm), 'results:')
 print('training resampled shape', X_train_resampled.shape)
 print('value counts in each class', y_train_resampled.value_counts())
 metrics(y_te, y_pred)
 gini_score = gini(y_te, y_pred_prob[:, 1])
 print('Normalized gini score', gini_score)
 end = time()
print('Processing time', end-start,'s' )
multi_predictions_test = get_predictions(test_filtered,classifiers,scaler_f )<choose_model_class>
cv = StratifiedKFold(n_splits=5)
 scores_gini = []
for train_index, validation_index in cv.split(X_train, y_train):
train, val = X_train.iloc[train_index], X_train.iloc[validation_index]
target_train, target_val = y_train.iloc[train_index], y_train.iloc[validation_index]
clf = LGBMClassifier(random_state=42)
 clf.fit(train, target_train)
 y_val_pred = clf.predict(val)
 y_val_pred_prob = clf.predict_proba(val)
 gini_score = gini(target_val, y_val_pred_prob[:, 1])
 print('gini score:', gini_score)
 scores_gini.append(gini_score)
 print('mean gini_score: {}'.format(np.mean(scores_gini)))
ensemble_prediction_test = classifier_ensemble(predictions=multi_predictions_test,
classifier_weights=classifier_weights )<save_to_csv>
df_train = pd.read_csv('.. /input/porto-seguro-safe-driver-prediction/train.csv')
 X_train = df_train.drop(['id', 'target'], axis=1)
 y_train = df_train['target']
features = df_train.columns.tolist()
cat_features = [c for c in features if 'cat' in c]
X_train = pd.get_dummies(X_train, columns=cat_features, prefix_sep='_', drop_first=True)
output = ensemble_prediction_test[['orig_id','surface']].reset_index().drop('series_id', axis =1)
 output.to_csv(path_or_buf = 'Ensemble_Classified.csv',
header = ['series_id','surface'],
index = False )<merge>
scores_gini = []
for train_index, validation_index in cv.split(X_train, y_train):
train, val = X_train.iloc[train_index], X_train.iloc[validation_index]
target_train, target_val = y_train.iloc[train_index], y_train.iloc[validation_index]
clf = LGBMClassifier(random_state=42, n_jobs=-1)
 clf.set_params(**best_params)
 clf.fit(train, target_train)
 y_val_pred = clf.predict(val)
 y_val_pred_prob = clf.predict_proba(val)
 gini_score = gini(target_val, y_val_pred_prob[:, 1])
 print('gini score:', gini_score)
 scores_gini.append(gini_score)
 print('mean gini_score: {}'.format(np.mean(scores_gini)) )
metadata = sorted_data.groupby('series_id' ).max() [['surface','test','new_group_id', 'orig_id']]
filtered_extracted_matched = metadata.join(extracted_features[feature_columns])
 unmatched_filtered = filtered_extracted_matched[filtered_extracted_matched.surface == 'no_surface']<predict_on_test>
pd.set_option('display.max_columns', None)
 y_preds = y_val_pred_prob[:,1]
results = pd.DataFrame({'target_true':target_val , 'target_pred': y_preds})
 b = results.nlargest(10, columns='target_pred')
 b2 = results.nlargest(1000, columns='target_pred')
 c = results.nsmallest(1000, columns='target_pred')
 c_counts_1 =(c['target_true']==1 ).sum()
b2_counts_1 =(b2['target_true']==1 ).sum()
print('Numeber of 1s in 1000 smallest probabilities:', c_counts_1)
 print('Number of 1s in 1000 largest probabilities:', b2_counts_1)
 display(b)
 display(val.loc[b.index] )
multi_predictions_unmatched = get_predictions(unmatched_filtered, classifiers, scaler_f)
 ensemble_prediction_unmatched = classifier_ensemble(multi_predictions_unmatched, classifier_weights)
 unmatched_output = ensemble_prediction_unmatched[['orig_id','surface']].reset_index().drop('series_id', axis =1 )<drop_column>
print(imp_features_10large)
 display(val.loc[b.index, imp_features_10large.feature] )
matched_output = metadata[(metadata.test == 1)&(metadata.surface != 'no_surface')][['orig_id','surface']].reset_index().drop('series_id', axis =1 )<save_to_csv>
features = X_train.columns.tolist()
cat_car_05 = [c for c in features if 'car_05_cat' in c]
calc_features = [c for c in features if 'calc' in c]
X_train.drop(calc_features, axis=1, inplace=True)
 X_train.drop(cat_car_05, axis=1, inplace=True)
 X_train['ps_reg_03'].replace(-1, X_train['ps_reg_03'].median() , inplace=True)
 X_train['ps_car_14'].replace(-1, X_train['ps_car_14'].median() , inplace=True)
 scores_gini = []
for train_index, validation_index in cv.split(X_train, y_train):
train, val = X_train.iloc[train_index], X_train.iloc[validation_index]
target_train, target_val = y_train.iloc[train_index], y_train.iloc[validation_index]
clf = LGBMClassifier(random_state=42, n_jobs=-1)
 clf.set_params(**best_params)
 clf.fit(train, target_train)
 y_val_pred = clf.predict(val)
 y_val_pred_prob = clf.predict_proba(val)
 gini_score = gini(target_val, y_val_pred_prob[:, 1])
 print('gini score:', gini_score)
 scores_gini.append(gini_score)
 print('mean gini_score: {}'.format(np.mean(scores_gini)) )
output = pd.concat([matched_output,unmatched_output])
 output.to_csv('Hybrid_Classified.csv',
header = ['series_id','surface'],
index = False )<load_from_csv>
y_preds = y_val_pred_prob[:,1]
results = pd.DataFrame({'target_true':target_val , 'target_pred': y_preds})
 b = results.nlargest(10, columns='target_pred')
 b2 = results.nlargest(1000, columns='target_pred')
 c = results.nsmallest(1000, columns='target_pred')
 c_counts_1 =(c['target_true']==1 ).sum()
b2_counts_1 =(b2['target_true']==1 ).sum()
print('Numeber of 1s in 1000 smallest probabilities:', c_counts_1)
 print('Number of 1s in 1000 largest probabilities:', b2_counts_1)
train_X = pd.read_csv('.. /input/career-con-2019/X_train.csv')
 test_X = pd.read_csv('.. /input/cconlink/X_test_trial.csv')
 tot = pd.concat([train_X, test_X])
 train_X = train_X.iloc[:,3:].values.reshape(-1,128,10)
 test_X = test_X.iloc[:,3:].values.reshape(-1,128,10)
 print('train_X shape:', train_X.shape, ', test_X shape:', test_X.shape )<load_from_csv>
df_test = pd.read_csv('.. /input/porto-seguro-safe-driver-prediction/test.csv')
 X_test = df_test.drop('id', axis=1)
 X_test = pd.get_dummies(X_test, columns=cat_features, prefix_sep='_', drop_first=True)
 X_test.drop(calc_features, axis=1, inplace=True)
 X_test.drop(cat_car_05, axis=1, inplace=True)
 X_test['ps_reg_03'].replace(-1, X_test['ps_reg_03'].median() , inplace=True)
 X_test['ps_car_14'].replace(-1, X_test['ps_car_14'].median() , inplace=True)
 assert(X_train.shape[1]==X_test.shape[1])
 clf.fit(X_train, y_train )
<compute_test_metric><EOS> 
preds = clf.predict_proba(X_test)[:,1]
df_subm = pd.read_csv('.. /input/porto-seguro-safe-driver-prediction/sample_submission.csv')
 df_subm.loc[:,'target'] = preds
display(df_subm)
 df_subm.to_csv('submission.csv', index=False)