Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<set_options>
warnings.filterwarnings("ignore")
 warnings.simplefilter(action='ignore', category=FutureWarning)
%matplotlib inline 
<load_from_csv>
train_X_0 = pd.read_csv(".. /input/lanl-master-s-features-creating-0/train_X_features_865.csv")
 train_X_1 = pd.read_csv(".. /input/lanl-master-s-features-creating-1/train_X_features_865.csv")
 y_0 = pd.read_csv(".. /input/lanl-master-s-features-creating-0/train_y.csv", index_col=False, header=None)
 y_1 = pd.read_csv(".. /input/lanl-master-s-features-creating-1/train_y.csv", index_col=False, header=None )
train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_df = pd.read_csv('.. /input/digit-recognizer/test.csv' )<prepare_x_and_y>
train_X = pd.concat([train_X_0, train_X_1], axis=0)
 train_X = train_X.reset_index(drop=True)
 print(train_X.shape)
 train_X.head()
X = train_df.drop('label',axis=1)
 y = train_df['label']
X = X.values.reshape(-1,28,28,1)
 X = X/255
y = to_categorical(y)
 print(plt.imshow(X[2][:,:,0]))
print(str(y[1]))<split>
train_y = pd.concat([y_0, y_1], axis=0)
 train_y = train_y.reset_index(drop=True)
 train_y.columns = ['time_to_failure']
train_y['time_to_failure'].shape
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=123 )<choose_model_class>
test_X = pd.read_csv(".. /input/lanl-master-s-features-creating-0/test_X_features_10.csv" )
datagen = ImageDataGenerator(zoom_range = 0.1, width_shift_range = 0.1, height_shift_range = 0.1, rotation_range = 10 )<choose_model_class>
scaler = StandardScaler()
train_columns = train_X.columns
train_X[train_columns] = scaler.fit_transform(train_X[train_columns])
 test_X[train_columns] = scaler.transform(test_X[train_columns] )
model = Sequential()<choose_model_class>
h2o.init(max_mem_size='16G' )
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation = 'relu', input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(5, 5), activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides =(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(5, 5), activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides =(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(1024, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation = 'softmax'))<choose_model_class>
train = h2o.H2OFrame(train_X)
 test = h2o.H2OFrame(test_X)
 y_train = h2o.H2OFrame(train_y )
model.compile(optimizer='adam',metrics=['accuracy'],loss='categorical_crossentropy' )<choose_model_class>
train['time_to_failure'] = y_train['time_to_failure']
x = list(train_X.columns)
 y = train_y.columns[0]
reduction_lr = ReduceLROnPlateau(monitor='val_accuracy',patience=2, verbose=1, factor=0.2, min_lr=0.00001 )<train_model>
%%time
nfolds=0
aml = H2OAutoML(max_models=500, seed=555, max_runtime_secs=6000,nfolds=nfolds,sort_metric="MAE")
 aml.train(x=x, y=y, training_frame=train )
hist = model.fit_generator(datagen.flow(X_train,y_train,batch_size=32),epochs=20,validation_data =(X_test,y_test),callbacks=[reduction_lr] )<compute_test_metric>
aml.leader
final_loss, final_acc = model.evaluate(X_test, y_test, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc))<predict_on_test>
preds = aml.predict(test)
 sample_submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 sample_submission['time_to_failure'] = preds.as_data_frame().values.flatten()
sample_submission.to_csv('h2o_submission_fold0.csv', index=False )
test_df = test_df.values.reshape(-1, 28, 28, 1)/ 255
y_pred = model.predict(test_df, batch_size = 64)
 y_pred = np.argmax(y_pred,axis = 1)
 y_pred = pd.Series(y_pred,name="Label")
 y_pred<save_to_csv>
%%time
nfolds=5
aml = H2OAutoML(max_models=500, seed=554, max_runtime_secs=8000,nfolds=nfolds,sort_metric="MAE")
 aml.train(x=x, y=y, training_frame=train )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),y_pred],axis = 1)
 submission.to_csv("submission.csv",index=False )<load_from_zip>
aml.leader
!git clone https://github.com/LenzDu/Kaggle-Competition-Sberbank Utils<load_from_csv>
preds = aml.predict(test)
 sample_submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 sample_submission['time_to_failure'] = preds.as_data_frame().values.flatten()
sample_submission.to_csv('h2o_submission_fold5.csv', index=False )
<import_modules><EOS> 
sample_submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 sample_submission['time_to_failure'] =(pd.read_csv('h2o_submission_fold0.csv')['time_to_failure']+pd.read_csv('h2o_submission_fold5.csv')['time_to_failure'])/2
sample_submission.to_csv('h2o_submission_blend.csv', index=False )