Kaggle competition: How Much Did It Rain? II
<SOS> metric: MAE Kaggle data source: how-much-did-it-rain-ii<save_to_csv>
N_FEATURES = 22
THRESHOLD = 73
submission_rfc = pd.DataFrame({
"ID_code": test["ID_code"],
"target": y_pred_rfc
})
 submission_rfc.to_csv('submission_rfc.csv', index=False )<save_to_csv>
train_df = pd.read_csv("/kaggle/input/train.zip")
 train_df[train_df.columns[1:]] = train_df[train_df.columns[1:]].astype(np.float32)
 train_df.shape
submission_tree = pd.DataFrame({
"ID_code": test["ID_code"],
"target": y_pred_tree
})
 submission_tree.to_csv('submission_tree.csv', index=False )<save_to_csv>
good_ids = set(train_df.loc[train_df['Ref'].notna() , 'Id'])
 train_df = train_df[train_df['Id'].isin(good_ids)]
train_df.shape
submission_cat = pd.DataFrame({
"ID_code": test["ID_code"],
"target": y_pred_cat
})
 submission_cat.to_csv('submission_cat.csv', index=False )<save_to_csv>
train_groups = train_df.groupby("Id")
 train_size = len(train_groups )
submission_lgb = pd.DataFrame({
"ID_code": test["ID_code"],
"target": y_pred_lgb
})
 submission_lgb.to_csv('submission_lgb.csv', index=False )<save_to_csv>
X_train = np.zeros(( train_size, MAX_SEQ_LEN, N_FEATURES), dtype=np.float32)
 y_train = np.zeros(train_size, dtype=np.float32)
 i = 0
for _, group in train_groups:
X = group.values
seq_len = X.shape[0]
X_train[i,:seq_len,:] = X[:,1:23]
y_train[i] = X[0,23]
i += 1
del X
del train_groups
X_train.shape, y_train.shape
submission_rfc_cat = pd.DataFrame({
"ID_code": test["ID_code"],
"target":(y_pred_rfc +y_pred_cat)/2
})
 submission_rfc_cat.to_csv('submission_rfc_cat.csv', index=False )<save_to_csv>
test_df = pd.read_csv("/kaggle/input/test.zip")
 test_df[test_df.columns[1:]] = test_df[test_df.columns[1:]].astype(np.float32)
 test_ids = test_df['Id'].unique()
test_df = test_df.reset_index(drop=True)
 test_df = test_df.fillna(0.0 )
submission_lgb_cat = pd.DataFrame({
"ID_code": test["ID_code"],
"target":(y_pred_lgb +y_pred_cat)/2
})
 submission_lgb_cat.to_csv('submission_lgb_cat.csv', index=False )<save_to_csv>
test_groups = test_df.groupby("Id")
 test_size = len(test_groups)
 X_test = np.zeros(( test_size, MAX_SEQ_LEN, N_FEATURES), dtype=np.float32)
 i = 0
for _, group in test_groups:
X = group.values
seq_len = X.shape[0]
X_test[i,:seq_len,:] = X[:,1:23]
i += 1
del X
del test_groups
X_test.shape
submission_rfc_lgb = pd.DataFrame({
"ID_code": test["ID_code"],
"target":(y_pred_rfc +y_pred_lgb)/2
})
 submission_rfc_lgb.to_csv('submission_rfc_lgb.csv', index=False )<set_options>
Input,
Dense,
LSTM,
GlobalAveragePooling1D,
AveragePooling1D,
TimeDistributed,
Flatten,
Bidirectional,
Dropout,
Masking,
Layer,
BatchNormalization 
)
%matplotlib inline
sns.set(color_codes=True)
 np.random.seed(25)
<load_from_csv>
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=5)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, min_delta=0.01 )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
BATCH_SIZE = 1024
N_EPOCHS = 30
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
class NonMasking(Layer):
def __init__(self, **kwargs):
self.supports_masking = True
super(NonMasking, self ).__init__(**kwargs)
 def build(self, input_shape):
input_shape = input_shape
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
return x
def get_output_shape_for(self, input_shape):
return input_shape
target = train['target']<count_missing_values>
def get_model_deep(shape=(MAX_SEQ_LEN, N_FEATURES)) :
inp = Input(shape)
 x = Dense(16 )(inp)
 x = BatchNormalization()(x)
 x = Masking(mask_value=0.)(x)
 x = Bidirectional(LSTM(64, return_sequences=True))(x)
 x = BatchNormalization()(x)
 x = TimeDistributed(Dense(64))(x)
 x = BatchNormalization()(x)
 x = Bidirectional(LSTM(128, return_sequences=True))(x)
 x = BatchNormalization()(x)
 x = TimeDistributed(Dense(1))(x)
 x = BatchNormalization()(x)
 x = NonMasking()(x)
 x = Flatten()(x)
 x = Dropout(0.5 )(x)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model
train.isnull().sum()<count_missing_values>
model = get_model_deep()
model.compile(optimizer=Nadam(lr=0.001), loss='mae')
 model.summary()
train.isnull().sum()<count_duplicates>
history = model.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[early_stopping, reduce_lr] )
print(any(train.duplicated()))<sort_values>
y_pred = model.predict(X_test, batch_size=BATCH_SIZE)
 submission = pd.DataFrame({'Id': test_ids, 'Expected': y_pred.reshape(-1)})
 submission.to_csv('submission.csv', index=False )