Kaggle competition: Titanic - Machine Learning from Disaster
features['Parch'] = full['Parch']
features['SibSp'] = full['SibSp']
features['Group_size'] = group['Group_count']
features.head()<categorify>
train_df=pd.read_csv("/kaggle/input/titanic/train.csv")
 test_df=pd.read_csv("/kaggle/input/titanic/test.csv")
 gender_submission_df=pd.read_csv("/kaggle/input/titanic/gender_submission.csv")
 test_PassengerId=test_df["PassengerId"]
scalar = StandardScaler()
features_z_transformed = features.copy()
continuous = ['Fare']
features_z_transformed[continuous] = scalar.fit_transform(features_z_transformed[continuous])
 features_z_transformed.Sex = features_z_transformed.Sex.apply(lambda x: 1 if x == 'male' else 0)
 features_final = pd.get_dummies(features_z_transformed)
 encoded = list(features_final.columns)
 print("{} total features after one-hot encoding.".format(len(encoded)))
 features_final_train = features_final[:891]
features_final_test = features_final[891:]<split>
train_df[["Pclass","Survived"]].groupby(["Pclass"],as_index=False ).mean().sort_values(by="Survived",ascending=False )
X_train, X_test, y_train, y_test = train_test_split(features_final_train,
train.Survived,
test_size = 0.2,
random_state = 0 )<choose_model_class>
train_df[["Sex","Survived"]].groupby(["Sex"],as_index=False ).mean().sort_values(by="Survived",ascending=False )
clf_A = GradientBoostingClassifier(random_state = 0)
 clf_B = LogisticRegression(random_state= 0)
 clf_C = RandomForestClassifier(random_state= 0)
 samples_100 = len(y_train)
 samples_10 = int(len(y_train)/2)
 samples_1 = int(len(y_train)/10)
 results = {}
for clf in [clf_A, clf_B, clf_C]:
clf_name = clf.__class__.__name__
results[clf_name] = {}
for i, samples in enumerate([samples_1, samples_10, samples_100]):
results[clf_name][i] = \
train_predict(clf, samples, X_train, y_train, X_test, y_test )<init_hyperparams>
train_df[["SibSp","Survived"]].groupby(["SibSp"],as_index=False ).mean().sort_values(by="Survived",ascending=False )
warnings.filterwarnings('ignore')
 clf = RandomForestClassifier(random_state = 0, oob_score = True)
 parameters = {'criterion' :['gini'],
'n_estimators' : [350],
'max_depth':[5],
'min_samples_leaf': [4],
'max_leaf_nodes': [10],
'min_impurity_decrease': [0],
'max_features' : [1]
}
scorer = make_scorer(accuracy_score)
 grid_obj = GridSearchCV(clf, parameters, scoring = scorer, cv = 10)
 grid_fit = grid_obj.fit(X_train,y_train)
 best_clf = grid_fit.best_estimator_
predictions =(clf.fit(X_train, y_train)).predict(X_test)
 best_predictions = best_clf.predict(X_test)
 print("Unoptimized model
------")
 print("Accuracy score on testing data: {:.4f}".format(accuracy_score(y_test, predictions)))
 print("Oob score on testing data: {:.4f}".format(clf.oob_score_))
print("
Optimized Model
------")
 print("Final accuracy score on the testing data: {:.4f}".format(accuracy_score(y_test, best_predictions)))
 print("Final oob score on the testing data: {:.4f}".format(best_clf.oob_score_))
print("
Best Parameters
------")
 best_clf<save_to_csv>
train_df[["Parch","Survived"]].groupby(["Parch"],as_index=False ).mean().sort_values(by="Survived",ascending=False )
final_predict = best_clf.predict(features_final_test)
 prediction = pd.DataFrame(full[891:].PassengerId)
 prediction['Survived'] = final_predict.astype('int')
 prediction.to_csv('./sample_submission.csv',index = False )<set_options>
train_df.columns[train_df.isnull().any() ]
%matplotlib inline
warnings.filterwarnings('ignore')
<load_from_csv>
test_df.columns[test_df.isnull().any() ]
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train.describe(include="all" )<count_missing_values>
train_df_len=len(train_df)
 train_df=pd.concat([train_df,test_df],axis=0 ).reset_index(drop=True )
print('The null of train set')
 print(pd.isnull(train ).sum())
 print('*'*50)
 for col in train.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(train[col].isnull().sum() / train[col].shape[0]))
print(msg )<count_missing_values>
train_df.columns[train_df.isnull().any() ]
print('The null of test set')
 print(pd.isnull(test ).sum())
 print('*'*50)
 for col in test.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(train[col].isnull().sum() / train[col].shape[0]))
print(msg )<drop_column>
train_df.isnull().sum()
train = train.drop(['Cabin'], axis = 1)
 test = test.drop(['Cabin'], axis = 1 )<drop_column>
train_df[train_df["Embarked"].isnull() ]
train = train.drop(['Ticket'], axis = 1)
 test = test.drop(['Ticket'], axis = 1 )<data_type_conversions>
train_df["Embarked"]=train_df["Embarked"].fillna("C" )
train = train.fillna({"Embarked": "S"} )<feature_engineering>
train_df[train_df["Embarked"].isnull() ]
combine = [train, test]
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train['Title'], train['Sex'] )<feature_engineering>
train_df[train_df["Fare"].isnull() ]
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace(['Countess', 'Lady', 'Sir'], 'Royal')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<categorify>
train_df["Embarked"]=train_df["Embarked"].fillna("C" )
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Royal": 5, "Rare": 6}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train.head()<categorify>
train_df.Fare[1043]=train_df[train_df["Pclass"]==3].Fare.mean()
mr_age = train[train["Title"] == 1]["AgeGroup"].mode()
miss_age = train[train["Title"] == 2]["AgeGroup"].mode()
mrs_age = train[train["Title"] == 3]["AgeGroup"].mode()
master_age = train[train["Title"] == 4]["AgeGroup"].mode()
royal_age = train[train["Title"] == 5]["AgeGroup"].mode()
rare_age = train[train["Title"] == 6]["AgeGroup"].mode()
age_title_mapping = {1: "Young Adult", 2: "Student", 3: "Adult", 4: "Baby", 5: "Adult", 6: "Adult"}
for x in range(len(train["AgeGroup"])) :
if train["AgeGroup"][x] == "Unknown":
train["AgeGroup"][x] = age_title_mapping[train["Title"][x]]
for x in range(len(test["AgeGroup"])) :
if test["AgeGroup"][x] == "Unknown":
test["AgeGroup"][x] = age_title_mapping[test["Title"][x]]<categorify>
train_df[train_df["Age"].isnull() ]
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
train['AgeGroup'] = train['AgeGroup'].map(age_mapping)
 test['AgeGroup'] = test['AgeGroup'].map(age_mapping)
 train.head()
train = train.drop(['Age'], axis = 1)
 test = test.drop(['Age'], axis = 1 )<drop_column>
train_df.Sex.value_counts()
train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1 )<categorify>
index_nan_age=list(train_df["Age"][train_df["Age"].isnull() ].index )
sex_mapping = {"male": 0, "female": 1}
train['Sex'] = train['Sex'].map(sex_mapping)
 test['Sex'] = test['Sex'].map(sex_mapping)
 train.head()<categorify>
for i in index_nan_age:
age_predict=train_df["Age"][(( train_df["SibSp"]==train_df.iloc[i]["SibSp"])&(train_df["Parch"]==train_df.iloc[i]["Parch"])&(train_df["Pclass"]==train_df.iloc[i]["Pclass"])) ].median()
age_med=train_df["Age"].median()
if not np.isnan(age_predict):
train_df["Age"].iloc[i]=age_predict
else:
train_df["Age"].iloc[i]=age_med
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train['Embarked'] = train['Embarked'].map(embarked_mapping)
 test['Embarked'] = test['Embarked'].map(embarked_mapping)
 train.head()<split>
train_df[train_df["Age"].isnull() ]
predictors = train.drop(['Survived', 'PassengerId'], axis=1)
 target = train["Survived"]
x_train, x_val, y_train, y_val = train_test_split(predictors, target, test_size = 0.22, random_state = 0 )<predict_on_test>
s="Braund, Mr.Owen Harris"
s.split("." )
gaussian = GaussianNB()
gaussian.fit(x_train, y_train)
 y_pred = gaussian.predict(x_val)
 acc_gaussian = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gaussian )<compute_train_metric>
s.split(".")[0].split("," )
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_val)
 acc_logreg = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_logreg )<compute_train_metric>
s.split(".")[0].split(",")[1]
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_val)
 acc_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_svc )<compute_train_metric>
s.split(".")[0].split(",")[-1]
linear_svc = LinearSVC()
linear_svc.fit(x_train, y_train)
 y_pred = linear_svc.predict(x_val)
 acc_linear_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_linear_svc )<compute_train_metric>
name=train_df["Name"]
train_df["Title"]=[ i.split(".")[0].split(",")[-1].strip() for i in name]
perceptron = Perceptron()
perceptron.fit(x_train, y_train)
 y_pred = perceptron.predict(x_val)
 acc_perceptron = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_perceptron )<train_model>
train_df["Title"].value_counts()
decisiontree = DecisionTreeClassifier()
decisiontree.fit(x_train, y_train)
 y_pred = decisiontree.predict(x_val)
 acc_decisiontree = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_decisiontree )<train_model>
train_df["Title"]=train_df["Title"].replace(["Lady","the Countess","Capt","Col","Don","Dr","Major","Rev","Sir","Jonkheer","Dona"],"other" )
randomforest = RandomForestClassifier()
randomforest.fit(x_train, y_train)
 y_pred = randomforest.predict(x_val)
 acc_randomforest = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_randomforest )<compute_train_metric>
train_df["Title"].value_counts()
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
 y_pred = knn.predict(x_val)
 acc_knn = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_knn )<predict_on_test>
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"]]
sgd = SGDClassifier()
sgd.fit(x_train, y_train)
 y_pred = sgd.predict(x_val)
 acc_sgd = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_sgd )<predict_on_test>
train_df.drop(labels=["Name"],axis=1,inplace=True )
gbk = GradientBoostingClassifier()
gbk.fit(x_train, y_train)
 y_pred = gbk.predict(x_val)
 acc_gbk = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gbk )<create_dataframe>
train_df=pd.get_dummies(train_df,columns=["Title"] )
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron', 'Linear SVC',
'Decision Tree', 'Stochastic Gradient Descent', 'Gradient Boosting Classifier','XGBClassifier'],
'Score': [acc_svc, acc_knn, acc_logreg,
acc_randomforest, acc_gaussian, acc_perceptron,acc_linear_svc, acc_decisiontree,
acc_sgd, acc_gbk,acc_xgb]})
 models.sort_values(by='Score', ascending=False )<save_to_csv>
train_df["Fsize"]=train_df["SibSp"] + train_df["Parch"] + 1
ids = test['PassengerId']
predictions = gbk.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('./submission.csv', index=False )<set_options>
train_df["family-size"]=[1 if i<5 else 0 for i in train_df["Fsize"]]
%matplotlib inline
warnings.filterwarnings('ignore')
<load_from_csv>
train_df=pd.get_dummies(train_df,columns=["family-size"] )
df = pd.read_csv('/kaggle/input/eval-lab-2-f464/train.csv')
 test = pd.read_csv('/kaggle/input/eval-lab-2-f464/test.csv' )<prepare_x_and_y>
train_df=pd.get_dummies(train_df,columns=["Embarked"] )
num_features = ['id','chem_1', 'chem_2', 'chem_4','chem_5','chem_6','attribute']
num_features1 = ['chem_1', 'chem_2', 'chem_4','chem_5','chem_6','attribute']
X_train = df[num_features]
y_train = df["class"]
X = X_train
y = y_train<import_modules>
a=" A./5.2152"
<predict_on_test>
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
clf1 = XGBClassifier().fit(X[num_features],y)
 y_pred1 = clf1.predict(test[num_features])
 clf2 = XGBClassifier().fit(X[num_features1],y)
 y_pred2 = clf2.predict(test[num_features1] )<save_to_csv>
train_df=pd.get_dummies(train_df,columns=["Ticket"],prefix="T" )
answer = pd.DataFrame(data={'id':test['id'],'class':y_pred1})
 answer.to_csv('final1.csv',index=False)
 answer = pd.DataFrame(data={'id':test['id'],'class':y_pred2})
 answer.to_csv('final2.csv',index=False )<import_modules>
train_df["Pclass"]=train_df["Pclass"].astype("category" )
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
train_df=pd.get_dummies(train_df,columns=["Pclass"] )
dataset = pd.read_csv('.. /input/eval-lab-2-f464/train.csv' )<load_from_csv>
train_df.Sex=train_df.Sex.astype("category" )
test = pd.read_csv('.. /input/eval-lab-2-f464/test.csv' )<count_missing_values>
train_df.Sex.value_counts()
dataset.isnull().sum()<drop_column>
train_df=pd.get_dummies(train_df,columns=["Sex"] )
dataset.drop(['id'], axis = 1, inplace = True )<prepare_x_and_y>
train_df.drop(labels=["PassengerId", "Cabin"],axis=1, inplace=True )
X = dataset[dataset.drop(['class'], axis = 1 ).columns].values
y = dataset['class'].values<split>
train_df.drop(labels=["SibSp", "Parch"],axis=1, inplace=True )
train_x, test_x, train_y, test_y = train_test_split(X, y, test_size = 20/120 )<train_model>
train_df.Sex_0.value_counts()
clf = DecisionTreeClassifier(random_state = 42)
 clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)) )<train_model>
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
clf = RandomForestClassifier(random_state = 42)
 clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)) )<train_model>
test=train_df[train_df_len:]
clf = GradientBoostingClassifier(random_state = 42)
 clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)) )<train_model>
test.drop(labels=["Survived"], axis=1, inplace=True )
clf = AdaBoostClassifier(random_state = 42)
 clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)) )<compute_train_metric>
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))
clf = ExtraTreesClassifier(random_state = 42, max_depth = 10, n_estimators = 1000, min_samples_split = 2)
 clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)))
 scoring = ['accuracy']
scores = cross_validate(clf, X, y, cv=6, scoring = scoring)
 print(np.mean(scores['test_accuracy']))<compute_train_metric>
logreg=LogisticRegression(solver ='lbfgs',multi_class ='multinomial',max_iter = 500)
 logreg.fit(X_train,y_train )
clf = BernoulliNB()
clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)) )<train_model>
acc_log_train=round(logreg.score(X_train,y_train)*100,2 )
clf = LogisticRegression(random_state = 42, penalty='l1')
 clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)) )<train_on_grid>
acc_log_test=round(logreg.score(X_test,y_test)*100,2 )
<compute_train_metric>
print("Training accuracy {}".format(acc_log_train))
print("Test accuracy {}".format(acc_log_test))
clf = SGDClassifier()
clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)))
 print(accuracy_score(train_y, clf.predict(train_x)) )<train_model>
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)
clf = KNeighborsClassifier(n_neighbors = 5, leaf_size = 10, weights = 'distance', algorithm = 'auto')
 clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)))
 print(accuracy_score(train_y, clf.predict(train_x)) )<train_on_grid>
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]
clf = XGBClassifier()
clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)) )<train_on_grid>
range(len(classifier))
<compute_train_metric>
cv_result=[]
best_estimators=[]
for i in range(len(classifier)) :
clf=GridSearchCV(classifier[i],param_grid=classifier_param[i],
cv=StratifiedKFold(n_splits=10),scoring="accuracy",
n_jobs=-1,
verbose=1 
)
 clf.fit(X_train,y_train)
 cv_result.append(clf.best_score_)
 best_estimators.append(clf.best_estimator_)
 print(cv_result[i])
clf = ExtraTreesClassifier(random_state = 42)
 clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)))
 print(accuracy_score(train_y, clf.predict(train_x)) )<compute_train_metric>
votingC=VotingClassifier(estimators=[("dt",best_estimators[0]), 
("rfc",best_estimators[2]), 
("lr",best_estimators[3])],
voting="soft",n_jobs=-1)
 votingC=votingC.fit(X_train,y_train)
 print(accuracy_score(votingC.predict(X_test),y_test))
clf = ExtraTreesClassifier(random_state = 0)
 clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)))
 print(accuracy_score(train_y, clf.predict(train_x)) )<compute_train_metric>
test_survived=pd.Series(votingC.predict(test),name="Survived")
 results=pd.concat([test_PassengerId,test_survived],axis=1)
 results.to_csv("titanic.csv",index=False )
clf = GaussianNB()
clf.fit(train_x, train_y)
 print(accuracy_score(test_y, clf.predict(test_x)) )<prepare_x_and_y>
test_survived=pd.Series(votingC.predict(test),name="Survived" ).astype(int)
 results=pd.concat([test_PassengerId,test_survived],axis=1)
 results.to_csv("titanic.csv",index=False )