Kaggle competition: Airbnb New User Bookings
def show_wav(sample, ax):
wav = sample["audio_wav"].numpy()
rate = SR
ax.plot(np.arange(len(wav)) / rate, wav)
 ax.set_title(
 sample["recording_id"].numpy().decode()
+("/%d" % sample["species_id"])
 +("TP" if sample["is_tp"] else "FP"))
return Audio(( wav * 2**15 ).astype(np.int16), rate=rate)
 fig, ax = plt.subplots(figsize=(15, 3))
show_wav(next(iter(parsed_dataset)) , ax )<normalization>
session=pd.read_csv("/kaggle/input/airbnb-recruiting-new-user-bookings/sessions.csv.zip")
 print(session.shape)
 session.head()
@tf.function
def _wav_to_spec(x):
mel_power = cfg['data_params']['mel_power']
stfts = tf.signal.stft(x["audio_wav"], frame_length=2048, frame_step=512, fft_length=2048)
 spectrograms = tf.abs(stfts)** mel_power
num_spectrogram_bins = stfts.shape[-1]
lower_edge_hertz, upper_edge_hertz, num_mel_bins = FMIN, FMAX, N_MEL
linear_to_mel_weight_matrix = tf.signal.linear_to_mel_weight_matrix(
 num_mel_bins, num_spectrogram_bins, SR, lower_edge_hertz,
upper_edge_hertz)
 mel_spectrograms = tf.tensordot(
 spectrograms, linear_to_mel_weight_matrix, 1)
 mel_spectrograms.set_shape(spectrograms.shape[:-1].concatenate(
 linear_to_mel_weight_matrix.shape[-1:]))
log_mel_spectrograms = tf.math.log(mel_spectrograms + 1e-6)
 y = {
'audio_spec': tf.transpose(log_mel_spectrograms),
}
y.update(x)
 return y
spec_dataset = parsed_dataset.filter(_filtTP ).map(_cut_wav ).map(_wav_to_spec )<categorify>
train_user=pd.read_csv("/kaggle/input/airbnb-recruiting-new-user-bookings/train_users_2.csv.zip")
 print(train_user.shape)
 train_user.head()
@tf.function
def _create_annot(x):
targ = tf.one_hot(x["species_id"], CLASS_N, on_value=x["is_tp"], off_value=0)
 return {
'input': x["audio_spec"],
'target': tf.cast(targ, tf.float32)
 }
annot_dataset = spec_dataset.map(_create_annot )<normalization>
test_user=pd.read_csv("/kaggle/input/airbnb-recruiting-new-user-bookings/test_users.csv.zip")
 print(test_user.shape)
 test_user.head()
def _preprocess_img(x, training=False, test=False):
image = tf.expand_dims(x, axis=-1)
 image = tf.image.resize(image, [HEIGHT, WIDTH])
 image = tf.image.per_image_standardization(image)
 @tf.function
def _specaugment(image):
ERASE_TIME = 50
ERASE_MEL = 16
image = tf.expand_dims(image, axis=0)
 xoff = tf.random.uniform([2], minval=ERASE_TIME//2, maxval=WIDTH-ERASE_TIME//2, dtype=tf.int32)
 xsize = tf.random.uniform([2], minval=ERASE_TIME//2, maxval=ERASE_TIME, dtype=tf.int32)
 yoff = tf.random.uniform([2], minval=ERASE_MEL//2, maxval=HEIGHT-ERASE_MEL//2, dtype=tf.int32)
 ysize = tf.random.uniform([2], minval=ERASE_MEL//2, maxval=ERASE_MEL, dtype=tf.int32)
 image = tf.squeeze(image, axis=0)
 return image
if training:
gau = tf.keras.layers.GaussianNoise(0.3)
 image = tf.cond(tf.random.uniform([])< 0.5, lambda: gau(image, training=True), lambda: image)
 image = tf.image.random_brightness(image, 0.2)
 image = tf.image.random_flip_left_right(image)
 if test:
pass
image =(image - tf.reduce_min(image)) /(tf.reduce_max(image)- tf.reduce_min(image)) * 255.0
image = tf.image.grayscale_to_rgb(image)
 image = cfg['model_params']['arch_preprocess'](image)
 return image
@tf.function
def _preprocess(x):
image = _preprocess_img(x['input'], training=True, test=False)
 return(image, x["target"])
 @tf.function
def _preprocess_val(x):
image = _preprocess_img(x['input'], training=False, test=False)
 return(image, x["target"])
 @tf.function
def _preprocess_test(x):
image = _preprocess_img(x['audio_spec'], training=False, test=True)
 return(image, x["recording_id"] )<choose_model_class>
class Custom_Proccess(BaseEstimator, TransformerMixin):
def transform(self,X,y=None):
X.gender.replace('-unknown-', 'OTHER', inplace=True)
 X['age'].fillna(-1,inplace=True)
 X['timestamp_first_active']=X['timestamp_first_active'].apply(lambda s:datetime(year=int(str(s)[0:4]), month=int(str(s)[4:6]),
day=int(str(s)[6:8])).strftime('%Y-%m-%d'))
X['timestamp_first_active']=X['timestamp_first_active'].astype('datetime64[ns]')
 X['age']=X['age'].astype('int64')
 X['date_account_created']=X['date_account_created'].astype('datetime64[ns]')
 X['dac_year']=X['date_account_created'].dt.year
X['dac_month']=X['date_account_created'].dt.month
X['dac_day']=X['date_account_created'].dt.day
X['tfa_year']=X['timestamp_first_active'].dt.year
X['tfa_month']=X['timestamp_first_active'].dt.month
X['tfa_day']=X['timestamp_first_active'].dt.day
X.signup_app.replace(['iOS','Android'],'SmartDevice',inplace=True)
 X.drop(['date_first_booking','date_account_created','timestamp_first_active','first_device_type','first_browser'],axis=1,inplace=True)
 return X
def fit(self, X, y=None, **fit_params):
return self
def create_model() :
def Classifier(shape_):
backbone = cfg['model_params']['arch'](( shape_), include_top=False, weights='imagenet')
 def cbr(x, out_layer, kernel, stride, dilation):
x = Conv2D(out_layer, kernel_size=kernel, dilation_rate=dilation, strides=stride, padding="same" )(x)
 x = BatchNormalization()(x)
 x = Activation("relu" )(x)
 return x
def wave_block(x, filters, kernel_size, n):
dilation_rates = [2**i for i in range(n)]
x = Conv2D(filters = filters,
kernel_size = 1,
padding = 'same' )(x)
 res_x = x
for dilation_rate in dilation_rates:
tanh_out = Conv2D(filters = filters,
kernel_size = kernel_size,
padding = 'same',
activation = 'tanh',
dilation_rate = dilation_rate )(x)
 sigm_out = Conv2D(filters = filters,
kernel_size = kernel_size,
padding = 'same',
activation = 'sigmoid',
dilation_rate = dilation_rate )(x)
 x = Multiply()([tanh_out, sigm_out])
 x = Conv2D(filters = filters,
kernel_size = 1,
padding = 'same' )(x)
 res_x = Add()([res_x, x])
 return res_x
def wavenet(layer):
x = cbr(layer, 192, 7, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 192, 3, 1)
 x = cbr(x, 96, 7, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 96, 3, 1)
 x = cbr(x, 48, 5, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 48, 3, 1)
 return x
def wavenet1(layer):
x = cbr(layer, 4, 7, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 3, 3, 1)
 x = cbr(x, 3, 7, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 16, 3, 1)
 x = cbr(x, 3, 5, 1, 1)
 return x
x0 = backbone
print('1')
 x1 = tf.keras.layers.GlobalAveragePooling2D()(x0.layers[-1].output)
 x3 = tf.keras.layers.GlobalAveragePooling2D()(x0.layers[-7].output)
 x5 = tf.keras.layers.GlobalAveragePooling2D()(x0.layers[-18].output)
 print('2')
 x1=wavenet(x0.layers[-1].output)
 x3=wavenet(x0.layers[-7].output)
 x5=wavenet(x0.layers[-18].output)
 x1 = tf.keras.layers.GlobalAveragePooling2D()(x1)
 x3 = tf.keras.layers.GlobalAveragePooling2D()(x3)
 x5 = tf.keras.layers.GlobalAveragePooling2D()(x5)
 print('4')
 x = tf.concat([x1,x3,x5],axis = 1)
 x = tf.keras.layers.Dropout(0.7 )(x)
 x = tf.keras.layers.Dense(192 )(x)
 x = tf.keras.layers.Dropout(0.4 )(x)
 output = tf.keras.layers.Softmax(dtype='float32' )(x)
 output =tf.keras.layers.Dense(CLASS_N )(x)
 print('5')
 model = tf.keras.models.Model(inputs = x0.input, outputs = output)
 return model
return Classifier([HEIGHT,WIDTH,3])
 model = create_model()
model.summary()<prepare_x_and_y>
session.drop(['action_detail','device_type'],inplace=True,axis=1)
 session.dropna(subset=['user_id','action'],inplace=True)
 session.action_type=session.action_type.fillna('Other')
 session.secs_elapsed=session.secs_elapsed.fillna(0 )
@tf.function
def _mixup(inp, targ):
indice = tf.range(len(inp))
indice = tf.random.shuffle(indice)
 sinp = tf.gather(inp, indice, axis=0)
 starg = tf.gather(targ, indice, axis=0)
 alpha = 0.2
t = tf.compat.v1.distributions.Beta(alpha, alpha ).sample([len(inp)])
 tx = tf.reshape(t, [-1, 1, 1, 1])
 ty = tf.reshape(t, [-1, 1])
 x = inp * tx + sinp *(1-tx)
 y = targ * ty + starg *(1-ty)
 return x, y<create_dataframe>
session_group=session.groupby(['user_id','action_type'] ).agg({'action':'count','secs_elapsed':'sum'} ).reset_index()
session_df=pd.get_dummies(session_group,columns=['action_type'] ).groupby(['user_id'] ).sum().reset_index()
session_df.head()
tfrecs = sorted(tf.io.gfile.glob(TRAIN_TFREC + '/*.tfrec'))
parsed_trainval =(tf.data.TFRecordDataset(tfrecs, num_parallel_reads=AUTOTUNE)
.map(_parse_function, num_parallel_calls=AUTOTUNE ).unbatch() 
.filter(_filtTP ).enumerate() )<data_type_conversions>
train_user_df=train_user.merge(session_df,left_on=['id'],
right_on=['user_id'],how='left' ).drop(['user_id'],axis=1 ).reset_index(drop=True)
 train_user_df.shape
indices = []
spid = []
recid = []
for i, sample in tqdm(parsed_trainval.prefetch(AUTOTUNE)) :
indices.append(i.numpy())
 spid.append(sample['species_id'].numpy())
 recid.append(sample['recording_id'].numpy().decode() )<create_dataframe>
test_user_df=test_user.merge(session_df,left_on=['id'],
right_on=['user_id'],how='left' ).drop(['user_id'],axis=1 ).reset_index(drop=True)
 test_user_df.shape
table = pd.DataFrame({'indices': indices, 'species_id': spid, 'recording_id': recid})
 table<categorify>
train_user_df.secs_elapsed.fillna(-1,inplace=True)
 train_user_df.action.fillna(-1,inplace=True)
 train_user_df.iloc[:,-11:]=train_user_df.iloc[:,-11:].fillna(-1)
 train_user_df['secs_elapsed']=train_user_df['secs_elapsed'].astype('int64')
 train_user_df['action']=train_user_df['action'].astype('int64' )
def create_idx_filter(indice):
@tf.function
def _filt(i, x):
return tf.reduce_any(indice == i)
 return _filt
@tf.function
def _remove_idx(i, x):
return x<categorify>
test_user_df.secs_elapsed.fillna(-1,inplace=True)
 test_user_df.action.fillna(-1,inplace=True)
 test_user_df.iloc[:,-11:]=test_user_df.iloc[:,-11:].fillna(-1)
 test_user_df['secs_elapsed']=test_user_df['secs_elapsed'].astype('int64')
 test_user_df['action']=test_user_df['action'].astype('int64' )
def create_train_dataset(batchsize, train_idx):
global parsed_trainval
parsed_train =(parsed_trainval 
.filter(create_idx_filter(train_idx)) 
.map(_remove_idx))
dataset =(parsed_train.cache() 
.shuffle(len(train_idx)) 
.repeat() 
.map(_cut_wav, num_parallel_calls=AUTOTUNE)
.map(_wav_to_spec, num_parallel_calls=AUTOTUNE)
.map(_create_annot, num_parallel_calls=AUTOTUNE)
.map(_preprocess, num_parallel_calls=AUTOTUNE)
.batch(batchsize))
if cfg['model_params']['mixup']:
dataset =(dataset.map(_mixup, num_parallel_calls=AUTOTUNE)
.prefetch(AUTOTUNE))
else:
dataset = dataset.prefetch(AUTOTUNE)
 return dataset
def create_val_dataset(batchsize, val_idx):
global parsed_trainval
parsed_val =(parsed_trainval 
.filter(create_idx_filter(val_idx)) 
.map(_remove_idx))
vdataset =(parsed_val 
.map(_cut_wav_val, num_parallel_calls=AUTOTUNE)
.map(_wav_to_spec, num_parallel_calls=AUTOTUNE)
.map(_create_annot, num_parallel_calls=AUTOTUNE)
.map(_preprocess_val, num_parallel_calls=AUTOTUNE)
.batch(8*strategy.num_replicas_in_sync)
.cache())
 return vdataset<categorify>
train_user_df.isnull().sum() /train_user_df.shape[0] *100
@tf.function
def _one_sample_positive_class_precisions(example):
y_true, y_pred = example
retrieved_classes = tf.argsort(y_pred, direction='DESCENDING')
 class_rankings = tf.argsort(retrieved_classes)
 retrieved_class_true = tf.gather(y_true, retrieved_classes)
 retrieved_cumulative_hits = tf.math.cumsum(tf.cast(retrieved_class_true, tf.float32))
idx = tf.where(y_true)[:, 0]
i = tf.boolean_mask(class_rankings, y_true)
 r = tf.gather(retrieved_cumulative_hits, i)
 c = 1 + tf.cast(i, tf.float32)
 precisions = r / c
dense = tf.scatter_nd(idx[:, None], precisions, [y_pred.shape[0]])
 return dense
class LWLRAP(tf.keras.metrics.Metric):
def __init__(self, num_classes, name='lwlrap'):
super().__init__(name=name)
 self._precisions = self.add_weight(
 name='per_class_cumulative_precision',
shape=[num_classes],
initializer='zeros', 
)
 self._counts = self.add_weight(
 name='per_class_cumulative_count',
shape=[num_classes],
initializer='zeros', 
)
 def update_state(self, y_true, y_pred, sample_weight=None):
precisions = tf.map_fn(
 fn=_one_sample_positive_class_precisions,
elems=(y_true, y_pred),
dtype=(tf.float32), 
)
 increments = tf.cast(precisions > 0, tf.float32)
 total_increments = tf.reduce_sum(increments, axis=0)
 total_precisions = tf.reduce_sum(precisions, axis=0)
 self._precisions.assign_add(total_precisions)
 self._counts.assign_add(total_increments)
 def result(self):
per_class_lwlrap = self._precisions / tf.maximum(self._counts, 1.0)
 per_class_weight = self._counts / tf.reduce_sum(self._counts)
 overall_lwlrap = tf.reduce_sum(per_class_lwlrap * per_class_weight)
 return overall_lwlrap
def reset_states(self):
self._precisions.assign(self._precisions * 0)
 self._counts.assign(self._counts * 0 )<categorify>
categorical_cols=[cname for cname in train_user_df.columns if cname not in ['id','date_account_created','date_first_booking','first_device_type','first_browser',
'timestamp_first_active','country_destination'] and
train_user_df[cname].dtype == "object"]
numerical_cols=[cname for cname in train_user_df.columns if cname not in ['id','date_account_created','date_first_booking','first_device_type','first_browser',
'timestamp_first_active','country_destination'] and
train_user_df[cname].dtype != "object"]
print("Categorical_cols -
",categorical_cols)
 print("Numerical_cols -
",numerical_cols )
def _parse_function_test(example_proto):
sample = tf.io.parse_single_example(example_proto, feature_description)
 wav, _ = tf.audio.decode_wav(sample['audio_wav'], desired_channels=1)
 @tf.function
def _cut_audio(i):
_sample = {
'audio_wav': tf.reshape(wav[i*SR*TIME:(i+1)*SR*TIME], [SR*TIME]),
'recording_id': sample['recording_id']
}
return _sample
return tf.map_fn(_cut_audio, tf.range(60//TIME), dtype={
'audio_wav': tf.float32,
'recording_id': tf.string
})
 def inference(model):
tdataset =(tf.data.TFRecordDataset(tf.io.gfile.glob(TEST_TFREC + '/*.tfrec'), num_parallel_reads=AUTOTUNE)
.map(_parse_function_test, num_parallel_calls=AUTOTUNE ).unbatch() 
.map(_wav_to_spec, num_parallel_calls=AUTOTUNE)
.map(_preprocess_test, num_parallel_calls=AUTOTUNE)
.batch(128*(60//TIME)).prefetch(AUTOTUNE))
rec_ids = []
probs = []
for inp, rec_id in tqdm(tdataset):
with strategy.scope() :
pred = model.predict_on_batch(tf.reshape(inp, [-1, HEIGHT, WIDTH, 3]))
prob = tf.sigmoid(pred)
 prob = tf.reduce_max(tf.reshape(prob, [-1, 60//TIME, CLASS_N]), axis=1)
 rec_id_stack = tf.reshape(rec_id, [-1, 60//TIME])
 for rec in rec_id.numpy() :
assert len(np.unique(rec)) == 1
rec_ids.append(rec_id_stack.numpy() [:,0])
 probs.append(prob.numpy())
 crec_ids = np.concatenate(rec_ids)
 cprobs = np.concatenate(probs)
 sub = pd.DataFrame({
'recording_id': list(map(lambda x: x.decode() , crec_ids.tolist())) ,
**{f's{i}': cprobs[:,i] for i in range(CLASS_N)}
})
 sub = sub.sort_values('recording_id')
 return sub<define_search_space>
categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 numerical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('cat', categorical_transformer, categorical_cols), 
('num', numerical_transformer, numerical_cols)
 ] )
class RMAC:
def __init__(self, shape, levels=3, power=None, overlap=0.4, norm_fm=False, sum_fm=True, verbose=False):
self.shape = shape
self.sum_fm = sum_fm
self.norm = norm_fm
self.power = power
steps = np.asarray([2, 3, 4, 5, 6, 7])
 B, H, W, D = shape
w = min([W, H])
 w2 = w // 2 - 1
b = np.asarray(( max(H, W)- w)) /(steps - 1);
idx = np.argmin(np.abs(((w**2 - w*b)/(w**2)) -overlap))
Wd = 0
Hd = 0
if H < W:
Wd = idx + 1
elif H > W:
Hd = idx + 1
self.regions = []
for l in range(levels):
wl = int(2 * w/(l+2)) ;
wl2 = int(wl / 2 - 1);
b = 0 if not(l + Wd)else(( W - wl)/(l + Wd))
cenW = np.asarray(np.floor(wl2 + np.asarray(range(l+Wd+1)) * b), dtype=np.int32)- wl2
b = 0 if not(l + Hd)else(( H - wl)/(l + Hd))
cenH = np.asarray(np.floor(wl2 + np.asarray(range(l+Hd+1)) * b), dtype=np.int32)- wl2
for i in cenH:
for j in cenW:
if i >= W or j >= H:
continue
ie = i+wl
je = j+wl
if ie >= W:
ie = W
if je >= H:
je = H
if ie - i < 1 or je - j < 1:
continue
self.regions.append(( i,j,ie,je))
if verbose:
print('RMAC regions = %s' % self.regions)
 def rmac(self, x):
y = []
for r in self.regions:
x_sliced = x[:, r[1]:r[3], r[0]:r[2], :]
if self.power is None:
x_maxed = tf.reduce_max(x_sliced, axis=(1,2))
else:
x_maxed = tf.reduce_mean(( x_sliced ** self.power), axis=(2,3)) **(1.0 / self.power)
 x_maxed = tf.pow(tf.reduce_mean(( tf.pow(x_sliced, self.power)) , axis=(2,3)) ,(1.0 / self.power))
y.append(x_maxed)
 y = tf.stack(y, axis=0)
 y = tf.transpose(y, [1,0,2])
 if self.norm:
y = tf.math.l2_normalize(y, 2)
 if self.sum_fm:
y = tf.reduce_mean(y, axis=(1))
return y<train_model>
test_id = test_user_df.id
test_X = test_user_df.drop(['id'], axis='columns' )
def train_and_inference(splits, split_id):
print("split_id", split_id)
 batchsize = cfg['model_params']['batchsize_per_tpu'] * strategy.num_replicas_in_sync
print("batchsize", batchsize)
 loss_fn = cfg['model_params']['loss']['fn'](from_logits=True, **cfg['model_params']['loss']['params'])
 idx_train_tf = tf.constant(splits[split_id][0])
 idx_val_tf = tf.constant(splits[split_id][1])
 dataset = create_train_dataset(batchsize, idx_train_tf)
 vdataset = create_val_dataset(batchsize, idx_val_tf)
 optimizer = cfg['model_params']['optim']['fn'](**cfg['model_params']['optim']['params'])
 with strategy.scope() :
model = create_model()
model.compile(optimizer=optimizer, loss=loss_fn, metrics=[LWLRAP(CLASS_N)])
 history = model.fit(dataset,
steps_per_epoch=cfg['model_params']['iteration_per_epoch'],
epochs=cfg['model_params']['epoch'],
validation_data=vdataset,
callbacks=[
tf.keras.callbacks.ReduceLROnPlateau(
 'val_lwlrap', patience=10 
),
tf.keras.callbacks.ModelCheckpoint(
 filepath='model_best_%d.h5' % split_id,
save_weights_only=True,
monitor='val_lwlrap',
mode='max',
save_best_only=True),
])
 plot_history(history, 'history_%d.png' % split_id)
 model.load_weights('model_best_%d.h5' % split_id)
 return inference(model )<load_from_csv>
labels = train_user_df.country_destination
le = LabelEncoder()
train_y = le.fit_transform(labels )
sub = pd.read_csv('.. /input/resnetwavenet851/submission.csv' )<load_from_csv>
train_X = train_user_df.drop(['id','country_destination'], axis='columns' )
sub2 = pd.read_csv('.. /input/automl-inference-audio-detection-soliset/submission.csv' )<feature_engineering>
def cross_validation_with_ndcg(pipe, X, y, scorer, cv=5):
skf = StratifiedKFold(n_splits=cv, shuffle=True, random_state=100)
 scores = []
for train_index, holdout_index in skf.split(X, y):
X_train, X_test = X.iloc[train_index], X.iloc[holdout_index]
y_train, y_test = y[train_index], y[holdout_index]
pipe.fit(X_train, y_train)
 predict = pipe.predict_proba(X_test)
 y_test = pd.get_dummies(y_test ).to_numpy()
score = scorer(y_test, predict)
 scores.append(round(score, 6))
print(f'{len(scores)} / {cv} DONE!', end='\r')
 return scores
sub.iloc[:,1:]=sub.iloc[:,1:]*0.30+sub2.iloc[:,1:]*0.70<save_to_csv>
n_estimaters_param=[50, 100, 200]
max_depth_param=[3,4,5]
learning_rate_param=[0.1,0.2]
params = [(x, y, z)for x in learning_rate_param for y in n_estimaters_param for z in max_depth_param]
result_list=[]
for learning_rates,n_estimaters, max_depth in params:
xg_model_ = XGBClassifier(max_depth=max_depth,learning_rate=learning_rates, n_estimators=n_estimaters,verbosity=0,
objective='multi:softprob',n_jobs=-1)
 search_pipe = Pipeline([ 
('customproccess',Custom_Proccess()), 
('preprocessor', preprocessor), 
("model", xg_model_)
 ])
 print(f'learning_rate: {learning_rates}, n_estimaters: {n_estimaters}, max_depth: {max_depth}')
 scores = cross_validation_with_ndcg(search_pipe, train_X, train_y, ndcg_score)
 result_list.append([learning_rates,n_estimaters,max_depth,np.mean(scores)] )
sub.to_csv("submission.csv", index=False )<load_from_csv>
result_df=pd.DataFrame(result_list,columns=['learning_rate','n_estimator','max_depth','mean_score'])
 result_df.sort_values(by='mean_score',ascending=False ).head(5 )
def read_train_csv(n_trains,path):
base_df=pd.DataFrame()
for i in range(n_trains):
base_df=base_df.append(pd.read_csv(path+f'train_n_{i}.csv'), ignore_index = True)
 return base_df
def find_best_train_csv(n_trains,path,n_bests):
base_df=pd.DataFrame()
for i in range(n_trains):
df=pd.read_csv(path+f'train_n_{i}.csv')
 base_df=base_df.append(df.nlargest(n_bests, ['best_score']), ignore_index = True)
 return base_df
def find_worst_train_csv(n_trains,path,n_worst):
base_df=pd.DataFrame()
for i in range(n_trains):
df=pd.read_csv(path+f'train_n_{i}.csv')
 base_df=base_df.append(df.nsmallest(n_worst, ['best_score']), ignore_index = True)
 return base_df
def average_csv(csv_list):
BLEND=pd.read_csv('.. /input/resnet-wavenet-my-best-single-model-ensemble/submission.csv')
 BLEND.iloc[:,1:]=pd.read_csv(csv_list[0])
 for sub_csv in csv_list[1:]:
BLEND.iloc[:,1:]+=pd.read_csv(sub_csv)
 BLEND.iloc[:,1:]=BLEND.iloc[:,1:]/len(csv_list)
 return BLEND
def create_csv_list(df,path):
df['CSV'] = path + df['CSV'].astype(str)
 return df["CSV"].tolist() 
<save_to_csv>
xg_model = XGBClassifier(max_depth=5,learning_rate=0.1, n_estimators=200,verbosity=0,objective='multi:softprob',n_jobs=-1)
 pipe = Pipeline([ 
('customproccess',Custom_Proccess()), 
('preprocessor', preprocessor), 
("model", xg_model)
 ])
 pipe.fit(train_X, train_y)
 predict = pipe.predict_proba(test_X )
print(KaggleDatasets().get_gcs_path('rfcx-species-audio-detection'))
n_trains=10
path='.. /input/rainforest-connection-species-audio-detectionv1/KAggle/'
train_data=read_train_csv(n_trains,path)
 print(train_data)
 BLEND_all=create_csv_list(train_data,path)
 BLEND_all=average_csv(BLEND_all)
 print(BLEND_all)
 BLEND_all.to_csv('submission.csv',index=False )<save_to_csv>
ids = []
cts = []
for i in range(len(test_id)) :
idx = test_id[i]
ids += [idx] * 5
cts += le.inverse_transform(np.argsort(predict[i])[::-1])[:5].tolist()
sub2 = pd.read_csv('.. /input/automl-inference-audio-detection-soliset/submission.csv')
 sub3 = pd.read_csv('.. /input/fork-of-bagging-rainforest/submission.csv')
 BLEND_all.iloc[:,1:]=(BLEND_all.iloc[:,1:]*0.30+sub3.iloc[:,1:]*0.70)*0.40+sub2.iloc[:,1:]*0.60/5
BLEND_all.to_csv('submission_ensemble.csv',index=False )<save_to_csv>
sub_df = pd.DataFrame(np.column_stack(( ids, cts)) , columns=['id', 'country'])
 sub_df.to_csv('sub-03.csv',index=False )