Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<predict_on_test>
warnings.filterwarnings("ignore")
 print(os.listdir(".. /input"))
test_pred = model.predict([test_img])
 test_pred = test_pred.reshape(-1)
 print('Done' )<concatenate>
train_X = pd.read_csv('.. /input/earthquake-data-overlapping4/train_X.csv')
 train_y = pd.read_csv('.. /input/earthquake-data-overlapping4/train_y.csv')
 test_X = pd.read_csv('.. /input/earthquake-data-overlapping4/test_X.csv')
 train_X.head()
img_indicies = np.random.choice(range(len(test_img)) , len(x_train)*1, False)
 test_sub = test_img[img_indicies]
test_pred_sub = test_pred[img_indicies]
Y = np.concatenate(( y_train, test_pred_sub))
print('Done' )<train_model>
scaler = StandardScaler()
scaler.fit(train_X)
 scaled_train_X = pd.DataFrame(scaler.transform(train_X), columns=train_X.columns)
mm = model.fit_generator(gen_flow, validation_data=(x_val, y_val),
steps_per_epoch=len(X)/ 32,
epochs=10,
callbacks = callbacks_list,
verbose = 1,
shuffle=True )<compute_test_metric>
scaled_test_X = pd.DataFrame(scaler.transform(test_X), columns=test_X.columns)
model.load_weights(filepath=filepath)
 test_score = model.evaluate(x_val, y_val,verbose=1)
 print('Test loss:', test_score[0])
 print('Test accuracy:', test_score[1] )<save_to_csv>
train_X_mean = np.mean(scaled_train_X, axis=0)
 train_X_mean.head()
model.load_weights(filepath=filepath)
 test_predictions = model.predict([test_img])
 pred_df = test_df[['id']].copy()
pred_df['is_iceberg'] = test_predictions
pred_df.to_csv('predictions.csv', index = False)
 pred_df.head(3 )<define_variables>
test_X_mean = np.mean(scaled_test_X, axis=0)
 test_X_mean.head()
sub_path = ".. /input/statoil-iceberg-submissions"
all_files = os.listdir(sub_path)
 all_files = all_files[1:3]
all_files.append('submission38.csv')
 all_files<load_from_csv>
c = 0
for _, i in(train_X_mean - test_X_mean ).abs().sort_values().iteritems() :
print(i, _)
 if _ == 'std_roll_std_50':
print(c - 9)
 c += 1
out1 = pd.read_csv(".. /input/statoil-iceberg-submissions/sub_200_ens_densenet.csv", index_col=0)
 out2 = pd.read_csv(".. /input/statoil-iceberg-submissions/sub_TF_keras.csv", index_col=0)
 out3 = pd.read_csv(".. /input/submission38-lb01448/submission38.csv", index_col=0)
 concat_sub = pd.concat([out1, out2, out3], axis=1)
 cols = list(map(lambda x: "is_iceberg_" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub.head() 
<feature_engineering>
features =(train_X_mean - test_X_mean ).abs().sort_values()
features = features[features > 0]
drop_features = features.index[167:]
concat_sub['is_iceberg_max'] = concat_sub.iloc[:, 1:6].max(axis=1)
 concat_sub['is_iceberg_min'] = concat_sub.iloc[:, 1:6].min(axis=1)
 concat_sub['is_iceberg_mean'] = concat_sub.iloc[:, 1:6].mean(axis=1)
 concat_sub['is_iceberg_median'] = concat_sub.iloc[:, 1:6].median(axis=1 )<define_variables>
train_X_less = scaled_train_X.drop(drop_features, axis=1)
 test_X_less = scaled_test_X.drop(drop_features, axis=1 )
cutoff_lo = 0.7
cutoff_hi = 0.3<save_to_csv>
n_fold = 5
def mae_cv(model):
folds = KFold(n_splits=n_fold, shuffle=True, random_state=42 ).get_n_splits(train_X_less.values)
 mae = -cross_val_score(model, train_X_less.values, train_y, scoring="neg_mean_absolute_error",
verbose=0,
cv=folds)
 return mae
<load_from_csv>
%%time
rf_model = RandomForestRegressor(n_estimators=120, n_jobs=-1, min_samples_leaf=1,
max_features = "auto",max_depth=15,)
 ".format(score.mean() , score.std()))
rf_model
sub_base = pd.read_csv('.. /input/submission38-lb01448/submission38.csv' )<feature_engineering>
%%time
params = {'loss_function':'MAE',}
cat_model = CatBoostRegressor(iterations=1000, eval_metric='MAE', verbose=False, **params)
 ".format(score.mean() , score.std()))
cat_model
concat_sub['is_iceberg_base'] = sub_base['is_iceberg']
concat_sub['is_iceberg'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1),
concat_sub['is_iceberg_max'],
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
concat_sub['is_iceberg_min'],
concat_sub['is_iceberg_base']))
concat_sub['is_iceberg'] = np.clip(concat_sub['is_iceberg'].values, 0.001, 0.999)
 concat_sub[['id', 'is_iceberg']].to_csv('submission39.csv',
index=False, float_format='%.6f' )<load_from_disk>
%%time
ENet_model = ElasticNet(alpha=0.0005, l1_ratio=0.9, random_state=3,max_iter=5000)
 ".format(score.mean() , score.std()))
ENet_model
train_df = pd.read_json('.. /input/train.json')
 test_df = pd.read_json('.. /input/test.json')
 train_df.head()<data_type_conversions>
%%time
lasso_model = Lasso(alpha =0.0005, random_state=1)
 ".format(score.mean() , score.std()))
lasso_model
X_band_1=np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in train_df["band_1"]])
 X_band_2=np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in train_df["band_2"]] )<feature_engineering>
class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, models):
self.models = models
def fit(self, X, y):
self.models_ = [clone(x)for x in self.models]
for model in self.models_:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([
model.predict(X)for model in self.models_
])
 return np.mean(predictions, axis=1 )
X_band = np.zeros([1604,75,75,3])
 for t in range(1604):
X_band[t,:,:,0] = X_band_1[t]
X_band[t,:,:,1] = X_band_2[t]
X_band[t,:,:,2] =(X_band_1[t]+X_band_2[t])/2<set_options>
%%time
averaged_models = AveragingModels(models =(rf_model,cat_model))
".format(score_rf_cat.mean() , score_rf_cat.std()))
".format(score_cat_enet.mean() , score_cat_enet.std()))
".format(score_cat_lasso.mean() , score_cat_lasso.std()))
K.set_image_data_format('channels_last')
<define_search_model>
averaged_models.fit(train_X_less.values, train_y)
 averaged_train_predict = averaged_models.predict(train_X_less.values)
 print(mean_absolute_error(train_y, averaged_train_predict))
def Iceberg_model(input_shape):
X_in = Input(input_shape)
 X = Conv2D(16,kernel_size=(5,5),input_shape=(75,75,3))(X_in)
 X = BatchNormalization()(X)
 X = Activation('relu' )(X)
 X = MaxPooling2D(pool_size=(2,2))(X)
 X = Conv2D(32,kernel_size=(5,5))(X)
 X = BatchNormalization()(X)
 X = Activation('relu' )(X)
 X = MaxPooling2D(pool_size=(2,2))(X)
 X = Conv2D(64,kernel_size=(5,5))(X)
 X = BatchNormalization()(X)
 X = Activation('relu' )(X)
 X = MaxPooling2D(pool_size=(2,2))(X)
 X = Flatten()(X)
 X = Dense(128 )(X)
 X = Activation('relu' )(X)
 X = Dense(64 )(X)
 X = Activation('relu' )(X)
 X = Dense(1 )(X)
 X = Activation('sigmoid' )(X)
 model = Model(inputs=X_in,outputs=X,name='Iceberg_model')
 return model<choose_model_class>
averaged_prediction = np.zeros(len(test_X_less))
averaged_prediction += averaged_models.predict(test_X_less.values)
 averaged_prediction
IcebergModel = Iceberg_model(( 75,75,3))<choose_model_class>
submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv', index_col='seg_id')
 submission.head()
<train_model><EOS> 
submission.time_to_failure = averaged_prediction
submission.to_csv('submission_average_167_features.csv',index=True )