Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<load_from_csv>
warnings.filterwarnings('ignore' )
train_csv = pd.read_csv(extracted_files_path + '/training.csv')
 test_csv = pd.read_csv(extracted_files_path + '/test.csv')
 looktable_csv = pd.read_csv(Id_table_path )<drop_column>
train1 = pd.read_csv('.. /input/train.csv')
 test1 = pd.read_csv('.. /input/test.csv')
 cols1 = [c for c in train1.columns if c not in ['id', 'target', 'wheezy-copper-turtle-magic']]
feature_8 = ['left_eye_center_x', 'left_eye_center_y',
'right_eye_center_x','right_eye_center_y',
'nose_tip_x', 'nose_tip_y',
'mouth_center_bottom_lip_x',
'mouth_center_bottom_lip_y', 'Image']
train_8_csv = train_csv[feature_8].dropna().reset_index()
train_30_csv = train_csv.dropna().reset_index()<categorify>
def instant_model(train, test, cols = cols1, clf = QuadraticDiscriminantAnalysis(0.5), selection = "PCA"):
oof = np.zeros(len(train))
preds = np.zeros(len(test))
for i in tqdm_notebook(range(512)) :
train2 = train[train['wheezy-copper-turtle-magic'] == i]
test2 = test[test['wheezy-copper-turtle-magic'] == i]
idx1 = train2.index
idx2 = test2.index
data = pd.concat([pd.DataFrame(train2[cols]), pd.DataFrame(test2[cols])])
 if selection == "variance":
data2 = StandardScaler().fit_transform(VarianceThreshold(threshold=2 ).fit_transform(data[cols]))
train3 = pd.DataFrame(data2[:train2.shape[0]], index = idx1)
 test3 = pd.DataFrame(data2[train2.shape[0]:], index = idx2)
 elif selection == "PCA":
pca = PCA(n_components = 40, random_state= 1234)
 pca.fit(data[:train2.shape[0]])
 train3 = pd.DataFrame(pca.transform(data[:train2.shape[0]]), index = idx1)
 test3 = pd.DataFrame(pca.transform(data[train2.shape[0]:]), index = idx2)
 train3['target'] = train2['target']
skf = StratifiedKFold(n_splits=11, random_state=42)
 for train_index, test_index in skf.split(train3, train3['target']):
clf = clf
X_train = train3.iloc[train_index, :].drop(["target"], axis = 1)
 X_test = train3.iloc[test_index, :].drop(["target"], axis = 1)
 y_train = train3.iloc[train_index, :]['target']
y_test = train3.iloc[test_index, :]['target']
clf.fit(X_train, y_train)
 train_prob = clf.predict_proba(X_train)[:,1]
test_prob = clf.predict_proba(X_test)[:,1]
oof[idx1[test_index]] = test_prob
preds[idx2] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'], oof)
 print(f'AUC: {auc:.5}')
 return oof, preds
def str_to_array(pd_series):

 data_size = len(pd_series)
 X = np.zeros(shape=(data_size,96,96,1), dtype=np.float32)
 for i in tqdm(range(data_size)) :
img_str = pd_series[i]
img_list = img_str.split(' ')
 img_array = np.array(img_list, dtype=np.float32)
 img_array = img_array.reshape(96,96,1)
 X[i] = img_array
return X<save_to_csv>
def get_newtrain(train, test, preds, oof):
test['target'] = preds
test.loc[test['target'] > 0.985, 'target'] = 1
test.loc[test['target'] < 0.015, 'target'] = 0
usefull_test = test[(test['target'] == 1)|(test['target'] == 0)]
new_train = pd.concat([train, usefull_test] ).reset_index(drop=True)
 new_train.loc[oof > 0.985, 'target'] = 1
new_train.loc[oof < 0.015, 'target'] = 0
return new_train
X_train_30 = str_to_array(train_30_csv['Image'])
 labels_30 = train_30_csv.drop(['index','Image'], axis=1)
 y_train_30 = labels_30.to_numpy(dtype=np.float32)
 print('X_train with 30 feature shape: ', X_train_30.shape)
 print('y_train with 30 feature shape: ', y_train_30.shape )<save_to_csv>
oof_temp, preds_temp = instant_model(train1, test1, selection = 'variance')
 newtrain1 = get_newtrain(train1, test1, preds_temp, oof_temp )
X_train_8 = str_to_array(train_8_csv['Image'])
 labels_8 = train_8_csv.drop(['index','Image'], axis=1)
 y_train_8 = labels_8.to_numpy(dtype=np.float32)
 print('X_train with 8 feature shape: ', X_train_8.shape)
 print('y_train with 8 feature shape: ', y_train_8.shape )<choose_model_class>
oof_qda_var, preds_qda_var = instant_model(newtrain1, test1, selection = 'variance')
 oof_knn_var, preds_knn_var = instant_model(newtrain1, test1, \
clf = KNeighborsClassifier(n_neighbors = 7, p = 2, weights = 'distance'),\
selection = 'variance' )
def create_model(output_n = 30):

 model = keras.models.Sequential([
keras.layers.InputLayer(input_shape=[96,96,1]),
keras.layers.Conv2D(filters=32, kernel_size=[5,5], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=32, kernel_size=[5,5], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Conv2D(filters=64, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=64, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Conv2D(filters=128, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=128, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Conv2D(filters=256, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=256, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Conv2D(filters=512, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=512, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Flatten() ,
keras.layers.Dense(units=512, activation='relu'),
keras.layers.Dropout (.1),
keras.layers.Dense(units=output_n),
])
 model.compile(optimizer = 'adam' , loss = "mean_squared_error", metrics=["mae"])
 return model<choose_model_class>
oof_qda_pca, preds_qda_pca = instant_model(newtrain1, test1)
 oof_knn_pca, preds_knn_pca = instant_model(newtrain1, test1, \
clf = KNeighborsClassifier(n_neighbors = 7, p = 2, weights = 'distance'))
model_30 = create_model(output_n=30)
 model_8 = create_model(output_n=8 )<choose_model_class>
logit = LogisticRegression()
newX_train_stack = pd.DataFrame({"QDA_var": oof_qda_var, "QDA_pca": oof_qda_pca, \
"KNN_var": oof_knn_var, "KNN_pca": oof_knn_pca})
 newX_test_stack = pd.DataFrame({"QDA_var": preds_qda_var, "QDA_pca": preds_qda_pca, \
"KNN_var": preds_knn_var, "KNN_pca": preds_knn_pca})
 newy_stack = newtrain1['target']
logit.fit(newX_train_stack, newy_stack)
 pred_stack_train = logit.predict_proba(newX_train_stack)[:,1]
pred_stack_test = logit.predict_proba(newX_test_stack)[:,1]
print("ROC_AUC: {0}".format(roc_auc_score(newy_stack, pred_stack_train)))
 stack_result = logit.predict_proba(newX_test_stack)[:,1]
<train_model><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = stack_result
sub.to_csv('submission_4stack.csv',index=False )