Kaggle competition: Porto Seguro’s Safe Driver Prediction
<SOS> metric: NormalizedGini Kaggle data source: porto-seguro’s-safe-driver-prediction<create_dataframe>
__author__ = 'Tilii: https://kaggle.com/tilii7'
warnings.filterwarnings('ignore')
testset = DataLoader(
 TestDataset(test_data_folder, df, mean, std),
batch_size=batch_size,
shuffle=False,
num_workers=0,
pin_memory=True
)<define_variables>
def timer(start_time=None):
if not start_time:
start_time = datetime.now()
return start_time
elif start_time:
thour, temp_sec = divmod(( datetime.now() - start_time ).total_seconds() , 3600)
 tmin, tsec = divmod(temp_sec, 60)
 print('
Time taken: %i hours %i minutes and %s seconds.' %(thour, tmin, round(tsec, 2)))
 train_df = pd.read_csv('.. /input/train.csv', dtype={'id': np.int32, 'target': np.int8})
 Y = train_df['target'].values
X = train_df.drop(['target', 'id'], axis=1)
 test_df = pd.read_csv('.. /input/test.csv', dtype={'id': np.int32})
 test = test_df.drop(['id'], axis=1)
threshold_label = [0.50,0.50,0.50,0.50,]<define_variables>
params = {
'min_child_weight': [1, 5, 10],
'gamma': [0.5, 1, 1.5, 2, 5],
'subsample': [0.6, 0.8, 1.0],
'colsample_bytree': [0.6, 0.8, 1.0],
'max_depth': [3, 4, 5]
}
augment = ['null']<prepare_output>
xgb = XGBClassifier(learning_rate=0.02, n_estimators=600, objective='binary:logistic',
silent=True, nthread=1)
probability_label, image_id = get_classification_preds(model_classification, testset)
 predict_label = probability_label>np.array(threshold_label ).reshape(1,4,1,1)
 image_id_class_id = []
encoded_pixel = []
for b in range(len(image_id)) :
for c in range(4):
image_id_class_id.append(image_id[b]+'_%d'%(c+1))
if predict_label[b,c]==0:
rle=''
else:
rle ='1 1'
encoded_pixel.append(rle)
 df_classification = pd.DataFrame(zip(image_id_class_id, encoded_pixel), columns=['ImageId_ClassId', 'EncodedPixels'] )<load_pretrained>
folds = 3
param_comb = 5
skf = StratifiedKFold(n_splits=folds, shuffle = True, random_state = 1001)
 random_search = RandomizedSearchCV(xgb, param_distributions=params, n_iter=param_comb, scoring='roc_auc', n_jobs=4, cv=skf.split(X,Y), verbose=3, random_state=1001)
 start_time = timer(None)
 random_search.fit(X, Y)
 timer(start_time)
ckpt_path = ".. /input/res18fullsize/model_swa.pth"
device = torch.device("cuda")
 model_segmentation = Unet("resnet18", encoder_weights=None, classes=4, activation=None)
 model_segmentation.to(device)
 model_segmentation.eval()
state = torch.load(ckpt_path, map_location=lambda storage, loc: storage)
 model_segmentation.load_state_dict(state["state_dict"] )<define_variables>
print('
All results:')
 print(random_search.cv_results_)
 print('
Best estimator:')
 print(random_search.best_estimator_)
 print('
Best normalized gini score for %d-fold search with %d parameter combinations:' %(folds, param_comb))
print(random_search.best_score_ * 2 - 1)
 print('
Best hyperparameters:')
 print(random_search.best_params_)
 results = pd.DataFrame(random_search.cv_results_)
 results.to_csv('xgb-random-grid-search-results-01.csv', index=False)
<categorify><EOS> 
y_test = random_search.predict_proba(test)
 results_df = pd.DataFrame(data={'id':test_df['id'], 'target':y_test[:,1]})
 results_df.to_csv('submission-random-grid-search-xgb-porto-01.csv', index=False)