Kaggle competition: Titanic - Machine Learning from Disaster
name=train_df["Name"]
train_df["Title"]=[ i.split(".")[0].split(",")[-1].strip() for i in name]<count_values>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 test_df.head()
train_df["Title"].value_counts()<feature_engineering>
train_df.isnull().sum() / len(train_df)*100
train_df["Title"]=train_df["Title"].replace(["Lady","the Countess","Capt","Col","Don","Dr","Major","Rev","Sir","Jonkheer","Dona"],"other" )<count_values>
test_df.isnull().sum() / len(test_df)*100
train_df["Title"].value_counts()<feature_engineering>
train_df['Sex'].value_counts()
train_df["Title"]=[0 if i == "Master" else 1 if i== "Miss" or i=="Ms" or i=="Mlle" or i=="Mrs" else 2 if i=="Mr" else 3 for i in train_df["Title"]]<drop_column>
train_df.groupby('Sex',as_index=False ).Survived.mean()
train_df.drop(labels=["Name"],axis=1,inplace=True )<categorify>
train_df[['Pclass','Survived']].groupby(['Pclass'],as_index=False ).mean().sort_values(by='Survived',ascending = False )
train_df=pd.get_dummies(train_df,columns=["Title"] )<feature_engineering>
train_df[["Embarked", "Survived"]].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
train_df["Fsize"]=train_df["SibSp"] + train_df["Parch"] + 1<feature_engineering>
train_df[['Parch','Survived']].groupby(['Parch'],as_index=False ).mean().sort_values(by='Survived',ascending=False )
train_df["family-size"]=[1 if i<5 else 0 for i in train_df["Fsize"]]<categorify>
train_df[["SibSp", "Survived"]].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
train_df=pd.get_dummies(train_df,columns=["family-size"] )<categorify>
train_df.groupby('Sex',as_index='False')['Age'].median()
train_df=pd.get_dummies(train_df,columns=["Embarked"] )<define_variables>
drop_list=['Cabin','Ticket','PassengerId']
train_df = train_df.drop(drop_list,axis=1)
 test_passenger_df = pd.DataFrame(test_df.PassengerId)
 test_df = test_df.drop(drop_list,axis=1)
 test_passenger_df.head()
a=" A./5.2152"<feature_engineering>
train_df.Embarked.fillna('S',inplace=True )
tickets=[]
for i in list(train_df.Ticket):
if not i.isdigit() :
tickets.append(i.replace(".","" ).replace("/","" ).strip().split() [0])
 else:
tickets.append("x")
 train_df["Ticket"]=tickets<categorify>
train_df.Age.fillna(28, inplace=True)
 test_df.Age.fillna(28, inplace=True )
train_df=pd.get_dummies(train_df,columns=["Ticket"],prefix="T" )<data_type_conversions>
test_df.Fare.fillna(test_df.Fare.median() , inplace=True )
train_df["Pclass"]=train_df["Pclass"].astype("category" )<categorify>
Combined_data = [train_df, test_df]
Combined_data
train_df=pd.get_dummies(train_df,columns=["Pclass"] )<data_type_conversions>
for dataset in Combined_data:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False )
train_df.Sex=train_df.Sex.astype("category" )<count_values>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Special": 5}
for dataset in Combined_data:
dataset['Title'] = dataset.Title.map(title_mapping)
 dataset['Title'] = dataset.Title.fillna(0 )
train_df.Sex.value_counts()<categorify>
for dataset in Combined_data:
dataset["Family"] = dataset['SibSp'] + dataset['Parch']
dataset["IsAlone"] = np.where(dataset["Family"] > 0, 0,1)
 dataset.drop('Family',axis=1,inplace=True)
 train_df.head()
train_df=pd.get_dummies(train_df,columns=["Sex"] )<drop_column>
for dataset in Combined_data:
dataset.drop(['SibSp','Parch','Name'],axis=1,inplace=True )
train_df.drop(labels=["PassengerId", "Cabin"],axis=1, inplace=True )<drop_column>
for dataset in Combined_data:
dataset["IsMinor"] = np.where(dataset["Age"] < 15, 1, 0 )
train_df.drop(labels=["SibSp", "Parch"],axis=1, inplace=True )<count_values>
train_df['Old_Female'] =(train_df['Age']>50)&(train_df['Sex']=='female')
 train_df['Old_Female'] = train_df['Old_Female'].astype(int)
 test_df['Old_Female'] =(test_df['Age']>50)&(test_df['Sex']=='female')
 test_df['Old_Female'] = test_df['Old_Female'].astype(int )
train_df.Sex_0.value_counts()<import_modules>
train_df2 = pd.get_dummies(train_df,columns=['Pclass','Sex','Embarked'],drop_first=True)
 test_df2 = pd.get_dummies(test_df,columns=['Pclass','Sex','Embarked'],drop_first=True)
 train_df2.head()
from sklearn.model_selection import train_test_split, StratifiedKFold, GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score 
<split>
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.metrics import accuracy_score
test=train_df[train_df_len:]<drop_column>
X = train_df2.drop("Survived",axis=1)
 y = train_df2["Survived"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=42 )
test.drop(labels=["Survived"], axis=1, inplace=True )<split>
logreg = LogisticRegression()
logreg.fit(X_train,y_train)
 y_pred = logreg.predict(X_test)
 acc_logreg = round(accuracy_score(y_pred, y_test)* 100, 2)
 acc_logreg
train=train_df[:train_df_len]
X_train= train.drop(labels=["Survived"],axis=1)
 y_train=train.Survived
print("X_train",len(X_train))
print("y_train",len(y_train))
X_train, X_test, y_train, y_test=train_test_split(X_train, y_train, test_size=0.10, random_state=42)
 print("X_train",len(X_train))
print("X_test",len(X_test))
print("y_train",len(y_train))
print("y_test",len(y_test))
print("test",len(test))<train_model>
cv_scores = cross_val_score(logreg,X,y,cv=5)
 np.mean(cv_scores)*100
logreg=LogisticRegression(solver ='lbfgs',multi_class ='multinomial',max_iter = 500)
 logreg.fit(X_train,y_train )<compute_test_metric>
decisiontree = DecisionTreeClassifier()
dep = np.arange(1,10)
 param_grid = {'max_depth' : dep}
clf_cv = GridSearchCV(decisiontree, param_grid=param_grid, cv=5)
 clf_cv.fit(X, y)
 clf_cv.best_params_,clf_cv.best_score_*100
print('Best value of max_depth:',clf_cv.best_params_)
 print('Best score:',clf_cv.best_score_*100 )
acc_log_train=round(logreg.score(X_train,y_train)*100,2 )<compute_test_metric>
random_forest = RandomForestClassifier()
ne = np.arange(1,20)
 param_grid = {'n_estimators':ne}
rf_cv = GridSearchCV(random_forest,param_grid,cv=5)
 rf_cv.fit(X, y)
 print('Best value of n_estimators:',rf_cv.best_params_)
 print('Best score:',rf_cv.best_score_*100 )
acc_log_test=round(logreg.score(X_test,y_test)*100,2 )<train_model>
gbk = GradientBoostingClassifier()
ne = np.arange(1,20)
 dep = np.arange(1,10)
 param_grid = {'n_estimators' : ne,'max_depth' : dep}
gbk_cv = GridSearchCV(gbk,param_grid=param_grid,cv=5)
 gbk_cv.fit(X, y)
 print('Best value of parameters:',gbk_cv.best_params_)
 print('Best score:',gbk_cv.best_score_*100 )
print("Training accuracy {}".format(acc_log_train))
print("Test accuracy {}".format(acc_log_test))<train_model>
y_final = gbk_cv.predict(test_df2)
 y_final
nb=GaussianNB()
nb.fit(X_train,y_train)
 acc_nb_train=round(nb.score(X_train,y_train)*100,2)
 acc_nb_test=round(nb.score(X_test,y_test)*100,2)
 print(acc_nb_train)
 print(acc_nb_test)
<choose_model_class>
df = pd.DataFrame({"PassengerId": test_passenger_df["PassengerId"],"Survived": y_final})
 df
random_state=42
classifier=[DecisionTreeClassifier(random_state=random_state),
SVC(random_state=random_state),
RandomForestClassifier(random_state=random_state),
LogisticRegression(random_state=random_state),
KNeighborsClassifier() ]
dt_param_grid={"min_samples_split": range(10,500,20),
"max_depth": range(1,20,2)}
svc_param_grid={"kernel":["rbf"],
"gamma":[0.001,0.01,0.1,1],
"C":[1,10,50,100,200,300,1000],
"probability" : [True]}
rf_param_grid={"max_features":[1,3,5,10,25,35,50],
"min_samples_split":[2,3,10],
"min_samples_leaf":[1,3,10],
"bootstrap":[False],
"n_estimators":[100,300],
"criterion":["gini","entropy"]}
logreg_param_grid={"C":np.logspace(-3,13,17),
"penalty":["l1","l2"],"max_iter": [500]}
knn_param_grid={"n_neighbors": np.linspace(1,50,50,dtype=int ).tolist() ,
"weights":["uniform","distance"],
"metric":["euclidean","manhattan"]}
classifier_param=[dt_param_grid,svc_param_grid,rf_param_grid,
logreg_param_grid,knn_param_grid] 
<define_variables>
df.to_csv('Survived.csv', index=False )