Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<load_from_csv>
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)
 valid_gen = generator(float_data, batch_size=batch_size, max_index=second_earthquake )
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 train_drug = pd.read_csv('.. /input/lish-moa/train_drug.csv')
 print(train_drug.shape )<categorify>
cb = [ModelCheckpoint("model.hdf5", save_best_only=True, period=3)]
model = Sequential()
model.add(CuDNNLSTM(units=50, return_sequences=True, input_shape=(None,n_features)))
 model.add(Dropout(0.2))
model.add(CuDNNLSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(CuDNNLSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
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=32,
verbose=2,
callbacks=cb,
validation_data=valid_gen,
validation_steps=200 
)
def rank_gauss(data):
epsilon = 1e-6
for k in tqdm(GENES + CELLS):
r_cpu = data.loc[:,k].argsort().argsort()
r_cpu =(r_cpu/r_cpu.max() -0.5)*2
r_cpu = np.clip(r_cpu,-1+epsilon,1-epsilon)
 r_cpu = sp_erfinv(r_cpu)
 data.loc[:,k] = r_cpu * np.sqrt(2)
 return data
class DenoisingAutoEncoder(nn.Module):
def __init__(self, in_out_channels, hidden_channels):
super(DenoisingAutoEncoder, self ).__init__()
self.encoder=nn.Sequential(
 nn.Linear(in_out_channels, hidden_channels),
nn.ReLU(True),
nn.Linear(hidden_channels, hidden_channels//2),
nn.ReLU(True),
nn.Linear(hidden_channels//2, hidden_channels//4),
nn.ReLU(True)
)
 self.decoder=nn.Sequential(
 nn.Linear(hidden_channels//4, hidden_channels//2),
nn.ReLU(True),
nn.Linear(hidden_channels//2, hidden_channels),
nn.ReLU(True),
nn.Linear(hidden_channels, in_out_channels), 
)
 def forward(self,x):
x=self.encoder(x)
 x=self.decoder(x)
 return x<sort_values>
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' )