Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<predict_on_test>
import numpy as np
import pandas as pd
test_predict = []
test_data = get_test_dataset(TEST_FILES, training = False)
 test_audio = test_data.map(lambda frames, recording_id: frames)
 for fold in range(N_FOLDS):
model.load_weights(f'./RFCX_model_fold {fold}.h5')
 test_predict.append(model.predict(test_audio, verbose = 1))<load_from_csv>
print(tf.__version__ )
SUB = pd.read_csv('.. /input/rfcx-species-audio-detection/sample_submission.csv')
 predict = np.array(test_predict ).reshape(N_FOLDS, len(SUB), 60 // TIME, params.num_classes)
 predict = np.mean(np.max(predict ,axis = 2), axis = 0)
 recording_id = test_data.map(lambda frames, recording_id: recording_id ).unbatch()
test_ids = next(iter(recording_id.batch(len(SUB)* 60 // TIME)) ).numpy().astype('U' ).reshape(len(SUB), 60 // TIME)
 pred_df = pd.DataFrame({ 'recording_id' : test_ids[:, 0],
**{f's{i}' : predict[:, i] for i in range(params.num_classes)} } )<save_to_csv>
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )
pred_df.sort_values('recording_id', inplace = True)
 pred_df.to_csv('submission.csv', index = False )<import_modules>
EPOCHS = 75
DROP_RATE = 0.4
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
RESIZE_SIZE =(224, 224)
 TTA_COUNT = 10
MODEL_VERSION = 'V17'
import pandas as pd, numpy as np
import os<define_variables>
DATA_DIR = '.. /input/digit-recognizer/'
def read_df(file_name):
file_path = DATA_DIR + file_name
data_df = pd.read_csv(file_path)
 return data_df
paths = [
".. /input/rfcx-best-performing-public-kernels/kkiller_inference-tpu-rfcx-audio-detection-fast_0861.csv",
".. /input/rfcx-best-performing-public-kernels/submission_khoongweihao_0845.csv",
]
weights = np.array([0.6, 0.4])
 sum(weights )<define_variables>
train_df = read_df('train.csv')
 train_df
cols = [f"s{i}" for i in range(24)]<load_from_csv>
IMAGE_SIZE =(28, 28)
 IMAGE_SHAPE =(*IMAGE_SIZE, 1)
 def get_images_from(data_df):
pixels_df = data_df.loc[ : , 'pixel0':'pixel783' ]
pixels_array = pixels_df.to_numpy(dtype=np.uint8)
 images = pixels_array.reshape(-1, *IMAGE_SHAPE)
 return images
scores = []
for path in paths:
df = pd.read_csv(path ).sort_values("recording_id" ).reset_index(drop=True)
 score = np.empty(( len(df), 24))
o = df[cols].values.argsort(1)
 score[np.arange(len(df)) [:, None], o] = np.arange(24)[None]
scores.append(score)
 scores = np.stack(scores)
 scores.shape<compute_test_metric>
def get_labels_from(data_df):
labels = data_df['label'].to_numpy(dtype=np.int32)
 return labels
sub_score = np.sum(scores*weights[:, None, None], 0)
 print(sub_score.shape)
 sub_score<prepare_output>
X = get_images_from(train_df)
 y = get_labels_from(train_df)
 print(X.shape)
 print(y.shape )
sub = pd.DataFrame(sub_score, columns=cols)
 sub["recording_id"] = df["recording_id"]
sub = sub[["recording_id"] + cols]
print(sub.shape)
 sub.head()<save_to_csv>
RESIZE_SHAPE =(*RESIZE_SIZE, 1)
 def resize_image(orig_np):
reshape_np = np.reshape(orig_np, IMAGE_SIZE)
 orig_im = Image.fromarray(reshape_np)
 resized_im = orig_im.resize(RESIZE_SIZE, Image.LANCZOS)
 resized_np = np.asarray(resized_im, dtype=np.uint8)
 resized_reshaped_np = np.reshape(resized_np, RESIZE_SHAPE)
 return resized_reshaped_np
sub.to_csv("submission.csv", index=False )<import_modules>
def resize_images(orig_nps):
n_images = orig_nps.shape[0]
resized_shape =(n_images, *RESIZE_SHAPE)
 resized_nps = np.empty(resized_shape, dtype=np.uint8)
 for i in range(n_images):
if i % 100 == 0:
print('.', end='', flush=True)
 x_np = orig_nps[i]
resized_nps[i] = resize_image(x_np)
 print()
return resized_nps
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import keras
import math<load_from_csv>
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))
test = pd.read_csv('.. /input/quickdraw-doodle-recognition/test_simplified.csv')
 print(test.shape)
 test.head()<categorify>
def do_transform(image,label):
DIM = RESIZE_SIZE[0]
XDIM = DIM%2
rot = 10.* tf.random.normal([1],dtype='float32')
 shr = 5.* tf.random.normal([1],dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 w_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 h_shift = DIM * 0.05 * tf.random.normal([1],dtype='float32')
 w_shift = DIM * 0.05 * tf.random.normal([1],dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2,-DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2,DIM//2),[DIM])
 z = tf.ones([DIM*DIM],dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m,tf.cast(idx,dtype='float32'))
idx2 = K.cast(idx2,dtype='int32')
 idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image,tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,1]),label
def img_to_np(img_str, ht, wt, lw, pad):
strokes = eval(img_str)
 ht_ = ht - 2*pad
wt_ = wt - 2*pad
img = np.zeros(( ht, wt), np.uint8)
 for s in strokes:
sx =(np.array(s[0])* wt_ / 256 ).round().astype('int')+ pad
sy =(np.array(s[1])* ht_ / 256 ).round().astype('int')+ pad
for i in range(len(sx)- 1):
p1 =(sx[i], sy[i])
 p2 =(sx[i+1], sy[i+1])
 img = cv2.line(img, p1, p2,(255, 0, 0), lw, lineType=cv2.LINE_AA)
 return img 
<define_variables>
def randints(shape, minval, maxval):
return tf.random.uniform(
 shape=shape, minval=minval, maxval=maxval+1, dtype=tf.int32 )
test_imgs = np.zeros(shape =(test.shape[0], 64, 64, 1))<feature_engineering>
def make_range_mask(size, start, end):
indice = tf.range(size, dtype=tf.int32)
 start_mask =(start <= indice)
 end_mask =(indice <= end)
 range_mask = start_mask & end_mask
return range_mask
def make_region_mask(image_height, image_width, top, left, bottom, right):
row_mask = make_range_mask(image_height, top, bottom)
 col_mask = make_range_mask(image_width, left, right)
 region_mask = row_mask[..., tf.newaxis] & col_mask[ tf.newaxis,...]
reshaped_region_mask = region_mask[..., tf.newaxis]
return reshaped_region_mask
%%time
for i, row in test.iterrows() :
test_imgs[i,:,:,0] = img_to_np(row.drawing, 64, 64, 1, 2)/ 255 
<predict_on_test>
def do_cutout(orig_image, label):
mask_ratio = 0.5
image_shape = tf.shape(orig_image)
 image_height = image_shape[0]
image_width = image_shape[1]
mask_h = tf.cast(tf.cast(image_height, tf.float32)* mask_ratio, tf.int32)
 mask_w = tf.cast(tf.cast(image_width, tf.float32)* mask_ratio, tf.int32)
 mask_value = 0.0
top = randints([], -mask_h // 2, image_height - mask_h // 2)
 left = randints([], -mask_w // 2, image_width - mask_w // 2)
 bottom = top + mask_h
right = left + mask_w
cut_region = make_region_mask(
 image_height, image_width, top, left, bottom, right)
 cutout_image = tf.where(cut_region, mask_value, orig_image)
 return cutout_image, label
%%time
probs = cnn.predict(test_imgs)
 print(probs.shape )<concatenate>
unique_y = np.unique(y)
 label_count = len(unique_y)
 print(unique_y)
 print(label_count )
N_train = probs.shape[0]
top_3_probs = np.zeros(shape=(N_train, 3))
for i in range(N_train):
p = probs[i, :]
top_classes = np.argpartition(p, -3)[-3:]
top_classes = top_classes[np.argsort(p[top_classes])]
top_classes = np.flip(top_classes)
 top_probs = p[top_classes]
top_3_probs[i,:] = top_probs
print(top_3_probs[:10, :].round(2))
print(top_3_probs.shape)
 plt.figure(figsize=[10,6])
 for i in range(3):
plt.subplot(3,1,i+1)
 plt.hist(top_3_probs[:,i], color='orchid', edgecolor='k', bins = np.arange(0, 1.01, 0.025))
plt.yscale('log')
 plt.show()<sort_values>
AUTO = tf.data.experimental.AUTOTUNE
def make_dataset(
 X_np, y_np,
transform=False, cutout=False, repeat=False, shuffle=False):
def _cast_to_float(x, y):
return tf.cast(x, tf.float32), y
ds = tf.data.Dataset.from_tensor_slices(( X_np, y_np))
ds = ds.map(_cast_to_float, num_parallel_calls=AUTO)
 if shuffle:
ds = ds.shuffle(2048)
 if transform:
ds = ds.map(do_transform, num_parallel_calls=AUTO)
 if cutout:
ds = ds.map(do_cutout, num_parallel_calls=AUTO)
 ds = ds.batch(BATCH_SIZE)
 if repeat:
ds = ds.repeat()
return ds
N_train = probs.shape[0]
predictions = []
t = 0.35
for i in range(N_train):
p = probs[i, :]
top_classes = np.argpartition(p, -3)[-3:]
top_classes = top_classes[np.argsort(p[top_classes])]
top_classes = np.flip(top_classes)
 top_probs = p[top_classes]
sel = top_probs > t
sel[0] = True
predictions.append(top_classes[sel])
 print(len(predictions))<load_from_csv>
def make_train_ds(X_np, y_np):
ds = make_dataset(
 X_np, y_np, transform=True, cutout=True, repeat=True, shuffle=True)
 return ds
def make_val_ds(X_np, y_np):
ds = make_dataset(
 X_np, y_np, transform=False, cutout=False, repeat=False, shuffle=False)
 return ds
def make_test_ds(X_np):
y_np = np.zeros(X_np.shape[0], dtype=np.int32)
 ds = make_dataset(
 X_np, y_np, transform=True, cutout=False, repeat=False, shuffle=False)
 return ds
submission = pd.read_csv('.. /input/quickdraw-doodle-recognition/sample_submission.csv')
 submission.head()<load_from_csv>
NFOLD = 5
k_fold = StratifiedKFold(n_splits=NFOLD)
 fold_index_list = []
for train_index, val_index in k_fold.split(X_resized, y):
fold_index_list.append(( train_index, val_index))
def get_fold(fold_i):
train_index, val_index = fold_index_list[fold_i]
X_train, y_train = X_resized[train_index], y[train_index]
X_val, y_val = X_resized[val_index], y[val_index]
train_ds = make_train_ds(X_train, y_train)
 val_ds = make_val_ds(X_val, y_val)
 return train_ds, val_ds
label_lookup_df = pd.read_csv('.. /input/models-and-submissions/Quick Draw Models/label_lookup.csv')
 label_lookup = {k:v for k,v in zip(label_lookup_df.index.values, label_lookup_df.label.values)}
label_lookup[0]<feature_engineering>
def calc_steps_per_epoch(fold_i):
train_index, val_index = fold_index_list[fold_i]
steps_per_epoch =(len(train_index)+ BATCH_SIZE - 1)// BATCH_SIZE
return steps_per_epoch
%%time
for i in range(N_train):
classes = predictions[i]
words_list = [label_lookup[c] for c in classes]
words_string = ' '.join(words_list)
 submission.loc[i, 'word'] = words_string
submission.head()<save_to_csv>
MODEL_INPUT_SHAPE =(*RESIZE_SIZE, 3)
 def make_model(name):
with strategy.scope() :
inputs = L.Input(shape=RESIZE_SHAPE, name="input")
 scaled = L.Lambda(lambda v: v / 255.0, name='scaling' )(inputs)
 img_input = L.Concatenate(name='concat' )([scaled, scaled, scaled])
 x = ResNet50(
 include_top=False, weights='imagenet',
input_shape=MODEL_INPUT_SHAPE, pooling='avg' )(img_input)
 x = L.Dropout(DROP_RATE, name="dropout" )(x)
 outputs = L.Dense(label_count, activation='sigmoid', name='classify' )(x)
 model = M.Model(inputs=inputs, outputs=outputs, name=name)
 model.compile(
 optimizer='adam',
loss="sparse_categorical_crossentropy",
metrics=['accuracy'])
 return model
submission.to_csv('submission.csv', index=False )<save_to_csv>
LR_START = 0.00001
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = min(5, EPOCHS // 5)
 LR_SUSTAIN_EPOCHS = 0
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr =(LR_MAX - LR_START)/ LR_RAMPUP_EPOCHS * epoch + LR_START
elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
lr = LR_MAX
else:
decay_total_epochs = EPOCHS - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS
decay_epoch_index = epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS
phase = math.pi * decay_epoch_index / decay_total_epochs
cosine_decay = 0.5 *(1 + math.cos(phase))
lr =(LR_MAX - LR_MIN)* cosine_decay + LR_MIN
return lr
def make_lr_callback() :
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose = False)
 return lr_callback
rng = [i for i in range(EPOCHS)]
lr_y = [lrfn(x)for x in rng]
plt.figure(figsize=(10, 4))
plt.plot(rng, lr_y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".\
format(lr_y[0], max(lr_y), lr_y[-1]))
submission.to_csv('submission.csv', index=False )<load_pretrained>
def make_best_model_file_path(fold_i):
file_name = "best_model_{0}_{1}.hdf5".format(MODEL_VERSION, fold_i)
 return file_name
pd.set_option('display.max_columns', 100)
 pd.set_option('display.max_rows', 100)
 DATA_PATH = '.. /input/jane-street-market-prediction/'
NFOLDS = 5
TRAIN = False
CACHE_PATH = '.. /input/mlp012003weights'
def save_pickle(dic, save_path):
with open(save_path, 'wb')as f:
pickle.dump(dic, f)
 def load_pickle(load_path):
with open(load_path, 'rb')as f:
message_dict = pickle.load(f)
 return message_dict
feat_cols = [f'feature_{i}' for i in range(130)]
target_cols = ['action', 'action_1', 'action_2', 'action_3', 'action_4']
f_mean = np.load(f'{CACHE_PATH}/f_mean_online.npy')
 all_feat_cols = [col for col in feat_cols]
all_feat_cols.extend(['cross_41_42_43', 'cross_1_2'])
 class Model(nn.Module):
def __init__(self):
super(Model, self ).__init__()
self.batch_norm0 = nn.BatchNorm1d(len(all_feat_cols))
self.dropout0 = nn.Dropout(0.2)
 dropout_rate = 0.2
hidden_size = 256
self.dense1 = nn.Linear(len(all_feat_cols), hidden_size)
 self.batch_norm1 = nn.BatchNorm1d(hidden_size)
 self.dropout1 = nn.Dropout(dropout_rate)
 self.dense2 = nn.Linear(hidden_size+len(all_feat_cols), hidden_size)
 self.batch_norm2 = nn.BatchNorm1d(hidden_size)
 self.dropout2 = nn.Dropout(dropout_rate)
 self.dense3 = nn.Linear(hidden_size+hidden_size, hidden_size)
 self.batch_norm3 = nn.BatchNorm1d(hidden_size)
 self.dropout3 = nn.Dropout(dropout_rate)
 self.dense4 = nn.Linear(hidden_size+hidden_size, hidden_size)
 self.batch_norm4 = nn.BatchNorm1d(hidden_size)
 self.dropout4 = nn.Dropout(dropout_rate)
 self.dense5 = nn.Linear(hidden_size+hidden_size, len(target_cols))
self.Relu = nn.ReLU(inplace=True)
 self.PReLU = nn.PReLU()
self.LeakyReLU = nn.LeakyReLU(negative_slope=0.01, inplace=True)
 self.RReLU = nn.RReLU()
def forward(self, x):
x = self.batch_norm0(x)
 x = self.dropout0(x)
 x1 = self.dense1(x)
 x1 = self.batch_norm1(x1)
 x1 = self.LeakyReLU(x1)
 x1 = self.dropout1(x1)
 x = torch.cat([x, x1], 1)
 x2 = self.dense2(x)
 x2 = self.batch_norm2(x2)
 x2 = self.LeakyReLU(x2)
 x2 = self.dropout2(x2)
 x = torch.cat([x1, x2], 1)
 x3 = self.dense3(x)
 x3 = self.batch_norm3(x3)
 x3 = self.LeakyReLU(x3)
 x3 = self.dropout3(x3)
 x = torch.cat([x2, x3], 1)
 x4 = self.dense4(x)
 x4 = self.batch_norm4(x4)
 x4 = self.LeakyReLU(x4)
 x4 = self.dropout4(x4)
 x = torch.cat([x3, x4], 1)
 x = self.dense5(x)
 return x
if True:
device = torch.device("cuda:0")
 model_list = []
tmp = np.zeros(len(feat_cols))
for _fold in range(NFOLDS):
torch.cuda.empty_cache()
model = Model()
model.to(device)
 model_weights = f"{CACHE_PATH}/online_model{_fold}.pth"
model.load_state_dict(torch.load(model_weights))
model.eval()
model_list.append(model)
<choose_model_class>
def make_model_check_point(fold_i):
best_model_file_path = make_best_model_file_path(fold_i)
 return ModelCheckpoint(
 best_model_file_path, monitor='val_accuracy', mode='max',
verbose=0, save_best_only=True, save_weights_only=False, period=1 )
SEED = 1111
np.random.seed(SEED)
 def create_mlp(
 num_columns, num_labels, hidden_units, dropout_rates, label_smoothing, learning_rate 
):
inp = tf.keras.layers.Input(shape=(num_columns,))
x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Dropout(dropout_rates[0] )(x)
 for i in range(len(hidden_units)) :
x = tf.keras.layers.Dense(hidden_units[i] )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Activation(tf.keras.activations.swish )(x)
 x = tf.keras.layers.Dropout(dropout_rates[i + 1] )(x)
 x = tf.keras.layers.Dense(num_labels )(x)
 out = tf.keras.layers.Activation("sigmoid" )(x)
 model = tf.keras.models.Model(inputs=inp, outputs=out)
 model.compile(
 optimizer=tfa.optimizers.RectifiedAdam(learning_rate=learning_rate),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics=tf.keras.metrics.AUC(name="AUC"), 
)
 return model
epochs = 200
batch_size = 4096
hidden_units = [160, 160, 160]
dropout_rates = [0.2, 0.2, 0.2, 0.2]
label_smoothing = 1e-2
learning_rate = 1e-3
tf.keras.backend.clear_session()
tf.random.set_seed(SEED)
 clf = create_mlp(
 len(feat_cols), 5, hidden_units, dropout_rates, label_smoothing, learning_rate 
)
 clf.load_weights('.. /input/jane-street-with-keras-nn-overfit/model.h5')
 tf_models = [clf]<statistical_test>
initial_weights_file_path = "initial_weights.hdf5"
model.save_weights(initial_weights_file_path )
if True:
env = janestreet.make_env()
env_iter = env.iter_test()
for(test_df, pred_df)in tqdm(env_iter):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, feat_cols].values
if np.isnan(x_tt.sum()):
x_tt = np.nan_to_num(x_tt)+ np.isnan(x_tt)* f_mean
cross_41_42_43 = x_tt[:, 41] + x_tt[:, 42] + x_tt[:, 43]
cross_1_2 = x_tt[:, 1] /(x_tt[:, 2] + 1e-5)
 feature_inp = np.concatenate((
x_tt,
np.array(cross_41_42_43 ).reshape(x_tt.shape[0], 1),
np.array(cross_1_2 ).reshape(x_tt.shape[0], 1), 
), axis=1)
 torch_pred = np.zeros(( 1, len(target_cols)))
 for model in model_list:
torch_pred += model(torch.tensor(feature_inp, dtype=torch.float ).to(device)).sigmoid().detach().cpu().numpy() / NFOLDS
torch_pred = np.median(torch_pred)
 tf_pred = np.median(np.mean([model(x_tt, training = False ).numpy() for model in tf_models],axis=0))
pred = torch_pred * 0.5 + tf_pred * 0.5
pred_df.action = np.where(pred >= 0.5, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )<import_modules>
history_list = []
for fold_i in range(NFOLD):
print("
print("
train_ds, val_ds = get_fold(fold_i)
 steps_per_epoch = calc_steps_per_epoch(fold_i)
 lr_callback = make_lr_callback()
check_point = make_model_check_point(fold_i)
 model.load_weights(initial_weights_file_path)
 history = model.fit(
 train_ds, epochs=EPOCHS, steps_per_epoch=steps_per_epoch,
validation_data=val_ds,
callbacks=[lr_callback, check_point], verbose=0)
 history_list.append(history)
 best_acc = max(history.history['accuracy'])
 best_val_acc = max(history.history['val_accuracy'])
 print(
 "Fold {0}: accuracy={1:.5f}, val_accuracy={2:.5f}".format(
 fold_i, best_acc, best_val_acc))
import pandas as pd
import gc
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import StratifiedKFold<load_from_csv>
test_df = read_df('test.csv')
 test_df
train = pd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv')
 train = reduce_mem_usage(train)
 features = [c for c in train.columns if 'feature' in c]
train = train.astype({c: np.float32 for c in train.select_dtypes(include='float16' ).columns})
 train = train.fillna(train.mean())
 f_mean = np.mean(train[features[1:]].values,axis=0)
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train.weight != 0]<prepare_x_and_y>
def make_val_pred_file_path(fold_i):
file_name = "val_pred_{0}_{1}".format(MODEL_VERSION, fold_i)
 return file_name
n_folds = 5
seed = 42
ScaleData = False
if(ScaleData):
scaler = MinMaxScaler(feature_range =(0, 1)).fit(train[features])
 train.loc[:,features] = scaler.transform(train[features])
 X = train[features]
y =(train['resp'].values > 0 ).astype(int)
 X_train = train[train['date'] < 400][features]
X_test = train[train['date'] >= 400][features]
train['action'] =(( train['weight'].values * train['resp'].values)> 0 ).astype('int')
 y_train = train[train['date'] < 400]['action']
y_test = train[train['date'] >= 400]['action']
X_train = train[features]
y_train = train['action']<count_unique_values>
model_pred = np.zeros(( X_test_resized.shape[0], label_count))
for fold_i in range(NFOLD):
best_model_file_path = make_best_model_file_path(fold_i)
 model.load_weights(best_model_file_path)
 train_ds, val_ds = get_fold(fold_i)
 val_pred = model.predict(val_ds)
 val_pred_file_name = make_val_pred_file_path(fold_i)
 np.save(val_pred_file_name, val_pred)
 test_ds = make_test_ds(X_test_resized)
 print("Fold {0}".format(fold_i), end='', flush=True)
 for i in range(TTA_COUNT):
print('.', end='', flush=True)
 model_pred += model.predict(test_ds)
 print()
y_pred = np.argmax(model_pred, axis=1)
 print(y_pred.shape )
unique, counts = np.unique(y_test, return_counts=True)
 dict(zip(unique, counts))<train_model>
submission_df = read_df('sample_submission.csv')
 submission_df
<normalization><EOS> 
submission_df['Label'] = y_pred
submission_df.to_csv("submission.csv", index=False)
 !head submission.csv