Kaggle competition: Toxic Comment Classification Challenge
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<compute_test_metric>
import numpy as np
import pandas as pd
from pathlib import Path
from typing import *
import torch
import torch.optim as optim
mean_absolute_error(y.target,GPIII(alldata[:y.shape[0]]))<compute_test_metric>
from fastai import *
from fastai.vision import *
from fastai.text import *
from fastai.callbacks import *
mean_absolute_error(y.target,(GPI(alldata[:y.shape[0]])+
GPII(alldata[:y.shape[0]])+
GPIII(alldata[:y.shape[0]])) /3.)<save_to_csv>
%%bash
pip install pytorch-pretrained-bert
submission.time_to_failure = GPI(alldata[y.shape[0]:] ).values
submission.to_csv('gpI.csv',index=False )<save_to_csv>
class Config(dict):
def __init__(self, **kwargs):
super().__init__(**kwargs)
 for k, v in kwargs.items() :
setattr(self, k, v)
 def set(self, key, val):
self[key] = val
setattr(self, key, val)
 config = Config(
 testing=False,
bert_model_name="bert-base-uncased",
max_lr=3e-5,
epochs=4,
use_fp16=True,
bs=32,
discriminative=False,
max_seq_len=256,
)
submission.time_to_failure = GPII(alldata[y.shape[0]:] ).values
submission.to_csv('gpII.csv',index=False )<save_to_csv>
bert_tok = BertTokenizer.from_pretrained(
 config.bert_model_name,
)
submission.time_to_failure = GPIII(alldata[y.shape[0]:] ).values
submission.to_csv('gpIII.csv',index=False )<save_to_csv>
def _join_texts(texts:Collection[str], mark_fields:bool=False, sos_token:Optional[str]=BOS):

 if not isinstance(texts, np.ndarray): texts = np.array(texts)
 if is1d(texts): texts = texts[:,None]
df = pd.DataFrame({i:texts[:,i] for i in range(texts.shape[1])})
 text_col = f'{FLD} {1} ' + df[0].astype(str)if mark_fields else df[0].astype(str)
 if sos_token is not None: text_col = f"{sos_token} " + text_col
for i in range(1,len(df.columns)) :
text_col +=(f' {FLD} {i+1} ' if mark_fields else ' ')+ df[i].astype(str)
 return text_col.values
submission.time_to_failure =(( GPI(alldata[y.shape[0]:])+
GPII(alldata[y.shape[0]:])+
GPIII(alldata[y.shape[0]:])) /3.).values
submission.to_csv('gpmean.csv',index=False )<load_from_csv>
class FastAiBertTokenizer(BaseTokenizer):

 def __init__(self, tokenizer: BertTokenizer, max_seq_len: int=128, **kwargs):
self._pretrained_tokenizer = tokenizer
self.max_seq_len = max_seq_len
def __call__(self, *args, **kwargs):
return self
def tokenizer(self, t:str)-> List[str]:

 return ["[CLS]"] + self._pretrained_tokenizer.tokenize(t)[:self.max_seq_len - 2] + ["[SEP]"]
data = pd.read_csv('.. /input/andrews-features/train_X.csv')
 labels = pd.read_csv('.. /input/andrews-features/train_y.csv' )<load_from_csv>
DATA_ROOT = Path(".. ")/ "input"
train, test = [pd.read_csv(DATA_ROOT / fname)for fname in ["train.csv", "test.csv"]]
val = train
def col_replace(df, df2):
df_cols = df.columns.tolist()
df2_cols = df2.columns.tolist()
replace_cols = [x for x in df2_cols if x in df_cols]
add_cols = [x for x in df2_cols if x not in df_cols]
df[replace_cols] = df2[replace_cols]
df = pd.concat([df, df2[add_cols]], axis=1)
 return(df.reset_index(drop=True))
data2 = pd.read_csv('.. /input/lanl-features/train_features_denoised.csv')
 data = col_replace(data, data2 ).iloc[:-1, :].drop(['seg_id', 'target'], axis=1 )<define_variables>
fastai_tokenizer = Tokenizer(tok_func=FastAiBertTokenizer(bert_tok, max_seq_len=config.max_seq_len), pre_rules=[], post_rules=[] )
warnings.filterwarnings('ignore')
 NUM_EVALS = 1000
N_FOLDS = 5
LGBM_MAX_LEAVES = 2**11
LGBM_MAX_DEPTH = 25
EVAL_METRIC_LGBM_REG = 'mae'
EVAL_METRIC_LGBM_CLASS = 'auc'
XGB_MAX_LEAVES = 2**12
XGB_MAX_DEPTH = 25
EVAL_METRIC_XGB_REG = 'mae'
EVAL_METRIC_XGB_CLASS = 'auc'
CB_MAX_DEPTH = 8
OBJECTIVE_CB_REG = 'MAE'
OBJECTIVE_CB_CLASS = 'Logloss'
BEST_SCORE = 0
def quick_hyperopt(data, labels, package='lgbm', num_evals=NUM_EVALS, diagnostic=False):
if package=='lgbm':
print('Running {} rounds of LightGBM parameter optimisation:'.format(num_evals))
gc.collect()
integer_params = ['max_depth',
'num_leaves',
'max_bin',
'min_data_in_leaf',
'min_data_in_bin']
def objective(space_params):
for param in integer_params:
space_params[param] = int(space_params[param])
 if space_params['boosting']['boosting'] == 'goss':
top_rate = space_params['boosting'].get('top_rate')
 other_rate = space_params['boosting'].get('other_rate')
 top_rate = max(top_rate, 0)
 top_rate = min(top_rate, 0.5)
 other_rate = max(other_rate, 0)
 other_rate = min(other_rate, 0.5)
 space_params['top_rate'] = top_rate
space_params['other_rate'] = other_rate
subsample = space_params['boosting'].get('subsample', 1.0)
 space_params['boosting'] = space_params['boosting']['boosting']
space_params['subsample'] = subsample
cv_results = lgb.cv(space_params, train, nfold = N_FOLDS, stratified=False,
early_stopping_rounds=100, metrics=EVAL_METRIC_LGBM_REG, seed=42)
 best_loss = cv_results['l1-mean'][-1]
return{'loss':best_loss, 'status': STATUS_OK }
train = lgb.Dataset(data, labels)
 boosting_list = [{'boosting': 'gbdt',
'subsample': hp.uniform('subsample', 0.5, 1)},
{'boosting': 'goss',
'subsample': 1.0,
'top_rate': hp.uniform('top_rate', 0, 0.5),
'other_rate': hp.uniform('other_rate', 0, 0.5)}]
metric_list = ['MAE', 'RMSE']
objective_list_reg = ['huber', 'gamma', 'fair', 'tweedie']
objective_list_class = ['binary', 'cross_entropy']
objective_list = objective_list_reg
space ={'boosting' : hp.choice('boosting', boosting_list),
'num_leaves' : hp.quniform('num_leaves', 2, LGBM_MAX_LEAVES, 1),
'max_depth': hp.quniform('max_depth', 2, LGBM_MAX_DEPTH, 1),
'max_bin': hp.quniform('max_bin', 32, 255, 1),
'min_data_in_leaf': hp.quniform('min_data_in_leaf', 1, 256, 1),
'min_data_in_bin': hp.quniform('min_data_in_bin', 1, 256, 1),
'min_gain_to_split' : hp.quniform('min_gain_to_split', 0.1, 5, 0.01),
'lambda_l1' : hp.uniform('lambda_l1', 0, 5),
'lambda_l2' : hp.uniform('lambda_l2', 0, 5),
'learning_rate' : hp.loguniform('learning_rate', np.log(0.005), np.log(0.2)) ,
'metric' : hp.choice('metric', metric_list),
'objective' : hp.choice('objective', objective_list),
'feature_fraction' : hp.quniform('feature_fraction', 0.5, 1, 0.01),
'bagging_fraction' : hp.quniform('bagging_fraction', 0.5, 1, 0.01)
 }
trials = Trials()
best = fmin(fn=objective,
space=space,
algo=tpe.suggest,
max_evals=num_evals,
trials=trials)
 best['boosting'] = boosting_list[best['boosting']]['boosting']
best['metric'] = metric_list[best['metric']]
best['objective'] = objective_list[best['objective']]
for param in integer_params:
best[param] = int(best[param])
 print('{' + '
'.join('{}: {}'.format(k, v)for k, v in best.items())+ '}')
 if diagnostic:
return(best, trials)
 else:
return(best)
 if package=='xgb':
print('Running {} rounds of XGBoost parameter optimisation:'.format(num_evals))
gc.collect()
integer_params = ['max_depth']
def objective(space_params):
for param in integer_params:
space_params[param] = int(space_params[param])
 if space_params['tree_method']['tree_method'] == 'hist':
max_bin = space_params['tree_method'].get('max_bin')
 space_params['max_bin'] = int(max_bin)
 if space_params['tree_method']['grow_policy']['grow_policy']['grow_policy'] == 'depthwise':
grow_policy = space_params['tree_method'].get('grow_policy' ).get('grow_policy' ).get('grow_policy')
 space_params['grow_policy'] = grow_policy
space_params['tree_method'] = 'hist'
else:
max_leaves = space_params['tree_method']['grow_policy']['grow_policy'].get('max_leaves')
 space_params['grow_policy'] = 'lossguide'
space_params['max_leaves'] = int(max_leaves)
 space_params['tree_method'] = 'hist'
else:
space_params['tree_method'] = space_params['tree_method'].get('tree_method')
 cv_results = xgb.cv(space_params, train, nfold=N_FOLDS, metrics=[EVAL_METRIC_XGB_REG],
early_stopping_rounds=100, stratified=False, seed=42)
 best_loss = cv_results['test-mae-mean'].iloc[-1]
return{'loss':best_loss, 'status': STATUS_OK }
train = xgb.DMatrix(data, labels)
 boosting_list = ['gbtree', 'gblinear']
metric_list = ['MAE', 'RMSE']
tree_method = [{'tree_method' : 'exact'},
{'tree_method' : 'approx'},
{'tree_method' : 'hist',
'max_bin': hp.quniform('max_bin', 2**3, 2**7, 1),
'grow_policy' : {'grow_policy': {'grow_policy':'depthwise'},
'grow_policy' : {'grow_policy':'lossguide',
'max_leaves': hp.quniform('max_leaves', 32, XGB_MAX_LEAVES, 1)}}}]
objective_list_reg = ['reg:linear', 'reg:gamma', 'reg:tweedie']
objective_list_class = ['reg:logistic', 'binary:logistic']
objective_list = objective_list_reg
space ={'boosting' : hp.choice('boosting', boosting_list),
'tree_method' : hp.choice('tree_method', tree_method),
'max_depth': hp.quniform('max_depth', 2, XGB_MAX_DEPTH, 1),
'reg_alpha' : hp.uniform('reg_alpha', 0, 5),
'reg_lambda' : hp.uniform('reg_lambda', 0, 5),
'min_child_weight' : hp.uniform('min_child_weight', 0, 5),
'gamma' : hp.uniform('gamma', 0, 5),
'learning_rate' : hp.loguniform('learning_rate', np.log(0.005), np.log(0.2)) ,
'eval_metric' : hp.choice('eval_metric', metric_list),
'objective' : hp.choice('objective', objective_list),
'colsample_bytree' : hp.quniform('colsample_bytree', 0.1, 1, 0.01),
'colsample_bynode' : hp.quniform('colsample_bynode', 0.1, 1, 0.01),
'colsample_bylevel' : hp.quniform('colsample_bylevel', 0.1, 1, 0.01),
'subsample' : hp.quniform('subsample', 0.5, 1, 0.05),
'nthread' : -1
}
trials = Trials()
best = fmin(fn=objective,
space=space,
algo=tpe.suggest,
max_evals=num_evals,
trials=trials)
 best['tree_method'] = tree_method[best['tree_method']]['tree_method']
best['boosting'] = boosting_list[best['boosting']]
best['eval_metric'] = metric_list[best['eval_metric']]
best['objective'] = objective_list[best['objective']]
for param in integer_params:
best[param] = int(best[param])
 if 'max_leaves' in best:
best['max_leaves'] = int(best['max_leaves'])
 if 'max_bin' in best:
best['max_bin'] = int(best['max_bin'])
 print('{' + '
'.join('{}: {}'.format(k, v)for k, v in best.items())+ '}')
 if diagnostic:
return(best, trials)
 else:
return(best)
 if package=='cb':
print('Running {} rounds of CatBoost parameter optimisation:'.format(num_evals))
gc.collect()
integer_params = ['depth',
'min_data_in_leaf',
'max_bin']
def objective(space_params):
for param in integer_params:
space_params[param] = int(space_params[param])
 if space_params['bootstrap_type']['bootstrap_type'] == 'Bayesian':
bagging_temp = space_params['bootstrap_type'].get('bagging_temperature')
 space_params['bagging_temperature'] = bagging_temp
if space_params['grow_policy']['grow_policy'] == 'LossGuide':
max_leaves = space_params['grow_policy'].get('max_leaves')
 space_params['max_leaves'] = int(max_leaves)
 space_params['bootstrap_type'] = space_params['bootstrap_type']['bootstrap_type']
space_params['grow_policy'] = space_params['grow_policy']['grow_policy']
space_params['random_strength'] = max(space_params['random_strength'], 0)
 space_params['fold_len_multiplier'] = max(space_params['fold_len_multiplier'], 1)
 cv_results = cb.cv(train, space_params, fold_count=N_FOLDS,
early_stopping_rounds=25, stratified=False, partition_random_seed=42)
 best_loss = cv_results['test-MAE-mean'].iloc[-1]
return{'loss':best_loss, 'status': STATUS_OK}
train = cb.Pool(data, labels.astype('float32'))
bootstrap_type = [{'bootstrap_type':'Poisson'},
{'bootstrap_type':'Bayesian',
'bagging_temperature' : hp.loguniform('bagging_temperature', np.log(1), np.log(50)) },
{'bootstrap_type':'Bernoulli'}]
LEB = ['No', 'AnyImprovement', 'Armijo']
grow_policy = [{'grow_policy':'SymmetricTree'},
{'grow_policy':'Depthwise'},
{'grow_policy':'Lossguide',
'max_leaves': hp.quniform('max_leaves', 2, 32, 1)}]
eval_metric_list_reg = ['MAE', 'RMSE', 'Poisson']
eval_metric_list_class = ['Logloss', 'AUC', 'F1']
eval_metric_list = eval_metric_list_reg
space ={'depth': hp.quniform('depth', 2, CB_MAX_DEPTH, 1),
'max_bin' : hp.quniform('max_bin', 1, 32, 1),
'l2_leaf_reg' : hp.uniform('l2_leaf_reg', 0, 5),
'min_data_in_leaf' : hp.quniform('min_data_in_leaf', 1, 50, 1),
'random_strength' : hp.loguniform('random_strength', np.log(0.005), np.log(5)) ,
'bootstrap_type' : hp.choice('bootstrap_type', bootstrap_type),
'learning_rate' : hp.uniform('learning_rate', 0.05, 0.25),
'eval_metric' : hp.choice('eval_metric', eval_metric_list),
'objective' : OBJECTIVE_CB_REG,
'leaf_estimation_backtracking' : hp.choice('leaf_estimation_backtracking', LEB),
'grow_policy': hp.choice('grow_policy', grow_policy),
'fold_len_multiplier' : hp.loguniform('fold_len_multiplier', np.log(1.01), np.log(2.5)) ,
'od_type' : 'Iter',
'od_wait' : 25,
'task_type' : 'GPU',
'verbose' : 0
}
trials = Trials()
best = fmin(fn=objective,
space=space,
algo=tpe.suggest,
max_evals=num_evals,
trials=trials)
 best['bootstrap_type'] = bootstrap_type[best['bootstrap_type']]['bootstrap_type']
best['grow_policy'] = grow_policy[best['grow_policy']]['grow_policy']
best['eval_metric'] = eval_metric_list[best['eval_metric']]
best['leaf_estimation_backtracking'] = LEB[best['leaf_estimation_backtracking']]
for param in integer_params:
best[param] = int(best[param])
 if 'max_leaves' in best:
best['max_leaves'] = int(best['max_leaves'])
 print('{' + '
'.join('{}: {}'.format(k, v)for k, v in best.items())+ '}')
 if diagnostic:
return(best, trials)
 else:
return(best)
 else:
print('Package not recognised.Please use "lgbm" for LightGBM, "xgb" for XGBoost or "cb" for CatBoost.' )<train_on_grid>
label_cols = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
lgbm_params = quick_hyperopt(data, labels, 'lgbm', 2500 )<save_model>
class BertTokenizeProcessor(TokenizeProcessor):
def __init__(self, tokenizer):
super().__init__(tokenizer=tokenizer, include_bos=False, include_eos=False)
 class BertNumericalizeProcessor(NumericalizeProcessor):
def __init__(self, *args, **kwargs):
super().__init__(*args, vocab=Vocab(list(bert_tok.vocab.keys())) , **kwargs)
 def get_bert_processor(tokenizer:Tokenizer=None, vocab:Vocab=None):

 return [BertTokenizeProcessor(tokenizer=tokenizer),
NumericalizeProcessor(vocab=vocab)]
np.save('lgbm_params.npy', lgbm_params )<train_on_grid>
class BertDataBunch(TextDataBunch):
@classmethod
def from_df(cls, path:PathOrStr, train_df:DataFrame, valid_df:DataFrame, test_df:Optional[DataFrame]=None,
tokenizer:Tokenizer=None, vocab:Vocab=None, classes:Collection[str]=None, text_cols:IntsOrStrs=1,
label_cols:IntsOrStrs=0, label_delim:str=None, **kwargs)-> DataBunch:
"Create a `TextDataBunch` from DataFrames."
p_kwargs, kwargs = split_kwargs_by_func(kwargs, get_bert_processor)
 processor = get_bert_processor(tokenizer=tokenizer, vocab=vocab, **p_kwargs)
 if classes is None and is_listy(label_cols)and len(label_cols)> 1: classes = label_cols
src = ItemLists(path, TextList.from_df(train_df, path, cols=text_cols, processor=processor),
TextList.from_df(valid_df, path, cols=text_cols, processor=processor))
src = src.label_for_lm() if cls==TextLMDataBunch else src.label_from_df(cols=label_cols, classes=classes)
 if test_df is not None: src.add_test(TextList.from_df(test_df, path, cols=text_cols))
return src.databunch(**kwargs )
example_params, example_trials = quick_hyperopt(data, labels, 'lgbm', 5, diagnostic=True )<init_hyperparams>
databunch = BertDataBunch.from_df(".", train, val, test,
tokenizer=fastai_tokenizer,
vocab=fastai_bert_vocab,
text_cols="comment_text",
label_cols=label_cols,
bs=config.bs,
collate_fn=partial(pad_collate, pad_first=False, pad_idx=0),
)
lanl_params = {'num_leaves': 128,
'min_data_in_leaf': 79,
'objective': 'gamma',
'max_depth': -1,
'learning_rate': 0.01,
"boosting": "gbdt",
"bagging_freq": 5,
"bagging_fraction": 0.9,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501,
'feature_fraction': 0.1
}<load_from_csv>
bert_model = BertForSequenceClassification.from_pretrained(config.bert_model_name, num_labels=6 )
sub_1 = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 sub_2 = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 test = pd.read_csv('.. /input/andrews-features/test_X.csv')
 test2 = pd.read_csv('.. /input/lanl-features/test_features_denoised.csv')
 test = col_replace(test, test2 ).drop(['seg_id', 'target'], axis=1)
 preds1 = np.zeros(len(sub_1))
preds2 = np.zeros(len(sub_2))
MAE_val1 = 0
MAE_val2 = 0
n_fold = 5
folds = KFold(n_splits=n_fold, shuffle=True, random_state=42)
 X = data
y = labels.time_to_failure.values
for train_idx, valid_idx in folds.split(y):
X_train, X_valid = X.iloc[train_idx], X.iloc[valid_idx]
y_train, y_valid = y[train_idx], y[valid_idx]
model1 = lgb.LGBMRegressor(**lanl_params, n_estimators = 50000, n_jobs = -1, eval_metric='mae')
 model1.fit(X_train, y_train,eval_set=[(X_train, y_train),(X_valid, y_valid)],
verbose=0, early_stopping_rounds=200)
 val_preds1 = model1.predict(X_valid)
 MAE_val1 += mean_absolute_error(y_valid, val_preds1)/n_fold
preds1 += model1.predict(test)/n_fold
model2 = lgb.LGBMRegressor(**lgbm_params, n_estimators = 50000, n_jobs = -1)
 model2.fit(X_train, y_train,eval_set=[(X_train, y_train),(X_valid, y_valid)],
verbose=0, early_stopping_rounds=200)
 val_preds2 = model2.predict(X_valid)
 MAE_val2 += mean_absolute_error(y_valid, val_preds2)/n_fold
preds2 += model2.predict(test)/n_fold
sub_1['time_to_failure'] = preds1
sub_2['time_to_failure'] = preds2
sub_1.to_csv('submission_lanl_params.csv', index=False)
 sub_2.to_csv('submission_hyperopt_params.csv', index=False)
 print('CV score - public kernel parameters: {}'.format(MAE_val1))
print('CV score - quick_hyperopt parameters: {}'.format(MAE_val2))<import_modules>
loss_func = nn.BCEWithLogitsLoss()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import lightgbm as lgb
import os 
<load_from_csv>
learner = Learner(
 databunch, bert_model,
loss_func=loss_func, 
)
 if config.use_fp16: learner = learner.to_fp16()
<normalization>
learner.lr_find()
class myscaler:
def __init__(self, data):
self.shape = data.shape
if len(data.shape)== 3:
self.diff_stat = []
self.scaler(data)
 self.transform = self._transform
elif len(data.shape)== 2:
scaler = StandardScaler()
scaler.fit(data)
 self.transform = scaler.transform
elif len(data.shape == 1):
print("dim error")
 self.transform = None
def scaler(self, data):
features = data.shape[-1]
for i in range(features):
self.diff_stat.append([np.mean(data[...,i]), np.std(data[...,i])])
 def _transform(self, data):
assert data.shape[-1] == self.shape[-1]
features = data.shape[-1]
for i in range(features):
data[...,i] =(data[...,i] - self.diff_stat[i][0])/self.diff_stat[i][1]
return data
def transform(self,data):
return self.transform(data)
 def gen_feature(data):
features = []
features.append(data.abs().min())
 features.append(data.abs().max())
 features.append(data.abs().mean())
 features.append(data.min())
 features.append(data.max())
 features.append(data.mean())
 features.append(data.kurtosis())
 features.append(data.skew())
 for i in np.arange(0.01, 0.1, 0.01):
features.append(np.quantile(data, i))
for i in np.arange(0.9, 1, 0.01):
features.append(np.quantile(data, i))
data = data.diff().dropna()
features.append(data.abs().min())
 features.append(data.abs().max())
 features.append(data.abs().mean())
 features.append(data.min())
 features.append(data.max())
 features.append(data.mean())
 features.append(data.kurtosis())
 features.append(data.skew())
 for i in np.arange(0.01, 0.1, 0.01):
features.append(np.quantile(data, i))
for i in np.arange(0.9, 1, 0.01):
features.append(np.quantile(data, i))
return pd.Series(features)
 def rolling_feature(data, window_size = 2000, step = 2000):
def feature_small_set(data):
features = []
features.append(data.min())
 features.append(data.max())
 features.append(data.mean())
 features.append(data.std())
 data = np.diff(data)
 features.append(data.min())
 features.append(data.max())
 features.append(data.mean())
 features.append(data.std())
 for i in np.arange(0.9, 1, 0.01):
features.append(np.quantile(data, i))
return features
ans = []
for i in range(window_size, len(data), step):
ans.append(feature_small_set(data[(i-window_size):i]))
return np.array(ans)
 def check_quake(data):
return(data.diff(-1 ).dropna() >= 0 ).all()
def load_batch(data,labels, batch_size=20, shuffle=True):
assert len(data)== len(labels)
 size = len(data)
 for i in range(0, size, batch_size):
if(i+batch_size > size):
yield data[i:size], labels[i:size]
else:
yield data[i:i+batch_size], labels[i:i + batch_size]<define_variables>
learner.fit_one_cycle(config.epochs, max_lr=config.max_lr )
features_name = ["abs_min", "abs_max", "abs_mean", "min", 'max', 'mean', 'kurtosis', 'skew'] + ["quantile" + f"{i:.3}" for i in np.arange(0.01, 0.1, 0.01)] + ["quantile" + f"{i:.3}" for i in np.arange(0.9, 1, 0.01)]
features_name = features_name + ["diff_" + i for i in features_name ]<prepare_x_and_y>
def get_preds_as_nparray(ds_type)-> np.ndarray:

 preds = learner.get_preds(ds_type)[0].detach().cpu().numpy()
sampler = [i for i in databunch.dl(ds_type ).sampler]
reverse_sampler = np.argsort(sampler)
 return preds[reverse_sampler, :]
def gen_data(feature_fun = gen_feature, test_size=100, size = None, shuffle=True, start_from = None):
train_gen = pd.read_csv(".. /input/train.csv", chunksize=150_000, dtype={'acoustic_data': np.int16, 'time_to_failure': np.float32})
 X_train = []
y_train = []
X_test = []
y_test = []
if start_from is None:
start_from = 0
for i, data in enumerate(train_gen):
data.rename({'acoustic_data': "signal", 'time_to_failure': "quaketime"}, inplace=True, axis="columns")
 if i < start_from:
del data
continue
if size is not None and i >= start_from + size:
break
if check_quake(data["quaketime"])== False:
print("quake at", i, "excluded")
 try:
print(X_train[-1].shape)
 except IndexError:
pass
X_train.append(feature_fun(data.signal))
y_train.append(data.quaketime.values[-1])
 del data
gc.collect()
X_train = np.array(X_train)
 y_train = np.array(y_train)
 if shuffle:
index = np.arange(0, len(X_train))
np.random.shuffle(index)
 X_train = X_train[index]
y_train = y_train[index]
X_test = X_train[:test_size]
y_test = y_train[:test_size]
X_train = X_train[test_size:]
y_train = y_train[test_size:]
print(X_train.shape, y_train.shape)
 print(X_test.shape, y_test.shape)
 return X_train, y_train, X_test, y_test 
<prepare_x_and_y>
test_preds = get_preds_as_nparray(DatasetType.Test )
<normalization><EOS> 
sample_submission = pd.read_csv(DATA_ROOT / "sample_submission.csv")
 if config.testing: sample_submission = sample_submission.head(test.shape[0])
 sample_submission[label_cols] = test_preds
sample_submission.to_csv("predictions.csv", index=False )