Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<choose_model_class>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )
def get_net() :
net = EfficientNet.from_pretrained('efficientnet-b2')
 net._fc = nn.Linear(in_features=1408, out_features=4, bias=True)
 return net
net = get_net().cuda()<init_hyperparams>
passengerId = test_df.PassengerId
train_df = train_df.drop('PassengerId', axis = 1)
 test_df = test_df.drop('PassengerId', axis = 1 )
class TrainGlobalConfig:
num_workers = 4
batch_size = 22
n_epochs = 3
lr = 0.001
verbose = True
verbose_step = 1
step_scheduler = False
validation_scheduler = True
SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau
scheduler_params = dict(
 mode='min',
factor=0.6,
patience=1,
verbose=False,
threshold=0.001,
threshold_mode='abs',
cooldown=0,
min_lr=1e-9,
eps=1e-09 
)
<load_pretrained>
train_df['Title'] = train_df.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
 train_df['Title'].value_counts()
def run_training() :
device = torch.device('cuda:0')
 train_loader = torch.utils.data.DataLoader(
 train_dataset,
sampler=BalanceClassSampler(labels=train_dataset.get_labels() , mode="downsampling"),
batch_size=TrainGlobalConfig.batch_size,
pin_memory=False,
drop_last=True,
num_workers=TrainGlobalConfig.num_workers, 
)
 val_loader = torch.utils.data.DataLoader(
 validation_dataset,
batch_size=TrainGlobalConfig.batch_size,
num_workers=TrainGlobalConfig.num_workers,
shuffle=False,
sampler=SequentialSampler(validation_dataset),
pin_memory=False, 
)
 fitter = Fitter(model=net, device=device, config=TrainGlobalConfig)
 fitter.load(".. /input/alaska2-checkpoint/last-checkpoint.bin")
 fitter.fit(train_loader, val_loader )<set_options>
norm_titles = {
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royalty",
"Dona": "Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Lady" : "Royalty"
}
train_df.Title = train_df.Title.map(norm_titles)
 train_df.Title.value_counts()
!nvidia-smi<train_model>
train_grouped = train_df.groupby(['Sex','Title','Pclass'])
 train_grouped.Age.mean()
run_training()<load_from_csv>
train_df.Age = train_grouped.Age.apply(lambda x: x.fillna(x.mean()))
file = open('.. /input/alaska2-checkpoint/log.txt', 'r')
 for line in file.readlines() :
print(line[:-1])
 file.close()<load_pretrained>
train_df.Age.isnull().sum()
checkpoint = torch.load('.. /input/alaska2-checkpoint/last-checkpoint.bin')
 net.load_state_dict(checkpoint['model_state_dict']);
net.eval() ;<data_type_conversions>
test_df['Title'] = test_df.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
 test_df.Title = test_df.Title.map(norm_titles)
 test_grouped = test_df.groupby(['Sex','Title','Pclass'])
 test_df.Age = test_grouped.Age.apply(lambda x: x.fillna(x.mean()))
test_df.Age.isnull().sum()
test_df.Title.value_counts()
class DatasetSubmissionRetriever(Dataset):
def __init__(self, image_names, transforms=None):
super().__init__()
self.image_names = image_names
self.transforms = transforms
def __getitem__(self, index: int):
image_name = self.image_names[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/Test/{image_name}', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image_name, image
def __len__(self)-> int:
return self.image_names.shape[0]<define_variables>
most_embarked = train_df.Embarked.value_counts().index[0]
train_df.Embarked = train_df.Embarked.fillna(most_embarked)
 train_df.Fare = train_df.Fare.fillna(train_df.Fare.median())
 train_df.Cabin = train_df.Cabin.fillna('U')
 train_df.Cabin = train_df.Cabin.map(lambda x: x[0])
 train_df['Cabin'] = train_df.Cabin.replace({'T': 'G'})
 train_df.Cabin.value_counts()
results = []
for mode in range(0, 4):
dataset = DatasetSubmissionRetriever(
 image_names=np.array([path.split('/')[-1] for path in glob('.. /input/alaska2-image-steganalysis/Test/*.jpg')]),
transforms=get_test_transforms(mode), 
)
 data_loader = DataLoader(
 dataset,
batch_size=8,
shuffle=False,
num_workers=2,
drop_last=False, 
)
 result = {'Id': [], 'Label': []}
for step,(image_names, images)in enumerate(data_loader):
print(step, end='\r')
 y_pred = net(images.cuda())
 y_pred = 1 - nn.functional.softmax(y_pred, dim=1 ).data.cpu().numpy() [:,0]
result['Id'].extend(image_names)
 result['Label'].extend(y_pred)
 results.append(result )<prepare_output>
test_df.Cabin = test_df.Cabin.fillna('U')
 most_embarked = test_df.Embarked.value_counts().index[0]
test_df.Embarked = test_df.Embarked.fillna(most_embarked)
 test_df.Fare = test_df.Fare.fillna(train_df.Fare.median())
 test_df['Cabin'] = test_df.Cabin.apply(lambda name: name[0])
 test_df.Cabin.value_counts()
submissions = []
for mode in range(0,4):
submission = pd.DataFrame(results[mode])
 submissions.append(submission )<save_to_csv>
train_df.Sex = train_df.Sex.map({"male": 0, "female":1})
 pclass_dummies = pd.get_dummies(train_df.Pclass, prefix="Pclass")
 title_dummies = pd.get_dummies(train_df.Title, prefix="Title")
 cabin_dummies = pd.get_dummies(train_df.Cabin, prefix="Cabin")
 embarked_dummies = pd.get_dummies(train_df.Embarked, prefix="Embarked")
 train_dummies = pd.concat([train_df, pclass_dummies, title_dummies, cabin_dummies, embarked_dummies], axis=1)
 train_dummies.drop(['Pclass', 'Title', 'Cabin', 'Embarked', 'Name', 'Ticket'], axis=1, inplace=True)
 train_dummies.head()
for mode in range(0,4):
submissions[mode].to_csv(f'submission_{mode}.csv', index=False )<save_to_csv>
test_df.Sex = test_df.Sex.map({"male": 0, "female":1})
 pclass_dummies = pd.get_dummies(test_df.Pclass, prefix="Pclass")
 title_dummies = pd.get_dummies(test_df.Title, prefix="Title")
 cabin_dummies = pd.get_dummies(test_df.Cabin, prefix="Cabin")
 embarked_dummies = pd.get_dummies(test_df.Embarked, prefix="Embarked")
 test_dummies = pd.concat([test_df, pclass_dummies, title_dummies, cabin_dummies, embarked_dummies], axis=1)
 test_dummies.drop(['Pclass', 'Title', 'Cabin', 'Embarked', 'Name', 'Ticket'], axis=1, inplace=True)
 test_dummies.head()
submissions[0]['Label'] =(submissions[0]['Label']*3 + submissions[1]['Label'] + submissions[2]['Label'] + submissions[3]['Label'])/ 6
submissions[0].to_csv(f'submission.csv', index=False )<install_modules>
import operator
import math
import random
import numpy as np
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
from deap import gp
!pip install -q efficientnet_pytorch > /dev/null<set_options>
def mydeap(mungedtrain, epochs):
inputs = mungedtrain.drop('Survived', axis = 1 ).values.tolist()
outputs = mungedtrain['Survived'].values.tolist()
def protectedDiv(left, right):
try:
return left / right
except ZeroDivisionError:
return 1
pset = gp.PrimitiveSet("MAIN", 26)
 pset.addPrimitive(operator.add, 2)
 pset.addPrimitive(operator.sub, 2)
 pset.addPrimitive(operator.mul, 2)
 pset.addPrimitive(protectedDiv, 2)
 pset.addPrimitive(operator.neg, 1)
 pset.addPrimitive(math.cos, 1)
 pset.addPrimitive(math.sin, 1)
 pset.addPrimitive(max, 2)
 pset.addPrimitive(min, 2)
 pset.renameArguments(ARG0='x1')
 pset.renameArguments(ARG1='x2')
 pset.renameArguments(ARG2='x3')
 pset.renameArguments(ARG3='x4')
 pset.renameArguments(ARG4='x5')
 pset.renameArguments(ARG5='x6')
 pset.renameArguments(ARG6='x7')
 pset.renameArguments(ARG7='x8')
 pset.renameArguments(ARG8='x9')
 pset.renameArguments(ARG9='x10')
 pset.renameArguments(ARG10='x11')
 pset.renameArguments(ARG11='x12')
 pset.renameArguments(ARG12='x13')
 pset.renameArguments(ARG13='x14')
 pset.renameArguments(ARG14='x15')
 pset.renameArguments(ARG15='x16')
 pset.renameArguments(ARG16='x17')
 pset.renameArguments(ARG17='x18')
 pset.renameArguments(ARG18='x19')
 pset.renameArguments(ARG19='x20')
 pset.renameArguments(ARG20='x21')
 pset.renameArguments(ARG21='x22')
 pset.renameArguments(ARG22='x23')
 pset.renameArguments(ARG23='x24')
 pset.renameArguments(ARG24='x25')
 pset.renameArguments(ARG25='x26')
 creator.create("FitnessMin", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin)
 toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=3)
 toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
 toolbox.register("population", tools.initRepeat, list, toolbox.individual)
 toolbox.register("compile", gp.compile, pset=pset)
 def evalSymbReg(individual):
func = toolbox.compile(expr=individual)
 return sum(round(1.-(1./(1.+np.exp(-func(*in_)))))== out for in_, out in zip(inputs, outputs)) /len(mungedtrain),
toolbox.register("evaluate", evalSymbReg)
 toolbox.register("select", tools.selTournament, tournsize=3)
 toolbox.register("mate", gp.cxOnePoint)
 toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
 toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
 toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
random.seed(318)
 pop = toolbox.population(n=300)
 hof = tools.HallOfFame(1)
 stats_fit = tools.Statistics(lambda ind: ind.fitness.values)
 stats_size = tools.Statistics(len)
 mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size)
 mstats.register("avg", np.mean)
 mstats.register("std", np.std)
 mstats.register("min", np.min)
 mstats.register("max", np.max)
 pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.2, epochs, stats=mstats,
halloffame=hof, verbose=True)
 print(hof[0])
 func2 =toolbox.compile(expr=hof[0])
 return func2
<normalization><EOS> 
if __name__ == "__main__":
train = train_dummies
test = test_dummies.columns
mungedtrain = train_dummies.astype(float)
 GeneticFunction = mydeap(mungedtrain, epochs = 100)
 mytrain = mungedtrain.drop('Survived', axis = 1 ).values.tolist()
trainPredictions = Outputs(np.array([GeneticFunction(*x)for x in mytrain]))
print(accuracy_score(mungedtrain.Survived.astype(int),trainPredictions.astype(int)))
 mungedtest = test_dummies.astype(float)
 mytest = mungedtest.values.tolist()
testPredictions = Outputs(np.array([GeneticFunction(*x)for x in mytest]))
pdtest = pd.DataFrame({'PassengerId': passengerId,
'Survived': testPredictions.astype(int)})
 pdtest.to_csv('gptest.csv', index=False )