Kaggle competition: Porto Seguro’s Safe Driver Prediction
<SOS> metric: NormalizedGini Kaggle data source: porto-seguro’s-safe-driver-prediction<save_to_csv>
import pandas as pd
import numpy as np
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier, ExtraTreesClassifier, AdaBoostClassifier
from sklearn.metrics import roc_auc_score
from rgf.sklearn import RGFClassifier
Submission = pd.DataFrame({'Id': test.index, 'SalePrice': FinalPrediction})
 Submission.to_csv('Submission.csv', index=False)
 Submission<install_modules>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
!pip install.. /input/pretrainedmodels/pretrainedmodels-0.7.4/pretrainedmodels-0.7.4/ > /dev/null
package_path = '.. /input/unetmodelscript'
sys.path.append(package_path )<import_modules>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
import pdb
import os
import cv2
import torch
import pandas as pd
import numpy as np
from tqdm import tqdm
import torch.backends.cudnn as cudnn
from torch.utils.data import DataLoader, Dataset
from albumentations import(Normalize, Compose)
 from albumentations.torch import ToTensor
import torch.utils.data as data
import torchvision.models as models
import torch.nn as nn
from torch.nn import functional as F
from model import Unet<define_search_model>
id_test = test['id'].values
target_train = train['target'].values
train = train.drop(['target','id'], axis = 1)
 test = test.drop(['id'], axis = 1)
 col_to_drop = train.columns[train.columns.str.startswith('ps_calc_')]
train = train.drop(col_to_drop, axis=1)
 test = test.drop(col_to_drop, axis=1)
 train = train.replace(-1, np.nan)
 test = test.replace(-1, np.nan)
 cat_features = [a for a in train.columns if a.endswith('cat')]
train['intrain'] = True
test['intrain'] = False
both = pd.concat([train,test],axis=0)
 for column in cat_features:
	temp = pd.get_dummies(pd.Series(both[column]))
	both = pd.concat([both,temp],axis=1)
 	both = both.drop([column],axis=1)
 train = both[both.intrain==True].drop(['intrain'],axis=1)
 test = both[both.intrain==False].drop(['intrain'],axis=1)
 print(train.values.shape, test.values.shape )
class Resnet34_classification(nn.Module):
def __init__(self,num_class=4):
super(Resnet34_classification, self ).__init__()
e = ResNet34()
self.block = nn.ModuleList([
e.block0,
e.block1,
e.block2,
e.block3,
e.block4,
])
 e = None
self.feature = nn.Conv2d(512,32, kernel_size=1)
 self.logit = nn.Conv2d(32,num_class, kernel_size=1)
 def forward(self, x):
batch_size,C,H,W = x.shape
for i in range(len(self.block)) :
x = self.block[i](x)
 x = F.dropout(x,0.5,training=self.training)
 x = F.adaptive_avg_pool2d(x, 1)
 x = self.feature(x)
 logit = self.logit(x)
 return logit 
<load_pretrained>
class Ensemble(object):
def __init__(self, n_splits, stacker, base_models):
self.n_splits = n_splits
self.stacker = stacker
self.base_models = base_models
def fit_predict(self, X, y, T):
X = np.array(X)
 y = np.array(y)
 T = np.array(T)
 folds = list(StratifiedKFold(n_splits=self.n_splits, shuffle=True, random_state=2016 ).split(X, y))
S_train = np.zeros(( X.shape[0], len(self.base_models)))
 S_test = np.zeros(( T.shape[0], len(self.base_models)))
 for i, clf in enumerate(self.base_models):
S_test_i = np.zeros(( T.shape[0], self.n_splits))
for j,(train_idx, test_idx)in enumerate(folds):
X_train = X[train_idx]
y_train = y[train_idx]
X_holdout = X[test_idx]
print("Fit %s fold %d" %(str(clf ).split('(')[0], j+1))
clf.fit(X_train, y_train)
 y_pred = clf.predict_proba(X_holdout)[:,1]
S_train[test_idx, i] = y_pred
S_test_i[:, j] = clf.predict_proba(T)[:,1]
S_test[:, i] = S_test_i.mean(axis=1)
 print(" Model score: %.5f
" % roc_auc_score(y, S_train[:,i]))
self.base_preds = S_test
almost_zero = 1e-12
almost_one = 1 - almost_zero
S_train[S_train>almost_one] = almost_one
S_train[S_train<almost_zero] = almost_zero
S_train = np.log(S_train/(1-S_train))
S_test[S_test>almost_one] = almost_one
S_test[S_test<almost_zero] = almost_zero
S_test = np.log(S_test/(1-S_test))
results = cross_val_score(self.stacker, S_train, y, cv=self.n_splits, scoring='roc_auc')
 print("Stacker score: %.5f" %(results.mean()))
self.stacker.fit(S_train, y)
 print('Coefficients:', self.stacker.coef_)
 res = self.stacker.predict_proba(S_test)[:,1]
return res
model_classification = Resnet34_classification()
model_classification.load_state_dict(torch.load('.. /input/clsification/00007500_model.pth', map_location=lambda storage, loc: storage), strict=True )<categorify>
lgb_params = {}
lgb_params['learning_rate'] = 0.02
lgb_params['n_estimators'] = 650
lgb_params['max_bin'] = 10
lgb_params['subsample'] = 0.8
lgb_params['subsample_freq'] = 10
lgb_params['colsample_bytree'] = 0.8
lgb_params['min_child_samples'] = 500
lgb_params['random_state'] = 99
lgb_params2 = {}
lgb_params2['n_estimators'] = 1090
lgb_params2['learning_rate'] = 0.02
lgb_params2['colsample_bytree'] = 0.3
lgb_params2['subsample'] = 0.7
lgb_params2['subsample_freq'] = 2
lgb_params2['num_leaves'] = 16
lgb_params2['random_state'] = 99
lgb_params3 = {}
lgb_params3['n_estimators'] = 1100
lgb_params3['max_depth'] = 4
lgb_params3['learning_rate'] = 0.02
lgb_params3['random_state'] = 99
class TestDataset(Dataset):

 def __init__(self, root, df, mean, std):
self.root = root
df['ImageId'] = df['ImageId_ClassId'].apply(lambda x: x.split('_')[0])
 self.fnames = df['ImageId'].unique().tolist()
self.num_samples = len(self.fnames)
 self.transform = Compose(
 [
Normalize(mean=mean, std=std, p=1),
ToTensor() ,
] 
)
 def __getitem__(self, idx):
fname = self.fnames[idx]
path = os.path.join(self.root, fname)
 image = cv2.imread(path)
 images = self.transform(image=image)["image"]
return fname, images
def __len__(self):
return self.num_samples<define_variables>
lgb_model = LGBMClassifier(**lgb_params)
 lgb_model2 = LGBMClassifier(**lgb_params2)
 lgb_model3 = LGBMClassifier(**lgb_params3)
 log_model = LogisticRegression(fit_intercept=False )
sample_submission_path = '.. /input/severstal-steel-defect-detection/sample_submission.csv'
test_data_folder = ".. /input/severstal-steel-defect-detection/test_images"<define_search_space>
stack = Ensemble(n_splits=3,
stacker = log_model,
base_models =(lgb_model, lgb_model2, lgb_model3))
y_pred = stack.fit_predict(train, target_train, test )
<load_from_csv><EOS> 
sub = pd.DataFrame()
sub['id'] = id_test
sub['target'] = y_pred
sub.to_csv('stacked_1.csv', index=False )