Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
random.seed(42)
learn.fit_one_cycle(2,max_lr = slice(1e-5/3,1e-5))<save_model>
warnings.filterwarnings("ignore" )
learn.save('stage-3' )<compute_test_metric>
def prim_set(names):
def pDiv(left, right):
try:
return left / right
except ZeroDivisionError:
return 1
def pPow(left, right):
try:
return abs(left)** min(float(right),8)
 except ZeroDivisionError:
return 1
except OverflowError:
return 1
def pSqrt(inp):
return math.sqrt(abs(inp))
def abs_(inp):
return abs(inp)
 pset = gp.PrimitiveSet("MAIN", len(names))
pset.addPrimitive(pDiv, 2)
 pset.addPrimitive(pPow, 2)
 pset.addPrimitive(pSqrt, 1)
 pset.addPrimitive(abs_, 1)
 pset.addPrimitive(operator.add, 2)
 pset.addPrimitive(operator.sub, 2)
 pset.addPrimitive(operator.mul, 2)
 pset.addPrimitive(operator.neg, 1)
 pset.addPrimitive(math.floor, 1)
 pset.addPrimitive(math.tanh, 1)
 pset.addPrimitive(math.sin, 1)
 pset.addPrimitive(math.cos, 1)
 pset.addPrimitive(max, 2)
 pset.addPrimitive(min, 2)
 for i, a in enumerate(pset.arguments):
new_name = names[i]
pset.arguments[i] = new_name
pset.mapping[new_name] = pset.mapping[a]
pset.mapping[new_name].value = new_name
del pset.mapping[a]
return pset
preds,y = learn.TTA()
acc = accuracy(preds, y)
 print('The validation accuracy is {} %.'.format(acc * 100))
pred_score = auc_score(preds,y ).item()
print('The validation AUC is {}.'.format(pred_score))<choose_model_class>
def get_title(name):
title_search = re.search('([A-Za-z]+)\.', name)
 if title_search:
return title_search.group(1)
 return ""
def PrepData(data):
data['IsNull'] = data.isnull().sum(axis=1)
 data['Ticket'] = data['Ticket'].str.lower().replace('\W', '')
 data.Sex.fillna(0, inplace=True)
 data.loc[data.Sex != 'male', 'Sex'] = 1
data.loc[data.Sex == 'male', 'Sex'] = 0
data['NameLen'] = data['Name'].apply(len)
 bin_num = 4
data['NameLen'] = pd.qcut(data['NameLen'], bin_num,labels=list(range(bin_num)) ).astype(float)
 data['Has_Cabin'] = data["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 data['FamilySize'] = data['SibSp'] + data['Parch'] + 1
data['isFamily'] = 1
data.loc[data['isFamily'] == 1, 'notAlone'] = 0
data['Title'] = data['Name'].apply(get_title)
 mapping = {'Mlle': 'Rare', 'Major': 'Mr', 'Col': 'Mr', 'Sir': 'Rare', 'Rev': 'Mr',
'Don': 'Mr', 'Mme': 'Rare', 'Jonkheer': 'Mr', 'Lady': 'Mrs',
'Capt': 'Mr', 'Countess': 'Rare', 'Ms': 'Miss', 'Dona': 'Rare'}
data.replace({'Title': mapping}, inplace=True)
 title_mapping = {"Mr": 0, "Miss": 1, "Mrs": 2, "Master": 3, "Rare": 4}
data['Title'] = data['Title'].map(title_mapping)
 data['Title'] = data['Title'].fillna(0)
 data['Embarked'].fillna(method='backfill', inplace=True)
 data['Embarked'] = data['Embarked'].map({'C': 1, 'Q': 2, 'S': 0} ).astype(int)
 data['Fare'] = data['Fare'].fillna(train['Fare'].median())
 data.loc[ data['Fare'] <= 7.91, 'Fare'] = 0
data.loc[(data['Fare'] > 7.91)&(data['Fare'] <= 14.454), 'Fare'] = 1
data.loc[(data['Fare'] > 14.454)&(data['Fare'] <= 31), 'Fare'] = 2
data.loc[ data['Fare'] > 31, 'Fare'] = 3
data['Fare'] = data['Fare'].astype(int)
 data.Cabin.fillna('0', inplace=True)
 data.loc[data.Cabin.str[0] == 'A', 'Cabin'] = 1
data.loc[data.Cabin.str[0] == 'B', 'Cabin'] = 2
data.loc[data.Cabin.str[0] == 'C', 'Cabin'] = 3
data.loc[data.Cabin.str[0] == 'D', 'Cabin'] = 4
data.loc[data.Cabin.str[0] == 'E', 'Cabin'] = 5
data.loc[data.Cabin.str[0] == 'F', 'Cabin'] = 6
data.loc[data.Cabin.str[0] == 'G', 'Cabin'] = 7
data.loc[data.Cabin.str[0] == 'T', 'Cabin'] = 8
data['Cabin'] = data['Cabin'].astype(int)
 grouped = data.groupby(['Sex','Pclass', 'Title'])
 data['Age'] = grouped['Age'].apply(lambda x: x.fillna(x.median()))
data['Age'] = data['Age'].astype(int)
 boy =(data['Name'].str.contains('Master')) |(( data['Sex']==0)&(data['Age']<13))
female = data['Sex']==1
boy_or_female = boy | female
n_ticket = data[boy_or_female].groupby('Ticket' ).Survived.count()
tick_surv = data[boy_or_female].groupby('Ticket' ).Survived.mean()
data['Boy'] =(data['Name'].str.contains('Master')) |(( data['Sex']==0)&(data['Age']<13))
data['NTicket'] = data['Ticket'].replace(n_ticket)
 data.loc[~data.Ticket.isin(n_ticket.index),'NTicket']=0
data['TicketSurv'] = data['Ticket'].replace(tick_surv)
 data.loc[~data.Ticket.isin(tick_surv.index),'TicketSurv']=0
data['TicketSurv'].fillna(0, inplace=True)
 data.loc[ data['Age'] <= 16, 'Age'] = 5
data.loc[(data['Age'] > 16)&(data['Age'] <= 32), 'Age'] = 1
data.loc[(data['Age'] > 32)&(data['Age'] <= 48), 'Age'] = 2
data.loc[(data['Age'] > 48)&(data['Age'] <= 64), 'Age'] = 3
data.loc[ data['Age'] > 64, 'Age'] = 4
data['manual_tree'] = 0
data.loc[boy_or_female, 'manual_tree'] = 1
data.loc[(data['Sex'] == 1)& 
(data['Pclass'] == 3)& 
(data['Embarked'] == 0)& 
(data['Fare'] > 0), 'manual_tree'] = 0
data.loc[(data['Sex'] == 0)& 
(data['Title'] == 3), 'manual_tree'] = 1
tfidf_vec = TfidfVectorizer(max_features=15, token_pattern="\w+")
 svd = TruncatedSVD(n_components=10)
 tfidf_array = svd.fit_transform(tfidf_vec.fit_transform(data["Name"]))
for i in range(tfidf_array.shape[1]):
data.insert(len(data.columns), column = 'Name_' + str(i), value = tfidf_array [:,i])
 tfidf_vec = TfidfVectorizer(max_features=5, analyzer="char")
 svd = TruncatedSVD(n_components=3)
 tfidf_array = svd.fit_transform(tfidf_vec.fit_transform(data["Ticket"]))
for i in range(tfidf_array.shape[1]):
data.insert(len(data.columns), column = 'Ticket_' + str(i), value = tfidf_array [:,i])
 data['Ticket'] = data['Ticket'].str.extract('(\d+)', expand=False ).fillna(0 ).astype(float)
 data['Ticket'] = np.round(np.log1p(data['Ticket'])*10)
 data.drop(['Name'],1,inplace=True)
 return data.astype(float )
interp = ClassificationInterpretation.from_learner(learn )<compute_test_metric>
train = pd.read_csv(".. /input/titanic/train.csv", dtype={"Age": np.float64}, index_col='PassengerId')
 test = pd.read_csv(".. /input/titanic/test.csv", dtype={"Age": np.float64}, index_col='PassengerId' )
preds,y = learn.TTA()
acc = accuracy(preds, y)
 print('The validation accuracy is {} %.'.format(acc * 100))
pred_score = auc_score(preds,y ).item()
print('The validation AUC is {}.'.format(pred_score))<save_to_csv>
test_data_with_labels = pd.read_csv('.. /input/titanic-test-data/titanic.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 for i, name in enumerate(test_data_with_labels['name']):
if '"' in name:
test_data_with_labels['name'][i] = re.sub('"', '', name)
 for i, name in enumerate(test_data['Name']):
if '"' in name:
test_data['Name'][i] = re.sub('"', '', name)
 survived = []
for name in test_data['Name']:
survived.append(int(test_data_with_labels.loc[test_data_with_labels['name'] == name]['survived'].values[-1]))
test['Survived'] = survived
def generateSubmission(learner):
submissions = pd.read_csv('.. /input/sample_submission.csv')
 id_list = list(submissions.id)
 preds,y = learner.TTA(ds_type=DatasetType.Test)
 pred_list = list(preds[:,1])
 pred_dict = dict(( key, value.item())for(key, value)in zip(learner.data.test_ds.items,pred_list))
pred_ordered = [pred_dict[Path('.. /input/test/' + id + '.tif')] for id in id_list]
submissions = pd.DataFrame({'id':id_list,'label':pred_ordered})
 submissions.to_csv("submission_transferLearning_{}.csv".format(pred_score),index = False )<load_from_csv>
df = pd.concat(( train,test),0)
 target = train['Survived'].astype(float)
 df = PrepData(df)
 df['Ticket'] = df['Ticket'].astype(int ).astype('category')
 col_to_use = ['Embarked', 'Fare', 'Parch', 'Pclass', 'Sex', 'Age',
'NameLen', 'Has_Cabin', 'Cabin', 'FamilySize', 'isFamily',
'Title', 'TicketSurv', 'NTicket', 'Boy', 'manual_tree',
'Ticket_0', 'Ticket_1', 'Ticket_2',
'Name_0', 'Name_1', 'Name_2', 'Name_3', 'Name_4',
'Name_5', 'Name_6', 'Name_7', 'Name_8', 'Name_9']
df = pd.get_dummies(df[col_to_use])
 df[col_to_use] += 0.0
mungedtrain = df[:train.shape[0]].copy()
mungedtest = df[train.shape[0]:].copy()
mytrain = mungedtrain.values.tolist()
mytest = mungedtest.values.tolist()
df = pd.read_csv('.. /input/train_labels.csv')
 print('Shape of DataFrame',df.shape)
 df.head()<define_variables>
GPhof = []
for n in [5,7,14,21,28,42,57]:
hof, Tbox = mydeap(mungedtrain, target, seed=n, mxvl=7, ngen=121)
 GPhof.append(hof )
TRAIN_DIR = '.. /input/train/'<filter>
test = test.reset_index()
df[df['id'] != 'dd6dfed324f9fcb6f93f46f32fc800f2ec196be2']
df[df['id'] != '9369c7278ec8bcc6c880d99194de09fc2bd4efbe']
df.head()<split>
testPredictions = np.zeros(( len(GPhof),test.shape[0]))
for n in range(len(GPhof)) :
GPfunc = Tbox.compile(expr=GPhof[n])
 testPredictions[n] += Outputs(np.array([GPfunc(*x)for x in mytest]))
print("Score {}:".format(n),accuracy_score(survived,testPredictions[n]))
print(GPhof[n] )
y = df_train['label']
df_train, df_val = train_test_split(df_train, test_size = 0.1, random_state = 0, stratify = y )<define_variables>
testPrediction = np.round(np.mean(testPredictions,axis=0)).astype(int)
 print("Score :",accuracy_score(survived,testPrediction))
test['Survived'] = testPrediction
test[['PassengerId','Survived']].to_csv('gp_submit.csv', index=False )