Kaggle competition: Human Protein Atlas Image Classification
<SOS> metric: MacroFScore Kaggle data source: human-protein-atlas-image-classification<data_type_conversions>
import tensorflow as tf
new_train["Alley"] = new_train["Alley"].fillna("None")
 new_test["Alley"] = new_test["Alley"].fillna("None" )<data_type_conversions>
from keras import backend as K
from keras.optimizers import Adam, SGD, Adagrad, Adadelta
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, LearningRateScheduler, CSVLogger
from keras.models import Model, Sequential, load_model, model_from_json
from keras.layers import Flatten, Dense, Activation, Input, Dropout, Activation, BatchNormalization, Reshape
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
new_train["Fence"] = new_train["Fence"].fillna("None")
 new_test["Fence"] = new_test["Fence"].fillna("None" )<data_type_conversions>
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MultiLabelBinarizer
new_train["FireplaceQu"] = new_train["FireplaceQu"].fillna("None")
 new_test["FireplaceQu"] = new_test["FireplaceQu"].fillna("None" )<data_type_conversions>
import random
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import cv2
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
new_train[col] = new_train[col].fillna('None')
 new_test[col] = new_test[col].fillna('None')
 for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
new_train[col] = new_train[col].fillna(0)
 new_test[col] = new_test[col].fillna(0 )<data_type_conversions>
data_path = '.. /input'
train_path = os.path.join(data_path, 'train')
 test_path = os.path.join(data_path,'test')
 labels_path = os.path.join(data_path, 'train.csv' )
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
new_train[col] = new_train[col].fillna('None')
 new_test[col] = new_test[col].fillna('None')
 for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
new_train[col] = new_train[col].fillna(0)
 new_test[col] = new_test[col].fillna(0 )<data_type_conversions>
def pick_label(x):
t = x
if '27' in t:
return '27'
elif '15' in t:
return '15'
elif '10' in t:
return '10'
elif '9' in t:
return '9'
elif '8' in t:
return '8'
elif '0' in t:
return '0'
elif '25' in t:
return '25'
else:
return t[0]
train_labels = pd.read_csv(labels_path,index_col=False)
 labels_dict = dict(zip(train_labels.values[ : ,0], train_labels.values[ : , 1]))
t = {k:pick_label(v.split())for(k, v)in labels_dict.items() }
train_labels['t'] = t.values()
t_arr = np.array(list(t.items()))
new_train["MasVnrType"] = new_train["MasVnrType"].fillna("None")
 new_test["MasVnrType"] = new_test["MasVnrType"].fillna("None")
 new_train["MasVnrArea"] = new_train["MasVnrArea"].fillna(0)
 new_test["MasVnrArea"] = new_test["MasVnrArea"].fillna(0 )<feature_engineering>
train_ids, val_ids = train_test_split(train_labels, stratify=t_arr[ :, 1],
test_size=0.1, random_state=48 )
new_test['MSZoning'] = new_test['MSZoning'].fillna(train['MSZoning'].mode() [0] )<define_variables>
labels = [item.split() for item in train_labels['Target']]
mlb = MultiLabelBinarizer()
mlb.fit(labels)
 classes = mlb.classes_
y_val = mlb.transform([item.split() for item in val_ids['Target']] )
cols_to_drop = []
cols_to_clean = []<data_type_conversions>
def model(sample_shape):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), input_shape=sample_shape, name='conv1'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32, kernel_size=(3, 3), name='conv2'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32, kernel_size=(3, 3), name='conv2b'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), name='pool1'))
model.add(Conv2D(64, kernel_size=(3, 3), name='conv3'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64, kernel_size=(3, 3), name='conv4'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), name='pool2'))
model.add(Conv2D(128, kernel_size=(3, 3), name='conv5'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128, kernel_size=(3, 3), name='conv6'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), name='pool3'))
model.add(Conv2D(256, kernel_size=(3, 3), name='conv7'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(256, kernel_size=(3, 3), name='conv8'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(GlobalAveragePooling2D())
 model.add(Dense(4096, name='fc1'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dense(28))
model.add(Activation('sigmoid'))
return model
def handle_na(train, test, cols_to_clean, cols_to_drop):
if(cols_to_drop):
train.drop(cols_to_drop, axis = 1, inplace = True)
 test.drop(cols_to_drop, axis = 1, inplace = True)
 if(cols_to_clean):
for col in cols_to_clean:
train[col+'_is_na'] = train[col].isnull()
test[col+'_is_na'] = test[col].isnull()
for col in test.columns:
if np.issubdtype(train[col].dtype, np.number):
train.loc[train[col].isnull() == True, col] = train[col].median()
test.loc[test[col].isnull() == True, col] = train[col].median()
if(train[col].dtype == 'O'):
train.loc[train[col].isnull() == True, col] = train[col].mode().iloc[0]
test.loc[test[col].isnull() == True, col] = train[col].mode().iloc[0]
print(f'Shape of Train Set: {train.shape}')
 print(f'Shape of Test Set : {test.shape}')
 return train, test<compute_test_metric>
DEPTH = 3
BATCH_SIZE = 32
IMG_SIZE = 256
SAMPLE_SHAPE =(IMG_SIZE, IMG_SIZE, DEPTH)
 SEED = 1234
random.seed(SEED )
new_train, new_test = handle_na(new_train, new_test, cols_to_clean, cols_to_drop )<categorify>
K.clear_session()
model = model(SAMPLE_SHAPE )
def label_encode(train, test):
combined = train.append(test, sort = False)
 for col in test.columns:
if(train[col].dtype == 'O'):
le = LabelEncoder()
le.fit(combined[col])
 train[col] = le.transform(train[col])
 test[col] = le.transform(test[col])
 return train, test<categorify>
EPOCHS = 50
val_steps = int(np.ceil(len(val_ids)/BATCH_SIZE))
num_steps = int(np.ceil(len(train_ids)/BATCH_SIZE))
print('train_size: ', len(train_ids), 'batch: ', BATCH_SIZE, 'num steps: ', num_steps )
new_train, new_test = label_encode(new_train, new_test )<feature_engineering>
def f1(y_true, y_pred, thresholds= np.array(28*[0.5])) :
m, n = y_true.shape
p = tf.cast(tf.greater(y_pred, thresholds), tf.float32)
 tp = tf.reduce_sum(y_true * p, 0)
 num_pos = tf.reduce_sum(tf.cast(y_true, tf.float32), 0)
 pred_pos = tf.reduce_sum(p, 0)
 precision = tp/(pred_pos + K.epsilon())
 recall = tp /(num_pos + K.epsilon())
 f1 = tf.reduce_mean(tf.divide(2*precision*recall,(precision + recall + K.epsilon())))
 K.get_session().run(tf.local_variables_initializer())
 return f1
new_train['TotalSF'] = new_train['TotalBsmtSF'] + new_train['1stFlrSF'] + new_train['2ndFlrSF']
new_test['TotalSF'] = new_test['TotalBsmtSF'] + new_test['1stFlrSF'] + new_test['2ndFlrSF']<feature_engineering>
lr = 1e-3
adam = Adam(lr=lr)
 model.compile(optimizer=adam,
loss='binary_crossentropy',
metrics=[f1])
 earlyStopping = EarlyStopping(monitor='val_loss', min_delta=0, mode='min', patience=6, verbose=0,restore_best_weights=True)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', mode='min',factor=0.2, patience=3, min_lr = 1e-6, cooldown=1,verbose=1)
new_train['SalePrice'] = np.log(new_train['SalePrice'] )<split>
history = model.fit_generator(train_generator(BATCH_SIZE),
steps_per_epoch = num_steps,
validation_data=val_generator(BATCH_SIZE),
validation_steps=val_steps,
epochs=EPOCHS,
callbacks=[earlyStopping, reduce_lr], verbose=1 )
def split(df, target, test_size = 0.1):
X_train, X_valid, y_train, y_valid = train_test_split(df.drop(target, axis = 1), df[target], test_size = test_size, random_state = 42)
 print(f'Shape of Train Set: {X_train.shape}')
 print(f'Shape of Valid Set: {X_valid.shape}')
 return X_train, X_valid, y_train, y_valid<split>
predictions = model.predict_generator(val_generator(BATCH_SIZE),steps=val_steps)
X_train, X_valid, y_train, y_valid = split(new_train, 'SalePrice' )<train_model>
with tf.Session() as sess:
print(sess.run(f1(y_val, predictions)) )
def xgtrain(X_train, X_valid, y_train, y_valid):
regressor = XGBRegressor(
 n_estimators = 50000,
learning_rate = 0.01,
max_depth = 3,
subsample = 0.5,
colsample_bytree = 0.2 
)
 regressor_ = regressor.fit(
 X_train.values, y_train.values,
eval_metric = 'rmse',
eval_set = [ 
(X_train.values, y_train.values), 
(X_valid.values, y_valid.values)
 ],
verbose = 1000,
early_stopping_rounds = 500, 
)
 return regressor_<train_model>
submission = pd.read_csv(os.path.join(data_path, 'sample_submission.csv'), index_col=False )
%%time
regressor_ = xgtrain(X_train, X_valid, y_train, y_valid )<train_model>
num_steps = int(np.ceil(len(submission)/BATCH_SIZE))
test_pred = model.predict_generator(test_generator(BATCH_SIZE), steps=num_steps )
def lgbtrain(X_train, y_train, X_valid, y_valid):
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train)
 params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': {'l2_root'},
'max_depth': 3,
'learning_rate': 0.01,
'feature_fraction': 0.2,
'bagging_fraction': 0.8,
'bagging_freq': 5,
'num_leaves': 5
}
gbm = lgb.train(
 params,
lgb_train,
valid_sets = lgb_eval,
num_boost_round=50000,
early_stopping_rounds=100,
verbose_eval = 250 
)
 return gbm<train_model>
def binary_prf1(y_true, y_pred):
num_pos = np.sum(y_true)
 pred_pos = np.sum(y_pred)
 tp = np.sum(y_true * y_pred)
 if pred_pos > 0:
precision = tp/pred_pos
else:
precision = 0
if num_pos > 0:
recall = tp/num_pos
else:
recall = 0
print('no pos cases for this class')
 if precision >0 or recall > 0:
f1 = 2*precision*recall/(precision + recall)
 else:
f1 = 0
return precision, recall, f1
def max_thresh(y_val, predictions, n=100):
x = np.linspace(0,1,n+1)[1 : -1]
f1_matrix = np.zeros(( len(x), 28))
for i in range(28):
class_f1 = []
for thresh in x:
pred_class =(predictions > thresh ).astype(int)
 class_f1.append(( binary_prf1(y_val[ :, i], pred_class[ : , i])[2]))
f1_matrix[ :, i] = np.array(class_f1)
 max_loc = np.argmax(f1_matrix, axis=0)
 max_thresh = [x[i] for i in max_loc]
return max_thresh
%%time
gbm = lgbtrain(X_train, y_train, X_valid, y_valid )<save_to_csv>
max_t = max_thresh(y_val, predictions)
 with tf.Session() as sess:
print(sess.run(f1(y_val, predictions, thresholds=max_t)) )
def prepare_submission(comment = 'latest'):
xgb_preds = np.exp(regressor_.predict(new_test.values))
lgb_preds = np.expm1(gbm.predict(new_test.values))
submission['SalePrice'] = 0.75*xgb_preds + 0.25*lgb_preds
submission.to_csv('submission' + comment + '.csv', index=False)
 print(submission.head() )<import_modules>
pred_classes =(test_pred > max_t ).astype(int )
FileLinks('.' )<categorify>
pred_labels = mlb.inverse_transform(pred_classes)
 pred_labels = [' '.join(item)for item in pred_labels]
new_train.to_feather('new_train')
 new_test.to_feather('new_test' )<feature_engineering>
np.sum(submission["Predicted"] == '' )
seed_value=2509
os.environ['PYTHONHASHSEED']=str(seed_value)
 np.random.seed(seed_value)
 rn.seed(seed_value)
<load_from_csv>
submission[submission["Predicted"] == ''] = '0 25'
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv' )<data_type_conversions>
submission.to_csv('submission.csv', index=False )
combo = [train, test]
for item in combo:
item['Sex'] = item['Sex'].map({'male': 1, 'female': 0} ).astype(int )<feature_engineering>
submission.to_csv('submission.csv', index=False )