Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<count_values>
warnings.filterwarnings("ignore")
 PandasDataFrame = TypeVar('pandas.core.frame.DataFrame')
def get_num_files(path):

 if not os.path.exists(path):
return 0
return sum([len(files)for r, d, files in os.walk(path)] )<define_variables>
IS_LOCAL = False
if(IS_LOCAL):
PATH=".. /input/LANL/"
else:
PATH=".. /input/"
os.listdir(PATH)
 train_X = pd.read_csv(os.path.join(PATH,'.. /input/basic-features-randomized/scaled_train_X_rand_shuffle.csv'))
train_X = train_X.drop(train_X.columns[0], axis=1)
 train_y = pd.read_csv(os.path.join(PATH,'.. /input/basic-features-randomized/train_y_rand2_shuffle.csv'))
train_y = train_y.drop(train_y.columns[0], axis=1)
 train_X.head()
train_dir = '/kaggle/input/dog-breed-identification/train'
test_dir = '/kaggle/input/dog-breed-identification/test'
data_size = get_num_files(train_dir)
 test_size = get_num_files(test_dir)
 print('Data samples size: ', data_size)
 print('Test samples size: ', test_size )<load_from_csv>
submission = pd.read_csv(os.path.join(PATH,'LANL-Earthquake-Prediction/sample_submission.csv'), index_col='seg_id')
 test_X = pd.read_csv(os.path.join(PATH,'lanlfeatures198/feature_extraction_ds_test.csv'), index_col='seg_id')
 submission.shape, test_X.shape
labels_dataframe = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')
 sample_df = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')
 labels_dataframe.head(5 )<define_variables>

 def fold_generator(x, y, groups=None, num_folds=10, shuffle=True, seed=2019):
folds = KFold(num_folds, shuffle=shuffle, random_state=seed)
 for train_index, test_index in folds.split(x, y, groups):
yield train_index, test_index
class_to_num = dict(zip(dog_breeds, range(n_classes)) )<prepare_x_and_y>

 def search_cv(x, y, pipeline, grid, max_iter=None, num_folds=10, shuffle=True):
t0 = time.time()
cv = fold_generator(x, y, num_folds=num_folds)
 if max_iter is None:
search = GridSearchCV(pipeline, grid, cv=cv,
scoring='neg_mean_absolute_error')
 else:
search = RandomizedSearchCV(pipeline, grid, n_iter=max_iter, cv=cv,
scoring='neg_mean_absolute_error')
 search.fit(x, y)
 t0 = time.time() - t0
print("Best CV score: {:.4f}, time: {:.1f}s".format(-search.best_score_, t0))
print(search.best_params_)
 return search.best_estimator_
def images_to_array(data_dir, labels_dataframe, img_size =(224,224,3)) :

 images_names = labels_dataframe['id']
images_labels = labels_dataframe['breed']
data_size = len(images_names)
 X = np.zeros([data_size, img_size[0], img_size[1], img_size[2]], dtype=np.uint8)
 y = np.zeros([data_size,1], dtype=np.uint8)
 for i in tqdm(range(data_size)) :
image_name = images_names[i]
img_dir = os.path.join(data_dir, image_name+'.jpg')
 img_pixels = load_img(img_dir, target_size=img_size)
 X[i] = img_pixels
image_breed = images_labels[i]
y[i] = class_to_num[image_breed]
y = to_categorical(y)
 ind = np.random.permutation(data_size)
 X = X[ind]
y = y[ind]
print('Ouptut Data Size: ', X.shape)
 print('Ouptut Label Size: ', y.shape)
 return X, y<prepare_x_and_y>

 def make_predictions(x, y, pipeline, num_folds=10, shuffle=True, test=None, plot=True):
if test is not None:
sub_prediction = np.zeros(test.shape[0])
 oof_prediction = np.zeros(x.shape[0])
 for tr_idx, val_idx in fold_generator(x, y, num_folds=num_folds):
pipeline.fit(x.iloc[tr_idx], y.iloc[tr_idx])
 oof_prediction[val_idx] = pipeline.predict(x.iloc[val_idx])
 if test is not None:
sub_prediction += pipeline.predict(test)/ num_folds
if plot:
plot_predictions(y, oof_prediction)
 if test is None:
return oof_prediction
else:
return oof_prediction, sub_prediction
img_size =(331,331,3)
 X, y = images_to_array(train_dir, labels_dataframe, img_size )<feature_engineering>
grid = {
'model__max_depth': [4, 6, 8, 10, 12],
'model__max_features': ['auto', 'sqrt', 'log2'],
'model__min_samples_leaf': [2, 4, 8, 12, 14, 16, 20],
'model__min_samples_split': [2, 4, 6, 8, 12, 16, 20],
}
rf_pipe = make_pipeline(RandomForestRegressor(criterion='mae', n_estimators=50))
rf_pipe = search_cv(train_X, train_y, rf_pipe, grid, max_iter=10)
 rf_oof = make_predictions(train_X, train_y, rf_pipe )
inception_preprocessor = preprocess_input
inception_features = get_features(InceptionV3,
inception_preprocessor,
img_size, X )<feature_engineering>
rf_oof, rf_sub = make_predictions(train_X, train_y, rf_pipe,
test=test_X, plot=False )
xception_preprocessor = preprocess_input
xception_features = get_features(Xception,
xception_preprocessor,
img_size, X )<feature_engineering>
submission.time_to_failure = rf_sub
submission.to_csv('submission.csv',index=True )