Kaggle competition: LANL Earthquake Prediction
tr.replace(-np.inf, 0, inplace = True)
 tr.replace(np.inf, 0, inplace = True)
 te.replace(-np.inf, 0, inplace = True)
 te.replace(np.inf, 0, inplace = True )<define_variables>
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm
import warnings
import sys
import gc
from scipy.stats import kurtosis
from scipy.stats import skew
from scipy.stats import kstat
from scipy.signal import hilbert
from scipy.signal import hann
from scipy.signal import convolve
from scipy.signal import welch
import scipy.signal as sg
from tsfresh.feature_extraction import feature_calculators
import nolds
import librosa
from imblearn.over_sampling import SMOTE
from catboost import CatBoostRegressor, Pool
import tensorflow
from keras.models import Sequential
from keras.layers import Dense, LSTM
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler, RobustScaler, MaxAbsScaler
from sklearn.svm import NuSVR
from sklearn.svm import SVR
from sklearn.manifold import TSNE
from sklearn.metrics import mean_absolute_error, SCORERS
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LinearRegression, Lasso, MultiTaskLasso, ElasticNet, MultiTaskElasticNet, Ridge
from sklearn.neural_network import MLPRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, BaggingRegressor
svc_cols=[x for x in tr.columns if 'orientation_W' in x]
svc_cols=svc_cols+[x for x in tr.columns if 'orientation_Z' in x]<define_variables>
%matplotlib inline
%precision 4
warnings.filterwarnings('ignore')
 plt.style.use('ggplot')
 np.set_printoptions(suppress=True)
 pd.set_option("display.precision", 15 )
clf_cols=[x for x in tr.columns if not(x in svc_cols)]+['group_id']<merge>
print('pandas: {}'.format(pd.__version__))
print('numpy: {}'.format(np.__version__))
print('Python: {}'.format(sys.version))
print('Tensorflow: {}'.format(tensorflow.__version__))
X=tr.join(df_target['group_id'],on='series_id' )<prepare_x_and_y>
def load_data(nrows):

 filename = ".. /input/train.csv"
return pd.read_csv(
 filename,
dtype={
'acoustic_data': np.int16,
'time_to_failure': np.float32
},
nrows=nrows,
skiprows=1,
names = ['acoustic_data', 'time_to_failure'] 
)
 def get_data_iterator(nrows):
return pd.read_csv('.. /input/train.csv',
iterator=True,
dtype={
'acoustic_data': np.int16,
'time_to_failure': np.float32
},
nrows=nrows,
skiprows=1,
names = ['acoustic_data', 'time_to_failure'] 
)
y=X['group_id']
X=X[svc_cols]
T=te[svc_cols]<choose_model_class>
%%time
nrows = 600_000_000
data_df = load_data(nrows )
CV_STEPS=10
folds = StratifiedKFold(n_splits=CV_STEPS, shuffle=True, random_state=876 )<train_model>
del data_df
gc.collect()
i=0
tst_preds_gr = np.zeros(( T.shape[0], CV_STEPS))
oof_preds_gr = np.zeros(( X.shape[0]))
splitter=folds.split(X,y)
 for(train_index, valid_index)in(splitter):
print('='*20, i, '='*20)
 X_train = X.iloc[train_index]
print('train size:',len(train_index))
X_valid = X.iloc[valid_index]
print('valid size:',len(valid_index))
y_train = y.iloc[train_index]
y_valid = y.iloc[valid_index]
clf = SVC(kernel='linear')
 clf.fit(X_train, y_train)
 oof_preds_gr[valid_index] = clf.predict(X_valid)
 tst_preds_gr[:,i] = clf.predict(T)
 print('score ', accuracy_score(oof_preds_gr[valid_index], y_valid.values))
i=i+1<predict_on_test>
NY_FREQ_IDX = 75000
CUTOFF = 18000
MAX_FREQ_IDX = 20000
FREQ_STEP = 2500
def extr_test_pred(tst_preds_cv):
gr_preds=np.zeros(len(tst_preds_cv),dtype=int)
 for r in range(0,len(tst_preds_cv)) :
gr_preds[r]=Counter(tst_preds_cv[r,:] ).most_common(1)[0][0]
return gr_preds<feature_engineering>
def add_trend_feature(arr, abs_values=False):
arr = arr[::50]
idx = np.array(range(len(arr)))
 if abs_values:
arr = np.abs(arr)
 lr = LinearRegression()
lr.fit(idx.reshape(-1, 1), arr)
 return lr.coef_[0]
tr['group_id']=y<predict_on_test>
def classic_sta_lta(x, length_sta, length_lta):
sta = np.cumsum(x ** 2)
 sta = np.require(sta, dtype=np.float)
 lta = sta.copy()
sta[length_sta:] = sta[length_sta:] - sta[:-length_sta]
sta /= length_sta
lta[length_lta:] = lta[length_lta:] - lta[:-length_lta]
lta /= length_lta
sta[:length_lta - 1] = 0
dtiny = np.finfo(0.0 ).tiny
idx = lta < dtiny
lta[idx] = dtiny
return sta / lta
te['group_id']=extr_test_pred(tst_preds_gr )<prepare_x_and_y>
def calc_change_rate(x):
change =(np.diff(x)/ x[:-1] ).values
change = change[np.nonzero(change)[0]]
change = change[~np.isnan(change)]
change = change[change != -np.inf]
change = change[change != np.inf]
return np.mean(change )
X=tr[clf_cols]
T=te[clf_cols]
y=pd.DataFrame(target['surface'],index=tr.index)
 LABELS=[0,1,2,3,4,5,6,7,8]<save_to_csv>
def _embed(x, order=3, delay=1):

 N = len(x)
 if order * delay > N:
raise ValueError("Error: order * delay should be lower than x.size")
 if delay < 1:
raise ValueError("Delay has to be at least 1.")
 if order < 2:
raise ValueError("Order has to be at least 2.")
 Y = np.zeros(( order, N -(order - 1)* delay))
for i in range(order):
Y[i] = x[i * delay:i * delay + Y.shape[1]]
return Y.T
@jit('UniTuple(float64, 2 )(float64[:], float64[:])', nopython=True)
 def _linear_regression(x, y):

 n_times = x.size
sx2 = 0
sx = 0
sy = 0
sxy = 0
for j in range(n_times):
sx2 += x[j] ** 2
sx += x[j]
sxy += x[j] * y[j]
sy += y[j]
den = n_times * sx2 -(sx ** 2)
 num = n_times * sxy - sx * sy
slope = num / den
intercept = np.mean(y)- slope * np.mean(x)
 return slope, intercept
@jit('i8[:](f8, f8, f8)', nopython=True)
 def _log_n(min_n, max_n, factor):

 max_i = int(floor(log(1.0 * max_n / min_n)/ log(factor)))
 ns = [min_n]
for i in range(max_i + 1):
n = int(floor(min_n *(factor ** i)))
 if n > ns[-1]:
ns.append(n)
 return np.array(ns, dtype=np.int64)
 def perm_entropy(x, order=3, delay=1, normalize=False):

 x = np.array(x)
 ran_order = range(order)
 hashmult = np.power(order, ran_order)
 sorted_idx = _embed(x, order=order, delay=delay ).argsort(kind='quicksort')
 hashval =(np.multiply(sorted_idx, hashmult)).sum(1)
 _, c = np.unique(hashval, return_counts=True)
 p = np.true_divide(c, c.sum())
 pe = -np.multiply(p, np.log2(p)).sum()
if normalize:
pe /= np.log2(factorial(order))
return pe
def spectral_entropy(x, sf, method='fft', nperseg=None, normalize=False):

 x = np.array(x)
 if method == 'fft':
_, psd = periodogram(x, sf)
 elif method == 'welch':
_, psd = welch(x, sf, nperseg=nperseg)
 psd_norm = np.divide(psd, psd.sum())
 se = -np.multiply(psd_norm, np.log2(psd_norm)).sum()
if normalize:
se /= np.log2(psd_norm.size)
 return se
def svd_entropy(x, order=3, delay=1, normalize=False):

 x = np.array(x)
 mat = _embed(x, order=order, delay=delay)
 W = np.linalg.svd(mat, compute_uv=False)
 W /= sum(W)
 svd_e = -np.multiply(W, np.log2(W)).sum()
if normalize:
svd_e /= np.log2(order)
 return svd_e
X.to_csv('X.csv', index=False)
 T.to_csv('T.csv', index=False)
 y.to_csv('y.csv', index=False )<choose_model_class>
@jit('UniTuple(float64, 2 )(float64[:], float64[:])', nopython=True)
 def _linear_regression(x, y):

 n_times = x.size
sx2 = 0
sx = 0
sy = 0
sxy = 0
for j in range(n_times):
sx2 += x[j] ** 2
sx += x[j]
sxy += x[j] * y[j]
sy += y[j]
den = n_times * sx2 -(sx ** 2)
 num = n_times * sxy - sx * sy
slope = num / den
intercept = np.mean(y)- slope * np.mean(x)
 return slope, intercept
def petrosian_fd(x):

 n = len(x)
 diff = np.ediff1d(x)
 N_delta =(diff[1:-1] * diff[0:-2] < 0 ).sum()
return np.log10(n)/(np.log10(n)+ np.log10(n /(n + 0.4 * N_delta)))
 def katz_fd(x):

 x = np.array(x)
 dists = np.abs(np.ediff1d(x))
ll = dists.sum()
ln = np.log10(np.divide(ll, dists.mean()))
aux_d = x - x[0]
d = np.max(np.abs(aux_d[1:]))
return np.divide(ln, np.add(ln, np.log10(np.divide(d, ll))))
@jit('float64(float64[:], int32)')
 def _higuchi_fd(x, kmax):

 n_times = x.size
lk = np.empty(kmax)
 x_reg = np.empty(kmax)
 y_reg = np.empty(kmax)
 for k in range(1, kmax + 1):
lm = np.empty(( k,))
for m in range(k):
ll = 0
n_max = floor(( n_times - m - 1)/ k)
 n_max = int(n_max)
 for j in range(1, n_max):
ll += abs(x[m + j * k] - x[m +(j - 1)* k])
 ll /= k
ll *=(n_times - 1)/(k * n_max)
 lm[m] = ll
m_lm = 0
for m in range(k):
m_lm += lm[m]
m_lm /= k
lk[k - 1] = m_lm
x_reg[k - 1] = log(1./ k)
 y_reg[k - 1] = log(m_lm)
 higuchi, _ = _linear_regression(x_reg, y_reg)
 return higuchi
def higuchi_fd(x, kmax=10):

 x = np.asarray(x, dtype=np.float64)
 kmax = int(kmax)
 return _higuchi_fd(x, kmax )
CV_STEPS=30
folds = StratifiedKFold(n_splits=CV_STEPS, shuffle=True, random_state=1984 )<count_unique_values>
@jit('f8(f8[:])', nopython=True)
 def _dfa(x):

 N = len(x)
 nvals = _log_n(4, 0.1 * N, 1.2)
 walk = np.cumsum(x - x.mean())
 fluctuations = np.zeros(len(nvals))
for i_n, n in enumerate(nvals):
d = np.reshape(walk[:N -(N % n)],(N // n, n))
ran_n = np.array([float(na)for na in range(n)])
 d_len = len(d)
 slope = np.empty(d_len)
 intercept = np.empty(d_len)
 trend = np.empty(( d_len, ran_n.size))
for i in range(d_len):
slope[i], intercept[i] = _linear_regression(ran_n, d[i])
 y = np.zeros_like(ran_n)
 for p in [slope[i], intercept[i]]:
y = y * ran_n + p
trend[i, :] = y
flucs = np.sqrt(np.sum(( d - trend)** 2, axis=1)/ n)
 fluctuations[i_n] = flucs.sum() / flucs.size
nonzero = np.nonzero(fluctuations)[0]
fluctuations = fluctuations[nonzero]
nvals = nvals[nonzero]
if len(fluctuations)== 0:
dfa = np.nan
else:
dfa, _ = _linear_regression(np.log(nvals), np.log(fluctuations))
return dfa
def detrended_fluctuation(x):

 x = np.asarray(x, dtype=np.float64)
 return _dfa(x )
groups=yg.unique()
dummy=np.zeros(len(groups))<drop_column>
def extract_features(X_data, segId, serie):

 xcdm = serie - np.mean(serie)
 b, a = des_bw_filter_lp(cutoff=2500)
 xc0 = sg.lfilter(b, a, xcdm)
 b, a = des_bw_filter_bp(low=2500, high=5000)
 xc1 = sg.lfilter(b, a, xcdm)
 b, a = des_bw_filter_bp(low=5000, high=7500)
 xc2 = sg.lfilter(b, a, xcdm)
 b, a = des_bw_filter_bp(low=7500, high=10000)
 xc3 = sg.lfilter(b, a, xcdm)
 b, a = des_bw_filter_bp(low=10000, high=12500)
 xc4 = sg.lfilter(b, a, xcdm)
 b, a = des_bw_filter_bp(low=12500, high=15000)
 xc5 = sg.lfilter(b, a, xcdm)
 b, a = des_bw_filter_bp(low=15000, high=17500)
 xc6 = sg.lfilter(b, a, xcdm)
 b, a = des_bw_filter_bp(low=17500, high=20000)
 xc7 = sg.lfilter(b, a, xcdm)
 b, a = des_bw_filter_hp(cutoff=20000)
 xc8 = sg.lfilter(b, a, xcdm)
 sigs = [
serie,
pd.Series(xc0),
pd.Series(xc1),
pd.Series(xc2),
pd.Series(xc3),
pd.Series(xc4),
pd.Series(xc5),
pd.Series(xc6),
pd.Series(xc7),
pd.Series(xc8)
 ]
for i, sig in enumerate(sigs):
X_data.loc[segId, "hurst_exponent_%d" % i] = nolds.hurst_rs(sig)
 X_data.loc[segId, "classic_sta_lta_mean_%d" % i] = classic_sta_lta(sig, 100, 5000 ).mean()
X_data.loc[segId, "fractal_dim_higuchi"] = higuchi_fd(serie)
 X_data.loc[segId, "mean"] = np.mean(serie)
 X_data.loc[segId, "mad_6"] = pd.Series(xc5 ).mad()
X_data.loc[segId, "mad_7"] = pd.Series(xc6 ).mad()
X_data.loc[segId, "abs_q05"] = np.quantile(np.abs(serie), 0.05)
 X_data.loc[segId,"std_0_to_10"] = serie[(serie>=0)&(serie<=10)].std()
X_data.loc[segId,"std_minus_10_to_10"] = serie[(serie>=-10)&(serie<=10)].std()
for p in [5, 10]:
X_data.loc[segId, "nb_peaks_%i" % p] = feature_calculators.number_peaks(serie, p)
 X_data.loc[segId, "num_crossings"] = feature_calculators.number_crossing_m(serie, 0)

x_roll_std_100 = serie.rolling(100 ).std().dropna().values[::100]
X_data.loc[segId, "q30_roll_std_100"] = np.quantile(x_roll_std_100, 0.30 )
def group_id_drop(W):
try:
W.drop('group_id',axis=1,inplace=True)
 except:
print('Info: group_id already dropped.')
 return(W )<define_variables>
nrows = 615_000_000
rows_by_chunk = 150_000
segments = nrows//rows_by_chunk
X_data = pd.DataFrame(index=range(segments), dtype=np.float32)
 y_data = pd.DataFrame(index=range(segments), dtype=np.float32)
 data_iterator = get_data_iterator(nrows)
 for segId in tqdm(range(segments)) :
seg = data_iterator.get_chunk(rows_by_chunk)
 time_to_failure = seg['time_to_failure'].values[-1]
extract_features(X_data, segId, seg['acoustic_data'])
 y_data.loc[segId, "time_to_failure"] = time_to_failure
y_data = np.array(y_data ).flatten()
gc.collect()
X_data.head()
tst_preds_rf = np.zeros(( T.shape[0], CV_STEPS))
oof_preds_rf = np.zeros(( X.shape[0]))
importances = np.zeros(( CV_STEPS,X.shape[1]-1))
i=0
splitter=folds.split(groups,dummy)
 for(tr_gr_idx, va_gr_idx)in(splitter):
print('-'*20, i, '-'*20)
 tr_groups=groups[tr_gr_idx]
va_groups=groups[va_gr_idx]
X_train = X[X['group_id'].isin(tr_groups)]
X_train.drop('group_id',axis=1,inplace=True)
 train_index = X_train.index
print('train size:',len(train_index))
X_valid = X[X['group_id'].isin(va_groups)]
X_valid.drop('group_id',axis=1,inplace=True)
 valid_index = X_valid.index
print('valid size:',len(valid_index))
y_train = y.iloc[train_index]
curr_labels=y_train['surface'].unique()
print('used labels:',len(curr_labels),' [',','.join([str(c)for c in curr_labels]),']')
 y_valid = y.iloc[valid_index]
clf = RandomForestClassifier(n_estimators = 500, n_jobs = -1)
 clf.fit(X_train, y_train)
 oof_preds_rf[valid_index] = clf.predict(X_valid)
 TWG=T.drop('group_id',axis=1)
 tst_preds_rf[:,i] = clf.predict(TWG)
 ass_labels=np.unique(tst_preds_rf[:,i])
 print('assigned labels:',len(ass_labels),' [',','.join([str(int(c)) for c in ass_labels]),']')
 print('score ', accuracy_score(oof_preds_rf[valid_index], y_valid.values))
importances[i,0:X.shape[1]] = clf.feature_importances_
i=i+1<define_variables>
np.abs(X_data.corrwith(pd.Series(y_data)) ).sort_values(ascending=False ).head(20 )
tst_preds_rf0 = tst_preds_rf
oof_preds_rf0 = oof_preds_rf
importances0 = importances<predict_on_test>
gc.collect()
preds_rf0=extr_test_pred(tst_preds_rf0 )<compute_test_metric>
X_train, X_test, y_train, y_test = train_test_split(
 X_data, y_data,
test_size=0.2,
shuffle=True, random_state=42)
 X_train, X_valid, y_train, y_valid = train_test_split(
 X_train, y_train,
test_size=0.2,
shuffle=True, random_state=42 
)
 print("Training on", X_train.shape[0], "rows")
 print("Validating on", X_valid.shape[0], "rows")
 print("Testing on", X_test.shape[0], "rows")
 f,(ax1, ax2, ax3)= plt.subplots(1, 3, sharey=True)
 f.suptitle('Histogram of time_to_failure')
 ax1.hist(y_train, bins=17)
 ax1.set_title("train")
 ax2.hist(y_valid, bins=17)
 ax2.set_title("test")
 ax3.hist(y_test, bins=17)
 ax3.set_title("validation")
 plt.show()
accuracy_score(target['surface'], oof_preds_rf0 )<train_on_grid>
scaler = StandardScaler()
scaler.fit(X_train)
 X_train = scaler.transform(X_train)
 X_test = scaler.transform(X_test)
 X_valid = scaler.transform(X_valid)
 print("Data scaled" )
i=0
tst_preds_rf = np.zeros(( T.shape[0], CV_STEPS))
oof_preds_rf = np.zeros(( X.shape[0]))
importances = np.zeros(( CV_STEPS,X.shape[1]-1))
splitter=folds.split(X,y)
 for(train_index, valid_index)in(splitter):
print('
X_train = X.iloc[train_index]
print('train size:',len(train_index))
X_valid = X.iloc[valid_index]
print('valid size:',len(valid_index))
y_train = y.iloc[train_index]
curr_labels=y_train['surface'].unique()
X_train=group_id_drop(X_train)
 X_valid=group_id_drop(X_valid)
 print('used labels:',len(curr_labels),' [',','.join([str(c)for c in curr_labels]),']')
 y_valid = y.iloc[valid_index]
clf = RandomForestClassifier(n_estimators = 500, n_jobs = -1)
 clf.fit(X_train, y_train)
 oof_preds_rf[valid_index] = clf.predict(X_valid)
 TWG=T.drop('group_id',axis=1)
 tst_preds_rf[:,i] = clf.predict(TWG)
 ass_labels=np.unique(tst_preds_rf[:,i])
 print('assigned labels:',len(ass_labels),' [',','.join([str(int(c)) for c in ass_labels]),']')
 print('score ', accuracy_score(oof_preds_rf[valid_index], y_valid.values))
importances[i,0:X.shape[1]] = clf.feature_importances_
i=i+1<predict_on_test>
dataPool = Pool(X_train, y_train)
 modelCatBoost = CatBoostRegressor(iterations=1000,
loss_function='MAE',
learning_rate=0.1,
depth=6,
l2_leaf_reg=3,
border_count=128,
use_best_model=True,
silent=True)
 modelCatBoost.fit(dataPool, eval_set=Pool(X_valid, y_valid), plot=True)
 y_pred = modelCatBoost.predict(X_test)
 score = plotAndScore(y_pred, y_test, "Catboost" )
preds_rf=extr_test_pred(tst_preds_rf )<compute_test_metric>
params = {}
params["iterations"] = 500
params["loss_function"] = 'MAE'
params["learning_rate"] = 0.2
params["depth"] = 6
params["verbose"] = 0
cv_data = cv(pool=Pool(StandardScaler().fit_transform(X_data), y_data),
params=params,
fold_count=5,
inverted=False,
shuffle=True,
stratified=False,
as_pandas=True,
plot=True,
verbose=False)
 min_test_mean = cv_data["test-MAE-mean"].min()
print("best MAE :",min_test_mean )
accuracy_score(target['surface'], oof_preds_rf )<choose_model_class>
models = {
"CatBoostRegressor" : CatBoostRegressor(iterations=300, loss_function='MAE', learning_rate=0.2, depth=6, verbose=0),
"NuSVR" : NuSVR(kernel="rbf", C=1),
"SVR" : SVR(kernel="rbf", C=1),
"DecisionTreeRegressor" : DecisionTreeRegressor() ,
"RandomForestRegressor" : RandomForestRegressor(max_depth=6, n_estimators=200),
"GradientBoostingRegressor" : GradientBoostingRegressor() ,
"KNeighborsRegressor(n_neighbors=50)" : KNeighborsRegressor(n_neighbors=50),
'LinearRegression': LinearRegression() ,
'Ridge_1': Ridge(alpha=0.001),
'Lasso_1': Lasso(alpha=0.001),
'ElasticNet_1': ElasticNet(alpha=0.001, l1_ratio=0),
'ElasticNet_2': ElasticNet(alpha=0.001, l1_ratio=0.25),
'ElasticNet_3': ElasticNet(alpha=0.001, l1_ratio=0.33),
'ElasticNet_4': ElasticNet(alpha=0.001, l1_ratio=0.5),
'ElasticNet_5': ElasticNet(alpha=0.001, l1_ratio=0.66),
'ElasticNet_6': ElasticNet(alpha=0.001, l1_ratio=0.75),
'ElasticNet_7': ElasticNet(alpha=0.001, l1_ratio=1),
}
CV_STEPS=10
folds = StratifiedKFold(n_splits=CV_STEPS, shuffle=True, random_state=1984 )<init_hyperparams>
submission = pd.read_csv('.. /input/sample_submission.csv', index_col='seg_id')
 X_data_submission = pd.DataFrame(dtype=np.float64, index=submission.index)
 plot_data_submission = pd.Series()
for seg_id in tqdm(X_data_submission.index):
seg = pd.read_csv('.. /input/test/' + seg_id + '.csv')
 plot_data_submission.append(seg[::50])
 x = seg['acoustic_data']
extract_features(X_data_submission, seg_id, x)
NR_EPOCHS=1000
xgb_params = {
'booster' : 'gbtree',
'objective' : 'multi:softprob',
'num_class' : 9,
'seed': 1984,
'eta': 0.01,
'max_depth': 3,
'gamma': 0.1,
'lambda': 0.8,
'subsample': 0.8,
'colsample_bytree': 0.8,
'tree_method': 'gpu_hist',
'device': 'gpu',
'silent': 1,
}
verbose_eval = 100
early_stop = 200<train_model>
X_data_submission = scaler.transform(X_data_submission)
 submission['time_to_failure'] = [x*1.05 for x in modelCatBoost.predict(X_data_submission)]
def xgb_train_no_groups(params, X, y, T):
oof_train_preds = np.zeros(X.shape[0])
 tst_preds = np.zeros(( T.shape[0],CV_STEPS))
perf_dict_list=[]
i=0
splitter=folds.split(X,y)
 for(train_index, valid_index)in(splitter):
print('
X_train = X.iloc[train_index]
print('train size:',len(train_index))
X_valid = X.iloc[valid_index]
print('valid size:',len(valid_index))
y_train = y.iloc[train_index]
curr_labels=y_train['surface'].unique()
X_train=group_id_drop(X_train)
 X_valid=group_id_drop(X_valid)
 print('used labels:',len(curr_labels),' [',','.join([str(c)for c in curr_labels]),']')
 y_valid = y.iloc[valid_index]
d_train = xgb.DMatrix(data=X_train, label=y_train, feature_names=X_train.columns)
 d_valid = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X_valid.columns)
 watchlist = [(d_train, 'train'),(d_valid, 'valid')]
perf=dict()
model = xgb.train(dtrain=d_train, num_boost_round=NR_EPOCHS, evals=watchlist,
early_stopping_rounds=early_stop, verbose_eval=verbose_eval, params=params,
evals_result=perf)
 valid_pred = model.predict(xgb.DMatrix(X_valid, feature_names=X_valid.columns), ntree_limit=model.best_ntree_limit)
 TWG=T.drop('group_id',axis=1)
 test_pred = model.predict(xgb.DMatrix(TWG, feature_names=TWG.columns), ntree_limit=model.best_ntree_limit)
 oof_train_preds[valid_index] = valid_pred.argmax(axis=1)
 tst_preds[:,i] = test_pred.argmax(axis=1)
 perf_dict_list = perf_dict_list + [perf]
print('Accuracy =',accuracy_score(target['surface'][valid_index], valid_pred.argmax(axis=1)))
 i += 1
return model, oof_train_preds, tst_preds, perf_dict_list<split>
acoustic_data_small = []
for seg_id in tqdm(submission.index):
seg = pd.read_csv('.. /input/test/' + seg_id + '.csv')
 acoustic_data_small.append(seg['acoustic_data'][::50])
def xgb_train_groups_based(params, X, y, T):
oof_train_preds = np.zeros(X.shape[0])
 tst_preds = np.zeros(( T.shape[0],CV_STEPS))
perf_dict_list=[]
i=0
splitter=folds.split(groups,dummy)
 for(tr_gr_idx, va_gr_idx)in(splitter):
print('-'*20, i, '-'*20)
 tr_groups=groups[tr_gr_idx]
va_groups=groups[va_gr_idx]
X_train = X[X['group_id'].isin(tr_groups)]
X_train.drop('group_id',axis=1,inplace=True)
 train_index = X_train.index
print('train size:',len(train_index))
X_valid = X[X['group_id'].isin(va_groups)]
X_valid.drop('group_id',axis=1,inplace=True)
 valid_index = X_valid.index
print('valid size:',len(valid_index))
y_train = y.iloc[train_index]
curr_labels=y_train['surface'].unique()
print('used labels:',len(curr_labels))
y_valid = y.iloc[valid_index]
d_train = xgb.DMatrix(data=X_train, label=y_train, feature_names=X_train.columns)
 d_valid = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X_valid.columns)
 watchlist = [(d_train, 'train'),(d_valid, 'valid')]
perf=dict()
model = xgb.train(dtrain=d_train, num_boost_round=NR_EPOCHS, evals=watchlist,
early_stopping_rounds=early_stop, verbose_eval=verbose_eval, params=params,
evals_result=perf)
 valid_pred = model.predict(xgb.DMatrix(X_valid, feature_names=X_valid.columns), ntree_limit=model.best_ntree_limit)
 TWG=T.drop('group_id',axis=1)
 test_pred = model.predict(xgb.DMatrix(TWG, feature_names=TWG.columns), ntree_limit=model.best_ntree_limit)
 oof_train_preds[valid_index] = valid_pred.argmax(axis=1)
 tst_preds[:,i] = test_pred.argmax(axis=1)
 perf_dict_list = perf_dict_list + [perf]
print('Accuracy =',accuracy_score(target['surface'][valid_index], valid_pred.argmax(axis=1)))
 i += 1
return model, oof_train_preds, tst_preds, perf_dict_list<compute_train_metric>
del acoustic_data_small
del time_to_failure_small
gc.collect()
%%time
model0, oof_preds_xgb0, tst_preds_xgb0, perf = xgb_train_groups_based(xgb_params, X,y,T )<predict_on_test>
submission.to_csv('submission.csv')
 print("Submission data saved to csv." )