Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<load_pretrained>
warnings.filterwarnings("ignore")
 warnings.simplefilter(action='ignore', category=FutureWarning)
def load_imfeatures(folder):
path = PurePath(folder)
 features = sparse.load_npz(str(path / 'features.npz'))
if toy:
features = features[:100000]
return features<feature_engineering>
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 )
ftrain = load_imfeatures('.. /input/vgg16-train-features/' )<feature_engineering>
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()
ftest = load_imfeatures('.. /input/vgg16-test-features/' )<concatenate>
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
df_target = df_train[target_col]
df_both = pd.concat([df_train, df_test])
 del df_train, df_test
gc.collect() ;<concatenate>
test_X = pd.read_csv(".. /input/lanl-master-s-features-creating-0/test_X_features_10.csv" )
fboth = sparse.vstack([ftrain, ftest])
 del ftrain, ftest
gc.collect()
fboth.shape<feature_engineering>
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] )
df_both['im_max_feature'] = fboth.argmax(axis=1)
 df_both['im_min_feature'] = fboth.argmin(axis=1)
 df_both['im_n_features'] = fboth.getnnz(axis=1)
 df_both['im_mean_features'] = fboth.mean(axis=1)
 df_both['im_meansquare_features'] = fboth.power(2 ).mean(axis=1 )<normalization>
h2o.init(max_mem_size='16G' )
%%time
tsvd = TruncatedSVD(32)
 ftsvd = tsvd.fit_transform(fboth)
 del fboth
gc.collect()<data_type_conversions>
train = h2o.H2OFrame(train_X)
 test = h2o.H2OFrame(test_X)
 y_train = h2o.H2OFrame(train_y )
for col, dtype in dtypes.items() :
if dtype == 'category':
df_both[col] = df_both[col].astype('category' ).cat.codes<merge>
train['time_to_failure'] = y_train['time_to_failure']
x = list(train_X.columns)
 y = train_y.columns[0]
cat_cols = ['region', 'city', 'parent_category_name', 'category_name', 'param_1', 'param_2', 'param_3', 'user_type',
'im_max_feature', 'im_min_feature']
num_cols = ['price', 'image_top_1', 'deal_probability']
for cat_col in cat_cols:
df_group = df_both.groupby(cat_col)[num_cols].agg(['mean', 'std'])
 df_group.columns = ['{}_'.format(cat_col)+ '-'.join(col ).strip() for col in df_group.columns.values]
df_both = df_both.join(df_group, on=cat_col, how='left')
 del df_group
print(cat_col)
<feature_engineering>
%%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 )
df_both['activation_dow'] = df_both['activation_date'].dt.dayofweek<feature_engineering>
aml.leader
sw = stopwords.words('russian')
 txt_cols = ['title', 'description']
for txt_col in txt_cols:
df_both[txt_col + '_len'] = df_both[txt_col].str.len()
df_both[txt_col + '_wc'] = df_both[txt_col].str.count(' ')
 if txt_col != 'description':
feature_cnt = 50
tfidf = TfidfVectorizer(stop_words=sw, min_df=10, max_df=0.8, dtype=np.float32, max_features=32000)
 X_text = tfidf.fit_transform(df_both[txt_col])
 svr = LinearSVR(C=0.01 ).fit(X_text[:n_train], df_target)
 fnames = sorted(tfidf.vocabulary_, key=tfidf.vocabulary_.__getitem__)
 best_features = np.argsort(svr.coef_)
 selected_features = np.concatenate([best_features[:feature_cnt], best_features[-feature_cnt:]])
 features_names = list(map(fnames.__getitem__, selected_features))
df_both_tfidf = pd.DataFrame(X_text[:, selected_features].todense() , columns=features_names, index=df_both.index)
 del X_text, features_names, selected_features, best_features, svr, fnames, tfidf
df_both = df_both.join(df_both_tfidf)
 del df_both_tfidf
gc.collect()
print(txt_col )<create_dataframe>
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 )
df_ftsvd = pd.DataFrame(ftsvd, index=df_both.index ).add_prefix('im_tsvd_')
 df_both = pd.concat([df_both, df_ftsvd], axis=1)
 del df_ftsvd, ftsvd
gc.collect() ;<split>
%%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 )
df_train = df_both.iloc[:n_train]
df_test = df_both.iloc[n_train:]
del df_both
gc.collect()
df_train.shape, df_test.shape<define_variables>
aml.leader
ex_cols = {'item_id', 'user_id', 'deal_probability', 'title', 'description', 'image', 'activation_date'}
used_cols = [c for c in df_train.columns if c not in ex_cols]
print('Used cols:', ', '.join(used_cols))<split>
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 )
if validate:
fold_train, fold_valid, target_train, target_valid = train_test_split(df_train[used_cols], df_target, test_size=0.2, random_state=42)
 dtrain = lgb.Dataset(fold_train, target_train, categorical_feature=cat_cols)
 dvalid = lgb.Dataset(fold_valid, target_valid, categorical_feature=cat_cols)
 valid_sets = [dvalid]
valid_names = ['valid']
del fold_train, fold_valid, target_train, target_valid
else:
dtrain = lgb.Dataset(df_train[used_cols], df_target)
 valid_sets = [dtrain]
valid_names = ['train']
assert best_num_boost_round is not None<train_model>
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 )