Kaggle competition: LANL Earthquake Prediction
%%time
ids = sub_tabnet[['sig_id']]
sub_tabnet.drop('sig_id',inplace=True,axis=1)
 sub_resnet.drop('sig_id',inplace=True,axis=1)
 sub_smooth.drop('sig_id',inplace=True,axis=1)
 sub_xgb.drop('sig_id',inplace=True,axis=1)
 sub_gauss.drop('sig_id',inplace=True,axis=1)
 sub_en = 0.3*sub_gauss + 0.3*sub_tabnet + 0.25*sub_resnet + 0.1*sub_smooth +0.05*sub_xgb
sub_en['sig_id'] = ids[['sig_id']]
cols = sub_en.columns.tolist()
cols.insert(0, cols.pop(cols.index('sig_id')))
 sub_en = sub_en.reindex(columns=cols)
 sub_en.to_csv('submission.csv',index=False)
 sub_en<import_modules>
seed(639)
 set_random_seed(5944)
 float_data = pd.read_csv(".. /input/train.csv", nrows=300000, 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_Y(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
chunk = x[(last_index - n_steps * step_length):last_index]
X =(chunk.T[0].reshape(n_steps, step_length)- 4.5)/ 10.7
Y = chunk.T[1].min()
return np.c_[extract_features(X),
extract_features(X[:, -step_length // 10:]),
extract_features(X[:, -step_length // 100:])],Y
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
chunk = x[(last_index - n_steps * step_length):last_index]
X =(chunk.reshape(n_steps, step_length)- 4.5)/ 10.7
return np.c_[extract_features(X),
extract_features(X[:, -step_length // 10:]),
extract_features(X[:, -step_length // 100:])]
<install_modules>
features,targets = create_X_Y(float_data[0:150000])
 n_features = features.shape[1]
print("Our RNN is based on %i features"% n_features )
!pip install.. /input/pytorchtabnetpretraining/pytorch_tabnet-2.0.1-py3-none-any.whl
!pip install.. /input/iterative-stratification/iterative-stratification-master/
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<feature_engineering>
n_train = 5000
float_data = pd.read_csv(".. /input/train.csv", nrows=n_train*150000, dtype={"acoustic_data": np.float32, "time_to_failure": np.float32} ).values
os.environ["CUDA_LAUNCH_BLOCKING"] = "1" 
<set_options>
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],targets[j] = create_X_Y(data, last_index=row, n_steps=n_steps, step_length=step_length)
 yield samples, targets
batch_size = 64
second_earthquake = 50085877
tv_bd = 3800 * 150000
train_gen = generator(float_data, max_index = 4000 * 150000, batch_size=batch_size,n_steps=300,step_length=500)
 valid_gen = generator(float_data, min_index= 3800 * 150000,batch_size=batch_size,n_steps=300, step_length=500 )
def seed_everything(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed_everything(88 )<load_from_csv>
cb = [ModelCheckpoint("model.hdf5", save_best_only=True, period=3)]
model = Sequential()
model.add(CuDNNLSTM(64,return_sequences=True,input_shape=(None, n_features)))
 model.add(CuDNNGRU(48))
model.add(Dense(16,activation="relu"))
model.add(Dense(1))
model.summary()
model.compile(optimizer=RMSprop(lr=0.0005), loss="mae")
 history = model.fit_generator(train_gen,
steps_per_epoch=1000,
epochs=100,
verbose=2,
callbacks=cb,
validation_data=valid_gen,
validation_steps=200)