Kaggle competition: Statoil/C-CORE Iceberg Classifier Challenge
from keras import backend as K<split>
train = pd.read_json(".. /input/train.json")
 test = pd.read_json(".. /input/test.json")
 train.inc_angle = train.inc_angle.replace('na', 0)
 train.inc_angle = train.inc_angle.astype(float ).fillna(0.0)
 print("done!" )
models = []
for tr_idx, vl_idx in rkf.split(X_train, y_train):
x_tr, y_tr = X_train[tr_idx], y_train[tr_idx]
x_vl, y_vl = X_train[vl_idx], y_train[vl_idx]
model = train_model(x_tr, y_tr, x_vl, y_vl)
 models.append(model )<find_best_params>
x_band1 = np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in train["band_1"]])
 x_band2 = np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in train["band_2"]])
 X_train = np.concatenate([x_band1[:, :, :, np.newaxis]
, x_band2[:, :, :, np.newaxis]
,(( x_band1+x_band1)/2)[:, :, :, np.newaxis]], axis=-1)
 X_angle_train = np.array(train.inc_angle)
 y_train = np.array(train["is_iceberg"])
 x_band1 = np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in test["band_1"]])
 x_band2 = np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in test["band_2"]])
 X_test = np.concatenate([x_band1[:, :, :, np.newaxis]
, x_band2[:, :, :, np.newaxis]
,(( x_band1+x_band1)/2)[:, :, :, np.newaxis]], axis=-1)
 X_angle_test = np.array(test.inc_angle)
 X_train, X_valid, X_angle_train, X_angle_valid, y_train, y_valid = train_test_split(X_train
, X_angle_train, y_train, random_state=123, train_size=0.75 )
env = nflrush.make_env()<predict_on_test>
def get_callbacks(filepath, patience=2):
es = EarlyStopping('val_loss', patience=patience, mode="min")
 msave = ModelCheckpoint(filepath, save_best_only=True)
 return [es, msave]
def get_model() :
bn_model = 0
p_activation = "elu"
input_1 = Input(shape=(75, 75, 3), name="X_1")
 input_2 = Input(shape=[1], name="angle")
 img_1 = Conv2D(16, kernel_size =(3,3), activation=p_activation )(( BatchNormalization(momentum=bn_model))(input_1))
img_1 = Conv2D(16, kernel_size =(3,3), activation=p_activation )(img_1)
 img_1 = MaxPooling2D(( 2,2))(img_1)
 img_1 = Dropout(0.2 )(img_1)
 img_1 = Conv2D(32, kernel_size =(3,3), activation=p_activation )(img_1)
 img_1 = Conv2D(32, kernel_size =(3,3), activation=p_activation )(img_1)
 img_1 = MaxPooling2D(( 2,2))(img_1)
 img_1 = Dropout(0.2 )(img_1)
 img_1 = Conv2D(64, kernel_size =(3,3), activation=p_activation )(img_1)
 img_1 = Conv2D(64, kernel_size =(3,3), activation=p_activation )(img_1)
 img_1 = MaxPooling2D(( 2,2))(img_1)
 img_1 = Dropout(0.2 )(img_1)
 img_1 = Conv2D(128, kernel_size =(3,3), activation=p_activation )(img_1)
 img_1 = MaxPooling2D(( 2,2))(img_1)
 img_1 = Dropout(0.2 )(img_1)
 img_1 = GlobalMaxPooling2D()(img_1)
 img_2 = Conv2D(128, kernel_size =(3,3), activation=p_activation )(( BatchNormalization(momentum=bn_model))(input_1))
img_2 = MaxPooling2D(( 2,2))(img_2)
 img_2 = Dropout(0.2 )(img_2)
 img_2 = GlobalMaxPooling2D()(img_2)
 img_concat =(Concatenate()([img_1, img_2, BatchNormalization(momentum=bn_model )(input_2)]))
dense_ayer = Dropout(0.5 )(BatchNormalization(momentum=bn_model )(Dense(256, activation=p_activation )(img_concat)))
 dense_ayer = Dropout(0.5 )(BatchNormalization(momentum=bn_model )(Dense(64, activation=p_activation )(dense_ayer)))
 output = Dense(1, activation="sigmoid" )(dense_ayer)
 model = Model([input_1,input_2], output)
 optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
 model.compile(loss="binary_crossentropy", optimizer=optimizer, metrics=["accuracy"])
 return model
model = get_model()
model.summary()
for test, sample in tqdm.tqdm(env.iter_test()):
make_pred(test, sample, env, models )<save_to_csv>
file_path = ".model_weights.hdf5"
callbacks = get_callbacks(filepath=file_path, patience=5)
 model = get_model()
model.fit([X_train, X_angle_train], y_train, epochs=25
, validation_data=([X_valid, X_angle_valid], y_valid)
 , batch_size=32
, callbacks=callbacks )
env.write_submission_file()<import_modules>
model.load_weights(filepath=file_path)
 print("Train evaluate:")
 print(model.evaluate([X_train, X_angle_train], y_train, verbose=1, batch_size=200))
print("
print("watch list evaluate:")
 print(model.evaluate([X_valid, X_angle_valid], y_valid, verbose=1, batch_size=200))
import numpy as np
import pandas as pd
from tqdm import tqdm_notebook
import matplotlib.pyplot as plt
from kaggle.competitions import nflrush<load_from_csv>
prediction = model.predict([X_test, X_angle_test], verbose=1, batch_size=200 )
train_df = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2020/train.csv', low_memory=False )<prepare_x_and_y>
submission = pd.DataFrame({'id': test["id"], 'is_iceberg': prediction.reshape(( prediction.shape[0])) })
 submission.head(10 )
y_train = train_df.loc[range(0, 509762, 22), "Yards"]<feature_engineering>
submission.to_csv("./submission.csv", index=False )