Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<define_variables>
import numpy as np
import pandas as pd
w = [0.3, 0.3, 0.3, 0.1]
oof_train_ens = np.array([np.array(w)@ oof_train[:, :, i] for i in range(206)] ).T
oof_test_ens = np.array([np.array(w)@ oof_test[:, :, i] for i in range(206)] ).T<train_model>
from catboost import CatBoostRegressor, Pool
clipping = 1e-5
oof_test_ens = np.clip(oof_test_ens, clipping, 1-clipping)
 oof_train_ens = np.clip(oof_train_ens, clipping, 1-clipping )<load_pretrained>
train = pd.read_csv('.. /input/train.csv', iterator=True, chunksize=150_000, dtype={'acoustic_data': np.int16, 'time_to_failure': np.float64} )
joblib.dump(oof_train_ens, "oof_train_ens.pkl")
 joblib.dump(oof_test_ens, "oof_test_ens.pkl" )<compute_test_metric>
X_train = pd.DataFrame()
y_train = pd.Series()
for df in train:
ch = gen_features(df['acoustic_data'])
 X_train = X_train.append(ch, ignore_index=True)
 y_train = y_train.append(pd.Series(df['time_to_failure'].values[-1]))
corrmatrix = np.ones(( N_MODELS, N_MODELS))
corrmodel = {}
for i in range(N_MODELS):
for j in range(i+1, N_MODELS):
corrmodel[(i,j)] = np.zeros(206, dtype=np.float32)
 for z in range(206):
corrmodel[(i,j)][z] = np.corrcoef(oof_train[i, :, z], oof_train[j, :, z])[0, 1]
corrmodel[(i,j)] = np.where(np.isnan(corrmodel[(i,j)]), 0, corrmodel[(i,j)])
 corrmodel[(i,j)] = corrmodel[(i,j)].mean()
corrmatrix[i][j] = corrmodel[(i,j)]
corrmatrix[j][i] = corrmodel[(i,j)]
[print(k, v)for k, v in corrmodel.items() ]
fig, ax = plt.subplots(figsize=(8, 5))
sns.heatmap(corrmatrix, annot=True);<split>
train_pool = Pool(X_train, y_train )
prediction = join_control_group(oof_test_ens, test_control, submission, classnames)
 submit_preds(prediction, submission, test_control, classnames)
 sub = pd.read_csv("submission.csv");
print(sub.shape)
 sub.iloc[:5, :5]<save_to_csv>
m = CatBoostRegressor(iterations=10000, loss_function='MAE', boosting_type='Ordered')
 m.fit(X_train, y_train, silent=True)
 m.best_score_
def classwise_logloss(y, yhat):
yhat = np.clip(yhat, 1e-15, 1-1e-15)
 return -(y * np.log(yhat)+(1-y)*(np.log(1-yhat)) ).mean(axis=0)
 errors = np.zeros(( N_MODELS, 206))
for i in range(N_MODELS):
errors[i] = classwise_logloss(y, oof_train[i])
 errors = pd.DataFrame(errors, columns=classnames, index=list(inputs.keys()))
errors.to_csv("errors.csv", index=True )<feature_engineering>
submission = pd.read_csv('.. /input/sample_submission.csv', index_col='seg_id' )
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
warnings.filterwarnings('ignore' )<install_modules>
X_test = pd.DataFrame()
!pip install.. /input/iterative-stratification/iterative_stratification-0.1.6-py3-none-any.whl<import_modules>
for seg_id in submission.index:
seg = pd.read_csv('.. /input/test/' + seg_id + '.csv')
 ch = gen_features(seg['acoustic_data'])
 X_test = X_test.append(ch, ignore_index=True )
<set_options><EOS> 
submission['time_to_failure'] = m.predict(X_test ).clip(0, 16)
 submission.to_csv('submission.csv' )