Kaggle competition: LANL Earthquake Prediction
<compute_test_metric>
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.preprocessing import StandardScaler
from sklearn.svm import NuSVR
from sklearn.kernel_ridge import KernelRidge
from sklearn.metrics import mean_absolute_error, make_scorer
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold
def print_score(m):
res = [ m.score(X_train, y_train), m.score(X_valid, y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )<drop_column>
train = pd.read_csv('.. /input/train.csv', dtype={'acoustic_data': np.int16, 'time_to_failure': np.float64} )
<define_variables>
pd.options.display.precision = 15
dep_var = 'surface'
cont_vars = list(df_test.columns)
 procs = [FillMissing]<categorify>
rows = 150_000
shift_step = int(np.floor(rows / 2))
segments = int(np.floor(train.shape[0] / rows))
segments_augmented = 2*segments - 1
X_train = pd.DataFrame(index=range(segments_augmented), dtype=np.float64,
columns=['ave', 'std', 'max', 'min','q95','q99', 'q05','q01'])
 y_train = pd.DataFrame(index=range(segments_augmented), dtype=np.float64,
columns=['time_to_failure'])
 for segment in tqdm(range(segments)) :
for do_shift in [False,True]:
if(do_shift):
shift = shift_step
idx = segments + segment
if(segment==segments-1):
continue
else:
shift = 0
idx = segment
seg = train.iloc[segment*rows+shift:segment*rows+shift+rows]
x = seg['acoustic_data'].values
y = seg['time_to_failure'].values[-1]
y_train.loc[idx, 'time_to_failure'] = y
X_train.loc[idx, 'ave'] = x.mean()
X_train.loc[idx, 'std'] = x.std()
X_train.loc[idx, 'max'] = x.max()
X_train.loc[idx, 'min'] = x.min()
X_train.loc[idx, 'q95'] = np.quantile(x,0.95)
 X_train.loc[idx, 'q99'] = np.quantile(x,0.99)
 X_train.loc[idx, 'q05'] = np.quantile(x,0.05)
 X_train.loc[idx, 'q01'] = np.quantile(x,0.01)
data =(TabularList.from_df(df_train, path=path, cont_names=cont_vars, procs=procs,)
.split_by_idx(list(range(3801,3810)))
.label_from_df(cols=dep_var)
.add_test(TabularList.from_df(df_test, path=path, cont_names=cont_vars)) 
.databunch() )<define_variables>
X_train.loc[[0,1,2,3,4,0+segments,1+segments,2+segments,3+segments]]
data.show_batch(rows=5)
<choose_model_class>
scaler = StandardScaler()
scaler.fit(X_train)
 X_train_scaled = scaler.transform(X_train )
learn = tabular_learner(data, layers=[1000,500], ps=[0.005,0.05], emb_drop=0.08,
metrics=accuracy )<find_best_params>
nFolds = 3
customKFoldAvoidLeakToValidation = []
for train_id, valid_id in KFold(nFolds,shuffle=True ).split(X_train):
must_remove = []
for v in valid_id:
if(v>=segments):
must_remove.append(v-segments)
 must_remove.append(v-segments+1)
 else:
must_remove.append(v+segments-1)
 must_remove.append(v+segments)
 train_id = [t for t in train_id if t not in(must_remove)]
customKFoldAvoidLeakToValidation.append(( train_id, valid_id))
learn.model<train_model>
scorer = make_scorer(mean_absolute_error, greater_is_better=False )
learn.fit_one_cycle(5, 2e-3, wd=0.06 )<train_model>
parameters = [{ 'gamma': [0.6, 0.7, 0.8],
'C': [2.35, 2.4, 2.45, 2.5],
'nu': [0.85, 0.9, 0.95]}]
reg1 = GridSearchCV(NuSVR(kernel='rbf', tol=0.01), parameters, cv = customKFoldAvoidLeakToValidation, scoring=scorer)
 reg1.fit(X_train_scaled, y_train.values.flatten())
 y_pred1 = reg1.predict(X_train_scaled)
 print(reg1.best_params_)
 print(reg1.best_score_ )
learn.fit_one_cycle(6, 2e-3,wd=0.02 )<train_model>
parameters = [{'gamma': [0.06, 0.1, 0.08, 0.09],
'alpha': [0.005, 0.01, 0.05]}]
reg2 = GridSearchCV(KernelRidge(kernel='rbf'), parameters, cv = customKFoldAvoidLeakToValidation, scoring=scorer)
 reg2.fit(X_train_scaled, y_train.values.flatten())
 y_pred2 = reg2.predict(X_train_scaled)
 print(reg2.best_params_)
 print(reg2.best_score_ )
learn.fit_one_cycle(10, 1e-7,wd=0.02 )<predict_on_test>
score1 = mean_absolute_error(y_train.values.flatten() , y_pred1)
 print(f'Score1: {score1:0.3f}')
 score2 = mean_absolute_error(y_train.values.flatten() , y_pred2)
 print(f'Score2: {score2:0.3f}')
 score3 = mean_absolute_error(y_train.values.flatten() ,(0.5*y_pred1 + 0.5*y_pred2))
print(f'Score3: {score3:0.3f}' )
test_preds = learn.get_preds(DatasetType.Test)[1]
submit1 = pd.concat([test_seriesid,pd.Series(test_preds)],axis=1)
 submit1.columns =['series_id','surface'] 
<predict_on_test>
submission = pd.read_csv('.. /input/sample_submission.csv', index_col='seg_id' )
learn.get_preds(DatasetType.Test)[0]<predict_on_test>
X_test = pd.DataFrame(columns=X_train.columns, dtype=np.float64, index=submission.index )
preds, _ = learn.get_preds(ds_type=DatasetType.Test)
 labelled_preds = le.inverse_transform(np.array([np.argmax(preds[i])for i in range(len(preds)) ]))
sub['surface'] = labelled_preds<save_to_csv>
for seg_id in X_test.index:
seg = pd.read_csv('.. /input/test/' + seg_id + '.csv')
 x = seg['acoustic_data'].values
X_test.loc[seg_id, 'ave'] = x.mean()
X_test.loc[seg_id, 'std'] = x.std()
X_test.loc[seg_id, 'max'] = x.max()
X_test.loc[seg_id, 'min'] = x.min()
X_test.loc[seg_id, 'q95'] = np.quantile(x,0.95)
 X_test.loc[seg_id, 'q99'] = np.quantile(x,0.99)
 X_test.loc[seg_id, 'q05'] = np.quantile(x,0.05)
 X_test.loc[seg_id, 'q01'] = np.quantile(x,0.01 )
<set_options><EOS> 
X_test_scaled = scaler.transform(X_test)
 predictions_submit = 0.5*reg1.predict(X_test_scaled)+ 0.5*reg2.predict(X_test_scaled)
 predictions_submit = predictions_submit.clip(min=0)
 submission['time_to_failure'] = predictions_submit
submission.to_csv('submission.csv' )