Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<count_unique_values>
seed(639)
 set_random_seed(5944)
target['group_id'].nunique()<groupby>
%time
float_data = pd.read_csv(".. /input/train.csv", dtype={"acoustic_data": np.float32, "time_to_failure": np.float32} ).values
group27 = target[ target.group_id == 27]
print(group27 )<count_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:])]
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
target.group_id.value_counts()<merge>
batch_size = 32
second_earthquake = 50085877
float_data[second_earthquake, 1]
train_gen = generator(float_data, batch_size=batch_size)
 valid_gen = generator(float_data, batch_size=batch_size, max_index=second_earthquake)
train_group = pd.merge(tr,target, on = 'series_id', how = 'left')
 train_group[:5]<set_options>
n_steps = 100
cb = [ModelCheckpoint("model.hdf5", save_best_only=True, period=3)]
model = Sequential()
model.add(CuDNNGRU(48, input_shape=(None, n_features)))
 model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(1))
model.summary()
del serie1
gc.collect()<normalization>
model.compile(optimizer=adam(lr=0.001), loss="mae")
 hist = model.fit_generator(train_gen,
steps_per_epoch=1000,
epochs=30,
verbose=0,
callbacks=cb,
shuffle=True,
validation_data=valid_gen,
validation_steps=200 )
<feature_engineering><EOS> 
submission = pd.read_csv('.. /input/sample_submission.csv', index_col='seg_id', dtype={"time_to_failure": np.float32})
 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.head()
submission.to_csv('submission.csv' )