Kaggle competition: Human Protein Atlas Image Classification
<SOS> metric: MacroFScore Kaggle data source: human-protein-atlas-image-classification<drop_column>
warnings.filterwarnings("ignore" )
sel_train = tr_train.drop('LowQualFinSF', axis=1)
 sel_test = tr_test.drop('LowQualFinSF', axis=1)
 sel_train.drop('TotRmsAbvGrd', axis=1, inplace=True)
 sel_test.drop('TotRmsAbvGrd', axis=1, inplace=True)
 sel_train.drop('RoofStyle', axis=1, inplace=True)
 sel_test.drop('RoofStyle', axis=1, inplace=True)
 sel_train.drop('Heating', axis=1, inplace=True)
 sel_test.drop('Heating', axis=1, inplace=True)
 sel_train.drop('MoSold', axis=1, inplace=True)
 sel_test.drop('MoSold', axis=1, inplace=True)
 sel_train.drop('Electrical', axis=1, inplace=True)
 sel_test.drop('Electrical', axis=1, inplace=True)
 sel_train.drop('YrSold', axis=1, inplace=True)
 sel_test.drop('YrSold', axis=1, inplace=True)
 sel_train.drop('RoofMatl', axis=1, inplace=True)
 sel_test.drop('RoofMatl', axis=1, inplace=True)
 sel_train.drop('BsmtFinSF1', axis=1, inplace=True)
 sel_test.drop('BsmtFinSF1', axis=1, inplace=True)
 sel_train.drop('2ndFlrSF', axis=1, inplace=True)
 sel_test.drop('2ndFlrSF', axis=1, inplace=True )<drop_column>
path_to_train = '.. /input/train/'
data = pd.read_csv('.. /input/train.csv')
 train_dataset_info = []
for name, labels in zip(data['Id'], data['Target'].str.split(' ')) :
train_dataset_info.append({
'path':os.path.join(path_to_train, name),
'labels':np.array([int(label)for label in labels])})
 train_dataset_info = np.array(train_dataset_info )
sel_train.drop('BsmtFinSF2', axis=1, inplace=True)
 sel_test.drop('BsmtFinSF2', axis=1, inplace=True)
 sel_train.drop('LandSlope', axis=1, inplace=True)
 sel_test.drop('LandSlope', axis=1, inplace=True )<categorify>
class data_generator:
def create_train(dataset_info, batch_size, shape, augument=True):
while True:
random_indexes = np.random.choice(len(dataset_info), batch_size)
 batch_images = np.empty(( batch_size, shape[0], shape[1], shape[2]))
batch_labels = np.zeros(( batch_size, 28))
for i, idx in enumerate(random_indexes):
image = data_generator.load_image(
 dataset_info[idx]['path'], shape)
 if augument:
image = data_generator.augment(image)
 batch_images[i] = image
batch_labels[i][dataset_info[idx]['labels']] = 1
yield batch_images, batch_labels
def load_image(path, shape):
image_red_ch = skimage.io.imread(path+'_red.png')/255.0
image_yellow_ch = skimage.io.imread(path+'_yellow.png')/255.0
image_green_ch = skimage.io.imread(path+'_green.png')/255.0
image_blue_ch = skimage.io.imread(path+'_blue.png')/255.0
image_red_ch +=(image_yellow_ch/2 ).astype(np.uint8)
 image_blue_ch +=(image_yellow_ch/2 ).astype(np.uint8)
 image = np.stack((
image_red_ch,
image_green_ch,
image_blue_ch 
), -1)
 image = resize(image,(shape[0], shape[1]), mode='reflect')
 return image
def augment(image):
augment_img = iaa.Sequential([
iaa.OneOf([
iaa.Affine(rotate=0),
iaa.Affine(rotate=90),
iaa.Affine(rotate=180),
iaa.Affine(rotate=270),
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
])], random_order=True)
 image_aug = augment_img.augment_image(image)
 return image_aug
butch_train = sel_train[[col for col in sel_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = sel_test[[col for col in sel_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 print(set(df_train.columns)- set(df_test.columns))
butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis=1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
target = np.log1p(df_train['SalePrice'])
(butch_train.columns != butch_test.columns ).sum()<choose_model_class>
input_shape =(256,256,3)
 train_datagen = data_generator.create_train(
 train_dataset_info, 5, input_shape, augument=True )
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<choose_model_class>
def f1(y_true, y_pred):

 def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol<define_search_space>
def f1_loss(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f1 = 2*p*r /(p+r+K.epsilon())
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return 1-K.mean(f1 )
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )<compute_train_metric>
def reduce(x):
return K.argmax(x, axis=1)
 def cast(x):
return K.cast(x, 'float32')
 def create_model(input_shape, n_out):
inp = Input(input_shape)
 pretrain_model = MobileNetV2(include_top=False, weights=None, input_tensor=inp)
 x = pretrain_model.output
x = GlobalAveragePooling2D()(x)
 x = Dropout(0.5 )(x)
 x = Dense(n_out, activation="relu" )(x)
 for layer in pretrain_model.layers:
layer.trainable = True
return Model(inp, x )
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol<compute_test_metric>
epochs = 5; batch_size = 64
checkpointer = ModelCheckpoint('.. /working/InceptionResNetV2.model', verbose=2, save_best_only=True)
 early_stopping = EarlyStopping(monitor='val_loss', patience=2)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=1, factor=0.1)
 np.random.seed(2018)
 indexes = np.arange(train_dataset_info.shape[0])
 np.random.shuffle(indexes)
 train_indexes = indexes[:27500]
valid_indexes = indexes[27500:]
train_steps = len(train_indexes)//batch_size
valid_steps = len(valid_indexes)//batch_size
train_generator = data_generator.create_train(train_dataset_info[train_indexes], batch_size, input_shape, augument=True)
 validation_generator = data_generator.create_train(train_dataset_info[valid_indexes], 100, input_shape, augument=False)
 history = model.fit_generator(
 train_generator,
steps_per_epoch=train_steps,
validation_data=next(validation_generator),
validation_steps=valid_steps,
epochs=epochs,
verbose=1,
callbacks=[checkpointer, reduce_lr] )
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )<find_best_params>
submit = pd.read_csv('.. /input/sample_submission.csv')
 model = load_model(".. /working/InceptionResNetV2.model" )
modelname.append('Feat_Sel_EDA')
 lassoscore.append(0.1099)
 ridgescore.append(0.1107)
 kaggle_lasso.append(0.11953)
 kaggle_ridge.append(0.11861)
 timeelapsed.append(18000)
 maecv_lasso.append(13161.658)
 maecv_ridge.append(13273.673 )<feature_engineering>
%%time
predicted = []
for name in tqdm(submit['Id']):
path = os.path.join('.. /input/test/', name)
 image = data_generator.load_image(path, input_shape)
 score_predict = model.predict(image[np.newaxis])[0]
label_predict = np.arange(28)[score_predict>=0.5]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted.append(str_predict_label )
<feature_engineering><EOS> 
submit['Predicted'] = predicted
submit.to_csv('submission.csv', index=False )