Kaggle competition: Titanic - Machine Learning from Disaster
learn = cnn_learner(
 data,
models.resnet152,
metrics = [accuracy, kappa],
model_dir = Path('.. /kaggle/working'),
path = Path(".")
 )<train_model>
X = X_train.iloc[:,[2,4,5,6,7,9,11]].copy()
Y = X_train.iloc[:, 1]
accuracy = X_test.iloc[:, [0,1]].copy()
xText = X_test.iloc[:,[2,4,5,6,7,9,11]].copy()
finalTest = test.iloc[:,[1,3,4,5,6,8,10]].copy()
print(X.head(3))
print(Y.head(3))
print(finalTest.head(3))
learn.fit_one_cycle(3 )<save_model>
def mapAgeToCategory(x):
if x[0] < 10:
return 0
elif(x[0] >= 10 and x[0] < 20):
return 1
elif(x[0] >= 20 and x[0] < 30):
return 2
elif(x[0] >= 30 and x[0] < 40):
return 3
elif(x[0] >= 40 and x[0] < 50):
return 4
else:
return 5
def mapFareToCategory(x):
if x < 5:
return 0
elif x >= 5 and x < 10:
return 1
elif x >= 10 and x < 20:
return 1
elif x >= 20 and x < 30:
return 2
elif x >= 30 and x < 50:
return 3
elif x >=50 and x < 100:
return 4
else:
return 5
learn.save('resnet152-1' )<load_pretrained>
ohe = preprocessing.LabelEncoder()
ohe.fit(pd.unique(X.Embarked ).astype(str))
X.Embarked = ohe.transform(X.Embarked.astype(str))
xText.Embarked = ohe.transform(xText.Embarked.astype(str))
finalTest.Embarked = ohe.transform(finalTest.Embarked.astype(str))
print(ohe.classes_)
learn.load('resnet152-1');<train_model>
X['dependent'] = np.add(X.SibSp, X.Parch)
 xText['dependent'] = np.add(xText.SibSp, xText.Parch)
 finalTest['dependent'] = np.add(finalTest.SibSp, finalTest.Parch)
 X.Age.fillna(X.Age.mean() , inplace=True)
 xText.Age.fillna(xText.Age.mean() , inplace=True)
 finalTest.Age.fillna(finalTest.Age.mean() , inplace=True)
 X.Fare.fillna(X.Fare.mean() , inplace=True)
 xText.Fare.fillna(xText.Fare.mean() , inplace=True)
 finalTest.Fare.fillna(finalTest.Fare.mean() , inplace=True)
 le = preprocessing.LabelEncoder()
le.fit(pd.unique(X.Sex))
X.Sex = le.transform(X.Sex)
 xText.Sex = le.transform(xText.Sex)
 finalTest.Sex = le.transform(finalTest.Sex)
 X.Age = X[['Age','Sex']].apply(mapAgeToCategory, axis=1)
 xText.Age = xText[['Age', 'Sex']].apply(mapAgeToCategory, axis=1)
 finalTest.Age = finalTest[['Age','Sex']].apply(mapAgeToCategory, axis=1)
 le1 = preprocessing.LabelEncoder()
le1.fit(pd.unique(X.Embarked))
X.Embarked = le1.transform(X.Embarked)
 xText.Embarked = le1.transform(xText.Embarked)
 finalTest.Embarked = le1.transform(finalTest.Embarked)
 print(X.head(3))
X = X.iloc[:,[0,1,2,5]]
xText = xText.iloc[:,[0,1,2,5]]
finalTest = finalTest.iloc[:,[0,1,2,5]]
print(X.head(10))
learn.fit_one_cycle(3, slice(1e-6,1e-5))<save_model>
learn.save('resnet152-2' )<load_pretrained>
learn.load('resnet152-2');<normalization>
desicionTreeclf = DecisionTreeRegressor(criterion='mse',max_leaf_nodes=8, random_state=0,max_depth=6, min_samples_leaf=5)
 desicionTreeclf = desicionTreeclf.fit(X, Y)
<prepare_output><EOS> 
isSurvived = pd.DataFrame({'Survived' :(desicionTreeclf.predict(xText)) ,'PassengerId' :(X_test['PassengerId'])})
 finalIsSurvived = pd.DataFrame({'Survived' :(desicionTreeclf.predict(finalTest)) ,'PassengerId' :(test['PassengerId'])})
 isSurvived.Survived =isSurvived.Survived.round(0)
 isSurvived.Survived =isSurvived.Survived.astype(int)
 finalIsSurvived.Survived =finalIsSurvived.Survived.round(0)
 finalIsSurvived.Survived =finalIsSurvived.Survived.astype(int)
 print(isSurvived.head(1))
print(accuracy.head(1))
finalIsSurvived.to_csv('submit.csv', encoding='utf-8', index=False)
 print(mean_absolute_error(accuracy.iloc[:,1], isSurvived.iloc[:,1]))
print(accuracy_score(accuracy.iloc[:,1], isSurvived.iloc[:,0]))