Kaggle competition: LANL Earthquake Prediction
categorical = ["display_address", "manager_id", "building_id", "street_address"]
for f in categorical:
if train_df[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(train_df[f].values)+ list(test_df[f].values))
train_df[f] = lbl.transform(list(train_df[f].values))
test_df[f] = lbl.transform(list(test_df[f].values))
features_to_use.append(f )<feature_engineering>
samp_per_second = 31
ttf_array = np.concatenate([np.linspace(11, 0, samp_per_second * 11),
np.linspace(11, 0, samp_per_second * 11),
np.linspace(11, 0, samp_per_second * 11),
np.linspace(5, 0, samp_per_second * 5),
np.linspace(10, 0, samp_per_second * 10),
np.linspace(15, 0, samp_per_second * 15),
np.linspace(7, 0, samp_per_second * 7),
np.linspace(15, 10, samp_per_second * 5)])
 ss = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 ss_simulated = pd.DataFrame(ttf_array, columns=['time_to_failure'])
 ss_simulated.plot(figsize=(15, 5))
train_df['features'] = train_df["features"].apply(lambda x: " ".join(["_".join(i.split(" ")) for i in x]))
test_df['features'] = test_df["features"].apply(lambda x: " ".join(["_".join(i.split(" ")) for i in x]))
print(train_df["features"].head())
 tfidf = CountVectorizer(stop_words='english', max_features=200)
 tr_sparse = tfidf.fit_transform(train_df["features"])
 te_sparse = tfidf.transform(test_df["features"] )<prepare_x_and_y>
sub1352 = pd.read_csv('.. /input/lanl-submissions/LB1.352_submission_147.csv')
 sub1389 = pd.read_csv('.. /input/lanl-submissions/LB1.389_submissionverson_one.csv')
 sub1362 = pd.read_csv('.. /input/lanl-submissions/LB1.362_submission_147.csv')
 sub1371 = pd.read_csv('.. /input/lanl-submissions/LB1.371_submission_137.csv')
 sub1380 = pd.read_csv('.. /input/lanl-submissions/LB1.380_submission.csv')
 sub1381 = pd.read_csv('.. /input/lanl-submissions/LB1.381_submission_147.csv')
 sub1389 = pd.read_csv('.. /input/lanl-submissions/LB1.389_submissionverson_one.csv')
 sub_50feats = pd.read_csv('.. /input/lanl-submissions/submission_50feats_peaks 9000ks.csv')
 ss['sub1.352'] = sub1352['time_to_failure']
ss['sub1.389'] = sub1389['time_to_failure']
ss['sub1.362'] = sub1362['time_to_failure']
ss['sub1.371'] = sub1371['time_to_failure']
ss['sub1.380'] = sub1380['time_to_failure']
ss['sub1.381'] = sub1381['time_to_failure']
ss['sub1.389'] = sub1389['time_to_failure']
ss['simulated'] = ss_simulated['time_to_failure']
train_X = sparse.hstack([train_df[features_to_use], tr_sparse] ).tocsr()
test_X = sparse.hstack([test_df[features_to_use], te_sparse] ).tocsr()
target_num_map = {'high':0, 'medium':1, 'low':2}
train_y = np.array(train_df['interest_level'].apply(lambda x: target_num_map[x]))
print(train_X.shape, test_X.shape )<compute_train_metric>
sub1389.agg(['mean','median'] )
cv_scores = []
kf = model_selection.KFold(n_splits=5, shuffle=True, random_state=2016)
 for dev_index, val_index in kf.split(range(train_X.shape[0])) :
dev_X, val_X = train_X[dev_index,:], train_X[val_index,:]
dev_y, val_y = train_y[dev_index], train_y[val_index]
preds, model = runXGB(dev_X, dev_y, val_X, val_y)
 cv_scores.append(log_loss(val_y, preds))
print(cv_scores)
 break<save_to_csv>
sub1350_2037CV = pd.read_csv('.. /input/last-10-subs-data/1350_submission_lgb_FINALSHUFFLED_2.037CV_123Feats_24000Rows.csv')
 sub1355_2038CV = pd.read_csv('.. /input/last-10-subs-data/1355_submission_lgb_FINALSHUFFLED_2.038CV_119Feats_24000Rows.csv')
 sub1383_1991CV = pd.read_csv('.. /input/last-10-subs-data/1383_submission_lgb_FINALSHUFFLED_1.991CV_120Feats_24000Rows.csv')
 sub1388_2086CV = pd.read_csv('.. /input/last-10-subs-data/1398_submission_lgb_FINALSHUFFLED_2.086CV_138Feats_24000Rows.csv')
 sub1404_2009CV = pd.read_csv('.. /input/last-10-subs-data/1404_submission_lgb_FINALSHUFFLED_2.009CV_116Feats_24000Rows.csv')
 sub1432_xgb = pd.read_csv('.. /input/last-10-subs-data/1432_submission_xgb_FINALSHUFFLED.csv')
 sub1529_xgb = pd.read_csv('.. /input/last-10-subs-data/1529_submission_xgb_FINALSHUFFLED.csv')
 sub1532_1948CV = pd.read_csv('.. /input/last-10-subs-data/1532_submission_lgb_1.948CV_110Feats_24000Rows_15Fold.csv')
 sub1534_2092CV = pd.read_csv('.. /input/last-10-subs-data/1534_submission_lgb_FINALSHUFFLED_2.092CV_24Feats_24000Rows.csv')
 sub1863_2283CV = pd.read_csv('.. /input/last-10-subs-data/1863_submission_lgb_FINALSHUFFLED_2.283CV_17Feats_24000Rows.csv')
 ss['sub1350_2037CV'] = sub1350_2037CV['time_to_failure']
ss['sub1355_2038CV'] = sub1355_2038CV['time_to_failure']
ss['sub1383_1991CV'] = sub1383_1991CV['time_to_failure']
ss['sub1388_2086CV'] = sub1388_2086CV['time_to_failure']
ss['sub1404_2009CV'] = sub1404_2009CV['time_to_failure']
ss['sub1432_xgb'] = sub1432_xgb['time_to_failure']
ss['sub1529_xgb'] = sub1529_xgb['time_to_failure']
ss['sub1532_1948CV'] = sub1532_1948CV['time_to_failure']
ss['sub1534_2092CV'] = sub1534_2092CV['time_to_failure']
ss['sub1863_2283CV'] = sub1863_2283CV['time_to_failure']
preds, model = runXGB(train_X, train_y, test_X, num_rounds=940)
 out_df = pd.DataFrame(preds)
 out_df.columns = ["high", "medium", "low"]
out_df["listing_id"] = test_df.listing_id.values
out_df.to_csv("xgb_starter4.csv", index=False )<set_options>
submit = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv' )
%matplotlib inline<define_variables>
submit['time_to_failure'] = ss[['sub1.380','sub1.381','sub1529_xgb','sub1532_1948CV','sub1432_xgb']].mean(axis=1 )
<define_variables><EOS> 
submit.to_csv('blend_5_including2xgb.csv', index=False )