Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<choose_model_class>
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.layers import Dense, Dropout, Flatten,GlobalAveragePooling2D,BatchNormalization, Activation
import glob
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from tensorflow import keras
import os
model_lw = model_f(input_shape=(size_original,size_original,3))
model_lw.compile(optimizer = optimizers.Adam(lr=0.0001), loss = "binary_crossentropy", metrics = ["accuracy"] )<train_model>
policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
 mixed_precision.set_policy(policy )
filepath = model_name + ".h5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 train_history = model_lw.fit_generator(train_generator,
epochs=epochs,
steps_per_epoch = train_steps,
validation_data = val_generator,
validation_steps = val_steps,
shuffle = True,
callbacks=[checkpoint])
 print(str(train_history.history), file = open("Model_Details.txt", "w"))<load_pretrained>
SEED = 42
DEBUG = False
os.environ['PYTHONHASHSEED'] = str(SEED)
 np.random.seed(SEED)
 tf.random.set_seed(SEED )
model_lw.load_weights(model_name + ".h5" )<save_to_csv>
df = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 df.head()
predict = model_lw.predict_generator(test_generator, steps = len(test_generator), workers=0, verbose=1)
 df_test = pd.read_csv('.. /input/sample_submission.csv')
 df_test['label'] = predict
df_test.to_csv(model_name + "_result.csv", index=False)
 df_test.head 
<set_options>
df['path'] = '.. /input/cassava-leaf-disease-classification/train_images/' + df['image_id']
df.label.value_counts(normalize=True)* 100
base_dir = '.. /input/'
print(os.listdir(base_dir))
plt.style.use("ggplot")
<load_from_csv>
if DEBUG:
_, df = train_test_split(df, test_size = 0.1, random_state=SEED, shuffle=True, stratify=df['label'])
full_train_df = pd.read_csv(".. /input/train_labels.csv")
 full_train_df.head()<define_variables>
X_train, X_valid = train_test_split(df, test_size = 0.1, random_state=SEED, shuffle=True )
SAMPLE_SIZE = 80000
train_path = '.. /input/train/'
test_path = '.. /input/test/'
df_negatives = full_train_df[full_train_df['label'] == 0].sample(SAMPLE_SIZE, random_state=42)
 df_positives = full_train_df[full_train_df['label'] == 1].sample(SAMPLE_SIZE, random_state=42)
 train_df = sklearn.utils.shuffle(pd.concat([df_positives, df_negatives], axis=0 ).reset_index(drop=True))
train_df.shape<categorify>
train_ds = tf.data.Dataset.from_tensor_slices(( X_train.path.values, X_train.label.values))
valid_ds = tf.data.Dataset.from_tensor_slices(( X_valid.path.values, X_valid.label.values))
class CreateDataset(Dataset):
def __init__(self, df_data, data_dir = './', transform=None):
super().__init__()
self.df = df_data.values
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, index):
img_name,label = self.df[index]
img_path = os.path.join(self.data_dir, img_name+'.tif')
 image = cv2.imread(img_path)
 if self.transform is not None:
image = self.transform(image)
 return image, label<create_dataframe>
for path, label in train_ds.take(5):
print('Path: {}, Label: {}'.format(path, label))
transforms_train = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomHorizontalFlip(p=0.4),
transforms.RandomVerticalFlip(p=0.4),
transforms.RandomRotation(20),
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 train_data = CreateDataset(df_data=train_df, data_dir=train_path, transform=transforms_train )<define_variables>
for path, label in valid_ds.take(5):
print('Path: {}, Label: {}'.format(path, label))
batch_size = 128
valid_size = 0.1
num_train = len(train_data)
 indices = list(range(num_train))
split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 train_loader = DataLoader(train_data, batch_size=batch_size, sampler=train_sampler)
 valid_loader = DataLoader(train_data, batch_size=batch_size, sampler=valid_sampler )<load_from_csv>
AUTOTUNE = tf.data.experimental.AUTOTUNE
target_size_dim = 512
transforms_test = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 sample_sub = pd.read_csv(".. /input/sample_submission.csv")
 test_data = CreateDataset(df_data=sample_sub, data_dir=test_path, transform=transforms_test)
 test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False )<choose_model_class>
def process_data_train(image_path, label):
img = tf.io.read_file(image_path)
 img = tf.image.decode_jpeg(img, channels=3)
 img = tf.image.random_brightness(img, 0.3)
 img = tf.image.random_flip_left_right(img, seed=None)
 img = tf.image.random_flip_up_down(img)
 img = tf.image.random_crop(img, size=[target_size_dim, target_size_dim, 3])
 return img, label
def process_data_valid(image_path, label):
img = tf.io.read_file(image_path)
 img = tf.image.decode_jpeg(img, channels=3)
 img = tf.image.resize(img, [target_size_dim,target_size_dim])
 return img, label
class CNN(nn.Module):
def __init__(self):
super(CNN,self ).__init__()
self.conv1=nn.Sequential(
 nn.Conv2d(in_channels=3,out_channels=32,kernel_size=3,stride=1,padding=0),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.conv2=nn.Sequential(
 nn.Conv2d(in_channels=32,out_channels=64,kernel_size=2,stride=1,padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.conv3=nn.Sequential(
 nn.Conv2d(in_channels=64,out_channels=128,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.conv4=nn.Sequential(
 nn.Conv2d(in_channels=128,out_channels=256,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.conv5=nn.Sequential(
 nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.MaxPool2d(2,2))
self.dropout2d = nn.Dropout2d()
self.fc=nn.Sequential(
 nn.Linear(512*3*3,1024),
nn.ReLU(inplace=True),
nn.Dropout(0.4),
nn.Linear(1024,512),
nn.Dropout(0.4),
nn.Linear(512, 1),
nn.Sigmoid())
 def forward(self,x):

 x=self.conv1(x)
 x=self.conv2(x)
 x=self.conv3(x)
 x=self.conv4(x)
 x=self.conv5(x)
 x=x.view(x.shape[0],-1)
 x=self.fc(x)
 return x<train_model>
train_ds = train_ds.map(process_data_train, num_parallel_calls=AUTOTUNE)
 valid_ds = valid_ds.map(process_data_valid, num_parallel_calls=AUTOTUNE )
train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('CUDA is not available.Training on CPU...')
 else:
print('CUDA is available! Training on GPU...' )<choose_model_class>
for image, label in train_ds.take(1):
plt.imshow(image.numpy().astype('uint8'))
plt.show()
print("Image shape: ", image.numpy().shape)
 print("Label: ", label.numpy() )
model = CNN()
print(model)
 if train_on_gpu: model.cuda()<train_model>
def configure_for_performance(ds, batch_size = 32):
ds = ds.cache('/kaggle/dump.tfcache')
 ds = ds.shuffle(buffer_size=1024)
 ds = ds.batch(batch_size)
 ds = ds.prefetch(buffer_size=AUTOTUNE)
 return ds
batch_size = 8
train_ds_batch = configure_for_performance(train_ds, batch_size)
 valid_ds_batch = valid_ds.batch(batch_size )
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
 print("Number of trainable parameters:
{}".format(pytorch_total_params))<choose_model_class>
image_batch, label_batch = next(iter(train_ds_batch))
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters() , lr=0.00015 )<init_hyperparams>
plt.figure(figsize=(10, 10))
for i in range(8):
ax = plt.subplot(4, 4, i + 1)
 plt.imshow(image_batch[i].numpy().astype("uint8"))
label = label_batch[i].numpy()
plt.title(label)
 plt.axis("off" )
n_epochs = 20
valid_loss_min = np.Inf
train_losses = []
valid_losses = []
val_auc = []
test_accuracies = []
valid_accuracies = []
auc_epoch = []
for epoch in range(1, n_epochs+1):
train_loss = 0.0
valid_loss = 0.0
model.train()
for data, target in train_loader:
if train_on_gpu:
data, target = data.cuda() , target.cuda().float()
target = target.view(-1, 1)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item() *data.size(0)
 model.eval()
for data, target in valid_loader:
if train_on_gpu:
data, target = data.cuda() , target.cuda().float()
target = target.view(-1, 1)
 output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item() *data.size(0)
 y_actual = target.data.cpu().numpy()
y_pred = output[:,-1].detach().cpu().numpy()
val_auc.append(roc_auc_score(y_actual, y_pred))
train_loss = train_loss/len(train_loader.sampler)
 valid_loss = valid_loss/len(valid_loader.sampler)
 valid_auc = np.mean(val_auc)
 auc_epoch.append(np.mean(val_auc))
train_losses.append(train_loss)
 valid_losses.append(valid_loss)
 print('Epoch: {} | Training Loss: {:.6f} | Validation Loss: {:.6f} | Validation AUC: {:.4f}'.format(
 epoch, train_loss, valid_loss, valid_auc))
if valid_loss <= valid_loss_min:
print('Validation loss decreased({:.6f} --> {:.6f} ).Saving model...'.format(
 valid_loss_min,
valid_loss))
torch.save(model.state_dict() , 'best_model.pt')
 valid_loss_min = valid_loss<load_pretrained>
data_augmentation = keras.Sequential(
 [
tf.keras.layers.experimental.preprocessing.RandomRotation(0.2, interpolation='nearest'),
tf.keras.layers.experimental.preprocessing.RandomContrast(( 0.2))
]
)
model.load_state_dict(torch.load('best_model.pt'))<predict_on_test>
plt.figure(figsize=(10, 10))
for i in range(8):
augmented_images = data_augmentation(image_batch)
 ax = plt.subplot(4, 4, i + 1)
 plt.imshow(augmented_images[i].numpy().astype("uint8"))
label = label_batch[i].numpy()
plt.title(label)
 plt.axis("off" )
model.eval()
preds = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.cuda() , target.cuda()
output = model(data)
 pr = output.detach().cpu().numpy()
for i in pr:
preds.append(i)
 sample_sub['label'] = preds<feature_engineering>
from tensorflow.keras.applications import EfficientNetB4
for i in range(len(sample_sub)) :
sample_sub.label[i] = np.float(sample_sub.label[i] )<save_to_csv>
def load_pretrained_model(weights_path, drop_connect, target_size_dim, layers_to_unfreeze=5):
model = EfficientNetB4(
 weights=None,
include_top=False,
drop_connect_rate=0.4 
)
 model.load_weights(weights_path)
 model.trainable = True
if DEBUG:
for layer in model.layers:
pass
return model
def build_my_model(base_model, optimizer, loss='sparse_categorical_crossentropy', metrics = ['sparse_categorical_accuracy']):
inputs = tf.keras.layers.Input(shape=(target_size_dim, target_size_dim, 3))
x = data_augmentation(inputs)
 outputs_eff = base_model(x)
 global_avg_pooling = GlobalAveragePooling2D()(outputs_eff)
 dense_1= Dense(256 )(global_avg_pooling)
 bn_1 = BatchNormalization()(dense_1)
 activation = Activation('relu' )(bn_1)
 dropout = Dropout(0.3 )(activation)
 dense_2 = Dense(5 )(dropout)
 outputs = Activation('softmax', dtype='float32', name='predictions' )(dense_2)
 my_model = tf.keras.Model(inputs, outputs)
 my_model.compile(
 optimizer=optimizer,
loss=loss,
metrics=metrics 
)
 return my_model
sample_sub.to_csv('submission.csv', index=False)
 sample_sub.head()<import_modules>
!pip install livelossplot 
<define_variables>
model_weights_path = '.. /input/effnetb4-ns/effnetb4_ns.h5'
model_weights_path
TRAINING_LOGS_FILE = "training_logs.csv"
MODEL_FILE = "histopathologic_cancer_detector.h5"
ROC_PLOT_FILE = "roc.png"
KAGGLE_SUBMISSION_FILE = "kaggle_submission.csv"
INPUT_DIR = '.. /input/'<define_variables>
drop_rate = 0.4
base_model = load_pretrained_model(model_weights_path, drop_rate, target_size_dim)
 optimizer = tf.keras.optimizers.Adam(lr = 1e-4)
 my_model = build_my_model(base_model, optimizer)
 my_model.summary()
SAMPLE_COUNT =50000
TRAINING_RATIO = 0.9
IMAGE_SIZE = 96
EPOCHS = 12
BATCH_SIZE = 128
VERBOSITY = 1
TESTING_BATCH_SIZE = 3000<feature_engineering>
weight_path_save = 'best_model.hdf5'
last_weight_path = 'last_model.hdf5'
checkpoint = ModelCheckpoint(weight_path_save,
monitor= 'val_loss',
verbose=1,
save_best_only=True,
mode= 'min',
save_weights_only = False)
 checkpoint_last = ModelCheckpoint(last_weight_path,
monitor= 'val_loss',
verbose=1,
save_best_only=False,
mode= 'min',
save_weights_only = False)
 early = EarlyStopping(monitor= 'val_loss',
mode= 'min',
patience=5)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.8, patience=2, verbose=1, mode='auto', epsilon=0.0001, cooldown=5, min_lr=0.00001)
 callbacks_list = [checkpoint, checkpoint_last, early, reduceLROnPlat]
training_dir = INPUT_DIR + 'train/'
data_frame = pd.DataFrame({'path': glob(os.path.join(training_dir,'*.tif')) })
 data_frame['id'] = data_frame.path.map(lambda x: x.split('/')[3].split('.')[0])
 labels = pd.read_csv(INPUT_DIR + 'train_labels.csv')
 data_frame = data_frame.merge(labels, on = 'id')
 negatives = data_frame[data_frame.label == 0].sample(SAMPLE_COUNT)
 positives = data_frame[data_frame.label == 1].sample(SAMPLE_COUNT)
 data_frame = pd.concat([negatives, positives] ).reset_index()
data_frame = data_frame[['path', 'id', 'label']]
data_frame['image'] = data_frame['path'].map(imread)
 training_path = '.. /training1'
validation_path = '.. /validation1'
for folder in [training_path, validation_path]:
for subfolder in ['0', '1']:
path = os.path.join(folder, subfolder)
 os.makedirs(path, exist_ok=True)
 training, validation = train_test_split(data_frame, train_size=TRAINING_RATIO, stratify=data_frame['label'])
 data_frame.set_index('id', inplace=True)
 for images_and_path in [(training, training_path),(validation, validation_path)]:
images = images_and_path[0]
path = images_and_path[1]
for image in images['id'].values:
file_name = image + '.tif'
label = str(data_frame.loc[image,'label'])
 destination = os.path.join(path, label, file_name)
 if not os.path.exists(destination):
source = os.path.join(INPUT_DIR + 'train', file_name)
 shutil.copyfile(source, destination )<choose_model_class>
if DEBUG:
epochs = 3
else:
epochs = 15
print(f"Model will train for {epochs} epochs" )
training_data_generator = ImageDataGenerator(rescale=1./255,
horizontal_flip=True,
vertical_flip=True,
rotation_range=180,
zoom_range=[1, 1.5],
fill_mode='reflect',
width_shift_range=0.3,
height_shift_range=0.3,
shear_range=0.3,
channel_shift_range=0.3 )<train_model>
if DEBUG:
history = my_model.fit(train_ds_batch,
validation_data = valid_ds_batch,
epochs = epochs,
callbacks = callbacks_list,
steps_per_epoch = 1, 
)
 else:
history = my_model.fit(train_ds_batch,
validation_data = valid_ds_batch,
epochs = epochs,
callbacks = callbacks_list
)
training_generator = training_data_generator.flow_from_directory(training_path,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=BATCH_SIZE,
class_mode='binary')
 validation_generator = ImageDataGenerator(rescale=1./255 ).flow_from_directory(validation_path,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=BATCH_SIZE,
class_mode='binary' )<choose_model_class>
from sklearn.metrics import confusion_matrix, classification_report
input_shape =(96, 96, 3)
 inputs = Input(input_shape)
 dense = DenseNet121(include_top=False, input_shape=input_shape)
 outputs = GlobalAveragePooling2D()(dense(inputs))
outputs = Dropout(0.5 )(outputs)
 outputs = Dense(1, activation='sigmoid' )(outputs)
 model = Model(inputs, outputs)
 model.compile(optimizer=Adam(lr=0.0001, decay=0.00001),
loss='binary_crossentropy',
metrics=['accuracy'])
 model.summary()<train_model>
my_model.load_weights(weight_path_save )
history = model.fit_generator(training_generator,
steps_per_epoch=len(training_generator),
validation_data=validation_generator,
validation_steps=len(validation_generator),
epochs=EPOCHS,
verbose=VERBOSITY,shuffle=True,
callbacks=[PlotLossesKeras() ,
ModelCheckpoint(MODEL_FILE,
monitor='val_acc',
verbose=VERBOSITY,
save_best_only=True,
mode='max'),
CSVLogger(TRAINING_LOGS_FILE,
append=False,
separator=';')])
 model.load_weights(MODEL_FILE )<define_variables>
pred_valid_y = my_model.predict(valid_ds_batch, workers=4, verbose = True)
 pred_valid_y_labels = np.argmax(pred_valid_y, axis=-1 )
roc_validation_generator = ImageDataGenerator(rescale=1./255,shear_range=0.3,zoom_range=[1,1.5],
horizontal_flip=True,
rotation_range=10.,
width_shift_range = 0.3,
height_shift_range = 0.3 ).flow_from_directory(validation_path,target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=BATCH_SIZE,
class_mode='binary',
shuffle=False)
 predictions = model.predict_generator(roc_validation_generator, steps=len(roc_validation_generator), verbose=VERBOSITY)
 false_positive_rate, true_positive_rate, threshold = roc_curve(roc_validation_generator.classes, predictions)
 area_under_curve = auc(false_positive_rate, true_positive_rate)
 plt.plot([0, 1], [0, 1], 'k--')
 plt.plot(false_positive_rate, true_positive_rate, label='AUC = {:.3f}'.format(area_under_curve))
plt.xlabel('False positive rate')
 plt.ylabel('True positive rate')
 plt.title('ROC curve')
 plt.legend(loc='best')
 plt.savefig(ROC_PLOT_FILE, bbox_inches='tight')
 plt.show()<prepare_output>
valid_labels = np.concatenate([y.numpy() for x, y in valid_ds_batch], axis=0 )
testing_files = glob(os.path.join(INPUT_DIR+'test/','*.tif'))
submission = pd.DataFrame()
for index in range(0, len(testing_files), TESTING_BATCH_SIZE):
data_frame = pd.DataFrame({'path': testing_files[index:index+TESTING_BATCH_SIZE]})
 data_frame['id'] = data_frame.path.map(lambda x: x.split('/')[3].split(".")[0])
 data_frame['image'] = data_frame['path'].map(imread)
 images = np.stack(data_frame.image, axis=0)
 predicted_labels = [model.predict(np.expand_dims(image/255.0, axis=0)) [0][0] for image in images]
predictions = np.array(predicted_labels)
 data_frame['label'] = predictions
submission = pd.concat([submission, data_frame[["id", "label"]]])
 submission.to_csv(KAGGLE_SUBMISSION_FILE, index=False, header=True )<set_options>
print(classification_report(valid_labels, pred_valid_y_labels))
warnings.filterwarnings("ignore" )<define_variables>
print(confusion_matrix(valid_labels, pred_valid_y_labels))
def seed_everything(seed=0):
random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 np.random.seed(seed )<define_variables>
import glob
SEED = 1337
seed_everything(SEED )<data_type_conversions>
test_images = glob.glob('.. /input/cassava-leaf-disease-classification/test_images/*.jpg')
 print(test_images )
def reduce_memory_usage(df):
numerics = ["int16", "int32", "int64", "float64"]
for col, col_type in df.dtypes.iteritems() :
best_type = None
if col_type == "object":
df[col] = df[col].astype("category")
 best_type = "category"
elif col_type in numerics:
downcast = "integer" if "int" in str(col_type)else "float"
df[col] = pd.to_numeric(df[col], downcast=downcast)
 best_type = df[col].dtype.name
return df<load_pretrained>
df_test = pd.DataFrame(np.array(test_images), columns=['Path'])
 df_test.head()
%%time
train = pd.read_pickle(".. /input/ieeecis-fraud-detection-merged/train.pkl")
 test = pd.read_pickle(".. /input/ieeecis-fraud-detection-merged/test.pkl" )<data_type_conversions>
preds = []
for i in range(5):
pred_test = my_model.predict(test_ds, workers=16, verbose=1)
 preds.append(pred_test)
%%time
for df in [train, test]:
df["day"] = np.floor(( df["TransactionDT"] /(3600 * 24)- 1)% 7 ).astype(int)
 df["hour"] =(np.floor(df["TransactionDT"] / 3600)% 24 ).astype(int)
 df["TransactionAmt_log"] = np.log(df["TransactionAmt"])
 df["TransactionAmt_int"] = df["TransactionAmt"].astype(int)
 df["TransactionAmt_dec"] =(1000 *(df["TransactionAmt"] - df["TransactionAmt_int"])).astype(int)
 df["card_id"] = df["card1"].astype(str)+ "__" + df["card2"].astype(str)+ "__" + df["card3"].astype(str)+ "__" + df["card4"].astype(str)+ "__" + df["card5"].astype(str)
 for a, b in itertools.combinations(["card1", "card2", "card3", "card4", "card5", "addr1", "addr2", "dist1", "dist2"], 2):
df["{}__{}".format(a, b)] = df[a].astype(str)+ "__" + df[b].astype(str)
 df[["P_emaildomain_0", "P_emaildomain_1", "P_emaildomain_2"]] = df["P_emaildomain"].str.split(".", expand=True)
 df[["R_emaildomain_0", "R_emaildomain_1", "R_emaildomain_2"]] = df["R_emaildomain"].str.split(".", expand=True)
 df[["operating_system_0", "operating_system_1", "operating_system_2", "operating_system_3"]] = df["id_30"].str.split(" ", expand=True)
 df[["browser_0", "browser_1", "browser_2", "browser_3"]] = df["id_31"].str.split(" ", expand=True)
 df[["resolution_width", "resolution_height"]] = df["id_33"].str.split("x", expand=True)
 df[["DeviceInfo_0", "DeviceInfo_1", "DeviceInfo_2", "DeviceInfo_3", "DeviceInfo_4"]] = df["DeviceInfo"].str.split(r"[ -/_]", expand=True)[[0, 1, 2, 3, 4]]
df["TransactionAmt_to_mean_card1"] = df["TransactionAmt"] / df.groupby(["card1"])["TransactionAmt"].transform("mean")
 df["TransactionAmt_to_mean_card4"] = df["TransactionAmt"] / df.groupby(["card4"])["TransactionAmt"].transform("mean")
 df["TransactionAmt_to_std_card1"] = df["TransactionAmt"] / df.groupby(["card1"])["TransactionAmt"].transform("std")
 df["TransactionAmt_to_std_card4"] = df["TransactionAmt"] / df.groupby(["card4"])["TransactionAmt"].transform("std")
 df["id_02_to_mean_card1"] = df["id_02"] / df.groupby(["card1"])["id_02"].transform("mean")
 df["id_02_to_mean_card4"] = df["id_02"] / df.groupby(["card4"])["id_02"].transform("mean")
 df["id_02_to_std_card1"] = df["id_02"] / df.groupby(["card1"])["id_02"].transform("std")
 df["id_02_to_std_card4"] = df["id_02"] / df.groupby(["card4"])["id_02"].transform("std")
 df["D15_to_mean_card1"] = df["D15"] / df.groupby(["card1"])["D15"].transform("mean")
 df["D15_to_mean_card4"] = df["D15"] / df.groupby(["card4"])["D15"].transform("mean")
 df["D15_to_std_card1"] = df["D15"] / df.groupby(["card1"])["D15"].transform("std")
 df["D15_to_std_card4"] = df["D15"] / df.groupby(["card4"])["D15"].transform("std")
 df["D15_to_mean_addr1"] = df["D15"] / df.groupby(["addr1"])["D15"].transform("mean")
 df["D15_to_mean_addr2"] = df["D15"] / df.groupby(["addr2"])["D15"].transform("mean")
 df["D15_to_std_addr1"] = df["D15"] / df.groupby(["addr1"])["D15"].transform("std")
 df["D15_to_std_addr2"] = df["D15"] / df.groupby(["addr2"])["D15"].transform("std")
 df["device_manufacturer"] = df["DeviceInfo"].map(lambda x: get_device_manufacturer(x))<define_variables>
pred_y = np.mean(preds, axis=0 )
default_categorical_columns = ["ProductCD", "card1", "card2", "card3", "card4", "card5", "card6", "addr1", "addr2", "P_emaildomain", "R_emaildomain", "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "id_12", "id_13", "id_14", "id_15", "id_16", "id_17", "id_18", "id_19", "id_20", "id_21", "id_22", "id_23", "id_24", "id_25", "id_26", "id_27", "id_28", "id_29", "id_30", "id_31", "id_32", "id_33", "id_34", "id_35", "id_36", "id_37", "id_38", "DeviceType", "DeviceInfo"]<data_type_conversions>
df_test['image_id'] = df_test.Path.str.split('/' ).str[-1]
df_test['label'] = pred_y_argmax
df_test= df_test[['image_id','label']]
df_test.head()
<drop_column><EOS> 
df_test.to_csv('submission.csv', index=False )