Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<load_pretrained>
seed(639)
 set_random_seed(5944)
def imread(path, fast_mode=False):
img = cv2.imread(path)
 if not fast_mode and img is not None and len(img.shape)== 3:
img = np.array(img[:, :, ::-1])
 return img
img = imread(PATH + 'train_images/ID_8a6e65317' + '.jpg')
 IMG_SHAPE = img.shape 
<categorify>
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 str2coords(s, names=['id', 'yaw', 'pitch', 'roll', 'x', 'y', 'z']):

 coords = []
for l in np.array(s.split() ).reshape([-1, 7]):
coords.append(dict(zip(names, l.astype('float'))))
if 'id' in coords[-1]:
coords[-1]['id'] = int(coords[-1]['id'])
 return coords<train_model>
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 )
inp = train['PredictionString'][0]
print('Example input:
', inp)
 print()
print('Output:
', str2coords(inp))<normalization>
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
def rotate(x, angle):
x = x + angle
x = x -(x + np.pi)//(2 * np.pi)* 2 * np.pi
return x 
<categorify>
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)
 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(1))
model.compile(optimizer=adam(lr=0.0005), loss="mae")
 model.summary()
plot_model(model )
<prepare_x_and_y><EOS> 
history = model.fit_generator(train_gen,
steps_per_epoch=1000,
epochs=30,
verbose=0,
callbacks=cb,
validation_data=valid_gen,
validation_steps=200 )