Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<sort_values>
warnings.filterwarnings('ignore' )
print(train_data["Pclass"].unique())
 train_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<string_transform>
train_original = pd.read_csv('.. /input/train.csv')
 test_original = pd.read_csv('.. /input/test.csv')
 train_original.sample(10)
 total = [train_original,test_original]
train_data.Name[1].split()<feature_engineering>
for dataset in total:
dataset['Salutation'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False )
train_data = train_data.assign(fname = train_data.Name.str.split("," ).str[0])
 train_data["title"] = pd.Series([i.split(",")[1].split(".")[0].strip() for i in train_data.Name], index=train_data.index )<feature_engineering>
for dataset in total:
dataset['Salutation'] = dataset['Salutation'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Salutation'] = dataset['Salutation'].replace('Mlle', 'Miss')
 dataset['Salutation'] = dataset['Salutation'].replace('Ms', 'Miss')
 dataset['Salutation'] = dataset['Salutation'].replace('Mme', 'Mrs')
test_data = test_data.assign(fname = test_data.Name.str.split("," ).str[0])
 test_data["title"] = pd.Series([i.split(",")[1].split(".")[0].strip() for i in test_data.Name], index=test_data.index)
 train_data.drop("Name", axis=1, inplace=True)
 test_data.drop("Name", axis=1, inplace=True )<count_unique_values>
for dataset in total:
dataset['Salutation'] = pd.factorize(dataset['Salutation'])[0]
print(test_data.fname.nunique())
 print(test_data.title.nunique() )<categorify>
train=train_original.drop(['PassengerId','Name','Ticket','Cabin'], axis=1)
 test=test_original.drop(['PassengerId','Name','Ticket','Cabin'], axis=1)
 total = [train,test]
train.shape, test.shape
train_data["title"] = train_data['title'].replace(other_titles, 'Other')
 train_data["title"] = train_data["title"].map({"Mr":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Master":2, "Other":3})
 test_data["title"] = test_data['title'].replace(other_titles, 'Other')
 test_data["title"] = test_data["title"].map({"Mr":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Master":2, "Other":3} )<count_missing_values>
train.isnull().sum()
print(train_data.title)
 print(test_data.title.isna().sum() )<categorify>
def fill_missing_age(dataset):
for i in range(1,8):
median_age=dataset[dataset["Salutation"]==i]["Age"].median()
dataset["Age"]=dataset["Age"].fillna(median_age)
 return dataset
train = fill_missing_age(train )
oh = OneHotEncoder(handle_unknown="ignore", sparse = False)
 train_data = train_data.join(pd.DataFrame(oh.fit_transform(train_data[["fname", "title"]]), index = train_data.index))
test_data = test_data.join(pd.DataFrame(oh.transform(test_data[["fname", "title"]]), index = test_data.index))
train_data.drop("fname", axis = 1, inplace = True)
 test_data.drop("fname", axis = 1, inplace = True )<sort_values>
train[train['Embarked'].isnull() ]
print(train_data["Sex"].unique())
 train_data[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
train["Embarked"] = train["Embarked"].fillna('C' )
interactions = train_data.assign(sex_class = train_data['Sex'] + "_" + train_data['Pclass'].astype("str"))
interactions[['sex_class', 'Survived']].groupby(['sex_class'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<data_type_conversions>
test.isnull().sum()
train_data = train_data.assign(sex_class = train_data['Sex'] + "_" + train_data['Pclass'].astype("str"))
test_data = test_data.assign(sex_class = test_data['Sex'] + "_" + test_data['Pclass'].astype("str"))<categorify>
test.isnull().sum()
train_data = train_data.join(pd.get_dummies(train_data['Pclass'], prefix="Pclass"))
test_data = test_data.join(pd.get_dummies(test_data['Pclass'], prefix="Pclass"))<categorify>
test = fill_missing_age(test )
train_data["Sex"] = train_data["Sex"].map({"female":0, "male":1})
 test_data["Sex"] = test_data["Sex"].map({"female":0, "male":1} )<categorify>
def fill_missing_fare(dataset):
median_fare=dataset[(dataset["Pclass"]==3)&(dataset["Embarked"]=="S")]["Fare"].median()
dataset["Fare"]=dataset["Fare"].fillna(median_fare)
 return dataset
test = fill_missing_fare(test )
train_data["sex_class"] = train_data["sex_class"].map({"female_1":0, "female_2":1, "female_3":2, "male_1":4, "male_2":5, "male_3":6})
 test_data["sex_class"] = test_data["sex_class"].map({"female_1":0, "female_2":1, "female_3":2, "male_1":4, "male_2":5, "male_3":6} )<filter>
train.isnull().any()
def find_similar_passengers(id, dataset):
subset = dataset[(dataset.title == dataset.title[id])& 
(dataset.Pclass == dataset.Pclass[id])]
if subset["Age"].mean() == "NaN":
subset = dataset[(dataset["sex_class"] == dataset.iloc[id]["sex_class"])]
if subset["Age"].mean() == "NaN":
subset = dataset[(dataset["sex"] == dataset.iloc[id]["sex"])]
age = subset["Age"].mean()
return age<feature_engineering>
test.isnull().any()
no_ages = train_data[train_data["Age"].isna() ].index
for pid in no_ages:
train_data.Age[pid] = find_similar_passengers(pid, train_data)
 no_ages_test = test_data[test_data["Age"].isna() ].index
for pid2 in no_ages_test:
test_data.Age[pid2] = find_similar_passengers(pid2, test_data )<data_type_conversions>
pd.qcut(train["Age"], 6 ).value_counts()
train_data["age_group"] = pd.cut(train_data["Age"], bins=[0,5,65,100], labels=[0,1,2] ).astype("int64")
 test_data["age_group"] = pd.cut(test_data["Age"], bins=[0,5,65,100], labels=[0,1,2] ).astype("int64" )<sort_values>
for dataset in total:
dataset.loc[dataset["Age"] <= 19, "Age"] = 0
dataset.loc[(dataset["Age"] > 19)&(dataset["Age"] <= 25), "Age"] = 1
dataset.loc[(dataset["Age"] > 25)&(dataset["Age"] <= 32), "Age"] = 2
dataset.loc[(dataset["Age"] > 32)&(dataset["Age"] <= 35), "Age"] = 3
dataset.loc[(dataset["Age"] > 35)&(dataset["Age"] <= 40.5), "Age"] = 4
dataset.loc[dataset["Age"] > 40.59, "Age"] = 5
train_data[['SibSp', 'Survived']].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
pd.qcut(train["Fare"], 8 ).value_counts()
train_data[['Parch', 'Survived']].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
for dataset in total:
dataset.loc[dataset["Fare"] <= 7.75, "Fare"] = 0
dataset.loc[(dataset["Fare"] > 7.75)&(dataset["Fare"] <= 7.91), "Fare"] = 1
dataset.loc[(dataset["Fare"] > 7.91)&(dataset["Fare"] <= 9.841), "Fare"] = 2
dataset.loc[(dataset["Fare"] > 9.841)&(dataset["Fare"] <= 14.454), "Fare"] = 3
dataset.loc[(dataset["Fare"] > 14.454)&(dataset["Fare"] <= 24.479), "Fare"] = 4
dataset.loc[(dataset["Fare"] >24.479)&(dataset["Fare"] <= 31), "Fare"] = 5
dataset.loc[(dataset["Fare"] > 31)&(dataset["Fare"] <= 69.487), "Fare"] = 6
dataset.loc[dataset["Fare"] > 69.487, "Fare"] = 7
train_data["fsize"] = train_data["SibSp"] + train_data["Parch"] + 1
test_data["fsize"] = test_data["SibSp"] + test_data["Parch"] + 1<sort_values>
for dataset in total:
dataset['Sex'] = pd.factorize(dataset['Sex'])[0]
dataset['Embarked']= pd.factorize(dataset['Embarked'])[0]
train.head()
train_data[['fsize', 'Survived']].groupby(['fsize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<count_unique_values>
x = train.drop("Survived", axis=1)
 y = train["Survived"]
print(train_data.Ticket.nunique())
 print(train_data.Ticket.tail() )<feature_engineering>
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=.25,random_state=1 )
train_data["ticket_prefix"] = pd.Series([len(i.split())> 1 for i in train_data.Ticket], index=train_data.index )<sort_values>
MLA = [
ensemble.AdaBoostClassifier() ,
ensemble.BaggingClassifier() ,
ensemble.ExtraTreesClassifier() ,
ensemble.GradientBoostingClassifier() ,
ensemble.RandomForestClassifier() ,
gaussian_process.GaussianProcessClassifier() ,
linear_model.LogisticRegressionCV() ,
linear_model.PassiveAggressiveClassifier() ,
linear_model.RidgeClassifierCV() ,
linear_model.SGDClassifier() ,
linear_model.Perceptron() ,
naive_bayes.BernoulliNB() ,
naive_bayes.GaussianNB() ,
neighbors.KNeighborsClassifier() ,
svm.SVC(probability=True),
svm.NuSVC(probability=True),
svm.LinearSVC() ,
tree.DecisionTreeClassifier() ,
]
train_data[['ticket_prefix', 'Survived']].groupby(['ticket_prefix'], as_index=False ).mean().sort_values(by='Survived', ascending=False)
<drop_column>
MLA_columns = []
MLA_compare = pd.DataFrame(columns = MLA_columns)
 row_index = 0
for alg in MLA:
predicted = alg.fit(x_train, y_train ).predict(x_test)
 fp, tp, th = roc_curve(y_test, predicted)
 MLA_name = alg.__class__.__name__
MLA_compare.loc[row_index,'MLA Name'] = MLA_name
MLA_compare.loc[row_index, 'MLA Train Accuracy'] = round(alg.score(x_train, y_train), 4)
 MLA_compare.loc[row_index, 'MLA Test Accuracy'] = round(alg.score(x_test, y_test), 4)
 MLA_compare.loc[row_index, 'MLA Precission'] = precision_score(y_test, predicted)
 MLA_compare.loc[row_index, 'MLA Recall'] = recall_score(y_test, predicted)
 MLA_compare.loc[row_index, 'MLA AUC'] = auc(fp, tp)
 row_index+=1
MLA_compare.sort_values(by = ['MLA Test Accuracy'], ascending = False, inplace = True)
 MLA_compare
train_data.drop("ticket_prefix", axis=1, inplace=True)
 train_data.drop("Ticket", axis=1, inplace=True)
 test_data.drop("Ticket", axis=1, inplace=True )<drop_column>
tunealg = ensemble.AdaBoostClassifier()
tunealg.fit(x_train, y_train)
 print('BEFORE tuning Parameters: ', tunealg.get_params())
 print("BEFORE tuning Training w/bin set score: {:.2f}".format(tunealg.score(x_train, y_train)))
 print("BEFORE tuning Test w/bin set score: {:.2f}".format(tunealg.score(x_test, y_test)))
 print('-'*10)
train_data.drop("Cabin", axis=1, inplace=True)
 test_data.drop("Cabin", axis=1, inplace=True )<sort_values>
param_grid = {'n_estimators': [10,15,25,35,45,50,55,60,65],
'learning_rate': [0.1,0.2,0.3,0.4,0.5,1.0],
'algorithm': ['SAMME','SAMME.R'],
'random_state': [1,2,3,4,5,50, None],
}
tune_model = model_selection.GridSearchCV(ensemble.AdaBoostClassifier() , param_grid=param_grid, scoring = 'roc_auc')
 tune_model.fit(x_train, y_train)
 print('AFTER tuning Parameters: ', tune_model.best_params_)
 print("AFTER tuning Training w/bin set score: {:.2f}".format(tune_model.score(x_train, y_train)))
 print("AFTER tuning Test w/bin set score: {:.2f}".format(tune_model.score(x_test, y_test)))
 print('-'*10 )
train_data["Embarked"] = train_data["Embarked"].fillna("S")
 train_data[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<count_missing_values>
y_pred = tune_model.fit(x, y ).predict(test )
<categorify><EOS> 
submission = pd.DataFrame({
"PassengerId": test_original["PassengerId"],
"Survived": y_pred
})
 submission.to_csv('titanic.csv', index=False)