Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
%matplotlib inline
plt.style.use('fivethirtyeight')
 warnings.filterwarnings('ignore')
 train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 merged = train.append(test)
learn.fit_one_cycle(4, 2e-4 )<save_model>
merged['PTitle'] = merged['PTitle'].replace(['Lady', 'the Countess','Mlle', 'Ms', 'Mme', 'Dona','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer'], 'Rare')
 merged[['Sex','PTitle','Pclass','Survived']].groupby(['Sex','PTitle','Pclass'] ).mean()
learn.save('resnet152-3' )<load_pretrained>
merged.loc[merged['FamilySize'] == 1, 'Fsize'] = 'Alone'
merged.loc[(merged['FamilySize'] > 1)&(merged['FamilySize'] < 5), 'Fsize'] = 'Small'
merged.loc[merged['FamilySize'] >4, 'Fsize'] = 'Large'
fem_analysis = merged[merged['Sex'] == 'female']
fem_analysis[['PTitle','Pclass','Survived','Fsize']].groupby(['Pclass','PTitle','Fsize'] ).mean()
learn.load('resnet152-3');<train_model>
fem2_analysis = fem_analysis[(fem_analysis['Pclass'] == 3)& 
(fem_analysis['Fsize'] != 'Large')]
fem2_analysis[['PTitle','Embarked','Survived']].groupby(['PTitle','Embarked'] ).mean()
learn.fit_one_cycle(6, 2e-5 )<save_model>
fem2_analysis[['PTitle','Embarked','Survived']].groupby(['PTitle','Embarked'] ).count()
learn.save('resnet152-4' )<load_pretrained>
men_analysis = merged[merged['Sex'] == 'male']
men_analysis[['PTitle','Pclass','Survived','Fsize']].groupby(['PTitle','Pclass','Fsize'] ).mean()
learn.load('resnet152-4');<load_pretrained>
men2_analysis = men_analysis[(men_analysis['Pclass'] == 1)& 
(men_analysis['PTitle'] != 'Master')& 
(men_analysis['Fsize'] != 'Large')]
men2_analysis['HasCabin'] = men2_analysis['Cabin'].notnull()
men2_analysis[['PTitle','HasCabin','Survived']].groupby(['PTitle','HasCabin'] ).mean()
learn.load('resnet152-4');<predict_on_test>
merged['Group'] = merged['Sex']
merged.loc[(merged['Sex'] == 'female')& 
(merged['Fsize'] == 'Large')& 
(merged['Pclass'] == 3), 'Group'] = 'Females, large family, class 3'
merged.loc[(merged['PTitle'] == 'Miss')& 
(merged['Fsize'] != 'Large')& 
(merged['Pclass'] == 3)& 
(merged['Embarked'] == 'S'), 'Group'] = 'Miss, class 3, embarked Southampton'
merged.loc[(merged['PTitle'] == 'Master')& 
(( merged['Pclass'] < 3)| 
(merged['Fsize'] == 'Small')) , 'Group'] = 'Masters in Pclass 1&2 or small families'
merged.loc[(merged['PTitle'] == 'Rare')& 
(merged['Sex'] == 'male')& 
(merged['Pclass'] == 1)& 
(merged['Cabin'].notnull())& 
(merged['Fsize'] != 'Large'), 'Group'] = 'Rare title, Cabin Pclass 1, not large'
merged[['Group','Survived']].groupby(['Group'], sort=False ).mean().plot.bar()
plt.show()
merged[['Group','Survived']].groupby(['Group'], sort=False ).mean()
preds, _ = learn.get_preds(ds_type=DatasetType.Test )<load_from_csv>
merged['Predict'] = 1
merged.loc[merged['Group'] == 'male', 'Predict'] = 0
merged.loc[merged['Group'] == 'Females, large family, class 3', 'Predict'] = 0
merged.loc[merged['Group'] == 'Miss, class 3, embarked Southampton', 'Predict'] = 0
merged.loc[merged['Group'] == 'Masters in Pclass 1&2 or small families', 'Predict'] = 1
merged.loc[merged['Group'] == 'Rare title, Cabin Pclass 1, not large', 'Predict'] = 1
train_set = merged[merged['Survived'].notnull() ]
train_set['Score'] = train_set['Survived'] == train_set['Predict']
print("Accuracy training set:")
 print(train_set['Score'].mean() )
<data_type_conversions><EOS> 
my_solution = pd.DataFrame({'PassengerId': test_analysed['PassengerId'],
'Survived':test_analysed['Predict']})
 my_solution.to_csv('submission.csv', index = False )