Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<train_model>
seed(639)
 set_random_seed(5944)
 seed(639)
 set_random_seed(5944)
 float_data = pd.read_csv(".. /input/train.csv", dtype={"acoustic_data": np.float32, "time_to_failure": np.float32} ).values
def extract_features(z):
return np.c_[z.mean(axis=1),
z.min(axis=1),
z.max(axis=1),
z.std(axis=1)]
def create_X(x, last_index=None, n_steps=150, step_length=1000):
if last_index == None:
last_index=len(x)
 assert last_index - n_steps * step_length >= 0
temp =(x[(last_index - n_steps * step_length):last_index].reshape(n_steps, -1)- 5)/ 3
return np.c_[extract_features(temp),
extract_features(temp[:, -step_length // 10:]),
extract_features(temp[:, -step_length // 100:]),
extract_features(temp[:, -step_length // 300:])]
n_features = create_X(float_data[0:150000] ).shape[1]
print("Our RNN is based on %i features"% n_features)
 def generator(data, min_index=0, max_index=None, batch_size=16, n_steps=150, step_length=1000):
if max_index is None:
max_index = len(data)- 1
while True:
rows = np.random.randint(min_index + n_steps * step_length, max_index, size=batch_size)
 samples = np.zeros(( batch_size, n_steps, n_features))
targets = np.zeros(batch_size,)
 for j, row in enumerate(rows):
samples[j] = create_X(data[:, 0], last_index=row, n_steps=n_steps, step_length=step_length)
 targets[j] = data[row - 1, 1]
yield samples, targets
batch_size = 32
second_earthquake = 50085877
float_data[second_earthquake, 1]
train_gen = generator(float_data, batch_size=batch_size, min_index=second_earthquake + 1)
 valid_gen = generator(float_data, batch_size=batch_size, max_index=second_earthquake )
%%time
model.fit(
 train_dataset,
steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
callbacks=[lr_callback],
epochs=EPOCHS
)<predict_on_test>
cb = [ModelCheckpoint("model.hdf5", save_best_only=True)]
model = Sequential()
model.add(CuDNNLSTM(units=50))
model.add(Dropout(0.2))
model.add(Dense(units=1))
model.summary()
model.compile(optimizer='rmsprop',loss='mae')
 history = model.fit_generator(train_gen,
steps_per_epoch=1000,
epochs=430,
verbose=2,
callbacks=cb,
validation_data=valid_gen,
validation_steps=200 
)
 model = model = load_model("model.hdf5" )
%%time
probs = model.predict(test_dataset )<save_to_csv>
submission = pd.read_csv('.. /input/sample_submission.csv', index_col='seg_id', dtype={"time_to_failure": np.float32})
 x = None
for i, seg_id in enumerate(tqdm(submission.index)) :
seg = pd.read_csv('.. /input/test/' + seg_id + '.csv')
 x = seg['acoustic_data'].values[:]
submission.time_to_failure[i] =(model.predict(np.expand_dims(create_X(x), 0)))
 submission.to_csv('submission.csv' )