Kaggle competition: Titanic - Machine Learning from Disaster
learn_clas.lr_find(start_lr=slice(10e-7, 10e-5), end_lr=slice(0.1, 10))<find_best_params>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head(n=10)
best_clf_lr = learn_clas.recorder.min_grad_lr
best_clf_lr<train_model>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.describe()
learn_clas.fit_one_cycle(1, best_clf_lr )<set_options>
women_survived_values = train_data.loc[train_data.Sex == 'female']["Survived"]
women_survival_rate = sum(women_survived_values)/len(women_survived_values)
 print("Women survival rate:", women_survival_rate)
 print("
")
 men_survived_values = train_data.loc[train_data.Sex == 'male']["Survived"]
men_survival_rate = sum(men_survived_values)/len(men_survived_values)
 print("Men survival rate:", men_survival_rate )
learn_clas.freeze_to(-2 )<train_model>
print(pd.pivot_table(train_data, index='Survived', columns='Pclass', values='PassengerId', aggfunc='count'))
print("
")
 print(pd.pivot_table(train_data, index='Survived', columns='Embarked', values='PassengerId', aggfunc='count'))
learn_clas.fit_one_cycle(1, best_clf_lr )<load_pretrained>
all_data = pd.concat(objs=[train_data, test_data], axis=0 ).reset_index(drop=True)
 def extract_title(Name):
if '.' in Name:
return Name.split(',')[1].split('.')[0].strip()
else:
return 'None'
all_data['Title'] = all_data['Name'].map(lambda x: extract_title(x))
train_data['Title'] = train_data['Name'].map(lambda x: extract_title(x))
test_data['Title'] = test_data['Name'].map(lambda x: extract_title(x))
title_list = all_data['Title'].unique()
print("Title list:", title_list)
 print("
")
 def identify_title_category(cols):
Parch = cols[0]
Title = cols[1]
if Title in ['Miss', 'Master'] and Parch == 0:
return 'Young without parent-child'
elif Title in ['Miss', 'Master'] and Parch > 0:
return 'Young with parent-child'
elif Title not in ['Miss', 'Master'] and Parch == 0:
return 'Old without parent-child'
else:
return 'Old with parent-child'
all_data['Title'] = all_data[["Parch","Title"]].apply(identify_title_category, axis=1)
 train_data['Title'] = train_data[["Parch","Title"]].apply(identify_title_category, axis=1)
 test_data['Title'] = test_data[["Parch","Title"]].apply(identify_title_category, axis=1)
 def replace_null_age(cols):
Age = cols[0]
Pclass = cols[1]
Sex = cols[2]
Title = cols[3]
if pd.isnull(Age):
return float(round(all_data[(( all_data["Pclass"] == Pclass)&(all_data["Sex"] == Sex)&(all_data["Title"] == Title)) ]["Age"].mean()))
else:
return Age
train_data["Age"] = train_data[["Age", "Pclass", "Sex", "Title"]].apply(replace_null_age,axis=1)
 test_data["Age"] = test_data[["Age", "Pclass", "Sex", "Title"]].apply(replace_null_age,axis=1)
 grouped_average_age = all_data.groupby(['Pclass', 'Sex', 'Title'])["Age"].mean()
print("grouped_average_age:", "
", grouped_average_age)
 train_data.head(n=10)
<train_model><EOS> 
test_data = test_data.fillna(test_data.mean())
 train_data.fillna(train_data.mean())
 survived_data = train_data["Survived"]
relevant_features = ["Pclass", "Sex", "SibSp", "Parch", "Embarked", "Age", "Fare"]
X_train = pd.get_dummies(train_data[relevant_features])
 X_test = pd.get_dummies(test_data[relevant_features])
 k_fold = KFold(n_splits=10, shuffle=True, random_state=1)
 model = RandomForestClassifier(n_estimators=100)
 scoring = "accuracy"
score = cross_val_score(model, X_train, survived_data, cv=k_fold, n_jobs=1, scoring=scoring)
 print("RF score:", score)
 print(round(np.mean(score)*100, 2))
model = SVC()
scoring = "accuracy"
score = cross_val_score(model, X_train, survived_data, cv=k_fold, n_jobs=1, scoring=scoring)
 print("SVC score:", score)
 print(round(np.mean(score)*100, 2))
model = KNeighborsClassifier(n_neighbors = 10)
 scoring = 'accuracy'
score = cross_val_score(model, X_train, survived_data, cv=k_fold, n_jobs=1, scoring=scoring)
 print("K Neighbor score", score)
 print(round(np.mean(score)*100, 2))
model = GaussianNB()
scoring = 'accuracy'
score = cross_val_score(model, X_train, survived_data, cv=k_fold, n_jobs=1, scoring=scoring)
 print("Gaussian score", score)
 print(round(np.mean(score)*100, 2))
model = DecisionTreeClassifier()
scoring = 'accuracy'
score = cross_val_score(model, X_train, survived_data, cv=k_fold, n_jobs=1, scoring=scoring)
 print("Decision Tree score", score)
 print(round(np.mean(score)*100, 2))
model = RandomForestClassifier(n_estimators=200, max_depth=6, random_state=100)
 scoring = "accuracy"
score = cross_val_score(model, X_train, survived_data, cv=k_fold, n_jobs=1, scoring=scoring)
 print("New RF score:", score)
 print(round(np.mean(score)*100, 2))
model.fit(X_train, survived_data)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission_7_1.csv', index=False)
 print("Submission done!")