Kaggle competition: Titanic - Machine Learning from Disaster
range(len(classifier))<train_on_grid>
warnings.filterwarnings('ignore')
 %matplotlib inline
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])
<predict_on_test>
train_data = pd.read_csv(".. /input/train.csv")
 train_data.columns
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))<save_to_csv>
test = pd.read_csv(".. /input/test.csv")
 IDtest = test["PassengerId"]
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 )<save_to_csv>
train_data.drop(['PassengerId','Ticket'], axis=1, inplace = True)
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 )<load_from_csv>
train_data.isnull().sum()
train_df = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_df.head()<load_from_csv>
train_data["Embarked"] = train_data["Embarked"].fillna("C")
 test["Embarked"] = test["Embarked"].fillna("C")
 train_data['Fare'].fillna(train_data['Fare'].median() , inplace = True)
 test['Fare'].fillna(test['Fare'].median() , inplace = True)
 train_data.Cabin.fillna("N", inplace=True)
 test.Cabin.fillna("N", inplace=True )
test_df = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_df.head()<sort_values>
train_data.isnull().sum()
train_df[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
train_title = [i.split(",")[1].split(".")[0].strip() for i in train_data["Name"]]
train_data["Title"] = pd.Series(train_title)
 train_data["Title"].head()
train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
test_title = [i.split(",")[1].split(".")[0].strip() for i in test["Name"]]
test["Title"] = pd.Series(test_title)
 test["Title"].head()
train_df[['SibSp', 'Survived']].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
train_data["Title"] = train_data["Title"].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 train_data["Title"] = train_data["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Mr":2, "Rare":3})
 train_data["Title"] = train_data["Title"].astype(int )
train_df[['Parch', 'Survived']].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
test["Title"] = test["Title"].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 test["Title"] = test["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Mr":2, "Rare":3})
 test["Title"] = test["Title"].astype(int )
train_df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False)
<define_variables>
train_data["Family_size"] = train_data["SibSp"] + train_data["Parch"] + 1
test["Family_size"] = test["SibSp"] + test["Parch"] + 1
all_df = [train_df, test_df]<data_type_conversions>
train_data['survived_dead'] = train_data['Survived'].apply(lambda x : 'Survived' if x == 1 else 'Dead' )
for df in all_df:
df['Sex'] = df['Sex'].map({'male': 0, 'female': 1} ).astype(int)
 train_df.head()<data_type_conversions>
lbl = LabelEncoder()
lbl.fit(list(train_data['Embarked'].values))
train_data['Embarked'] = lbl.transform(list(train_data['Embarked'].values))
lbl.fit(list(test['Embarked'].values))
test['Embarked'] = lbl.transform(list(test['Embarked'].values))
common_Pclass = 'S'
train_df['Embarked'].fillna(common_Pclass, inplace=True)
 train_df['Embarked'] = train_df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 test_df['Embarked'] = test_df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train_df.head()<count_missing_values>
def encode(x): return 1 if x == 'female' else 0
train_data['enc_sex'] = train_data.Sex.apply(encode)
 test['enc_sex'] = test.Sex.apply(encode )
train_df['Cabin'].isna().sum()<data_type_conversions>
train_data["has_cabin"] = [0 if i == 'N'else 1 for i in train_data.Cabin]
test["has_cabin"] = [0 if i == 'N'else 1 for i in test.Cabin]
train_df['Cabin'].fillna('X', inplace=True)
 test_df['Cabin'].fillna('X', inplace=True )<count_values>
def detect_outliers(train_data,n,features):

 outlier_indices = []
for col in features:
Q1 = np.percentile(train_data[col], 25)
 Q3 = np.percentile(train_data[col],75)
 IQR = Q3 - Q1
outlier_step = 1.5 * IQR
outlier_list_col = train_data[(train_data[col] < Q1 - outlier_step)|(train_data[col] > Q3 + outlier_step)].index
outlier_indices.extend(outlier_list_col)
 outlier_indices = Counter(outlier_indices)
 multiple_outliers = list(k for k, v in outlier_indices.items() if v > n)
 return multiple_outliers
Outliers_to_drop = detect_outliers(train_data,2,["Age","SibSp","Parch","Fare"])
train_df['Cabin'].value_counts()<data_type_conversions>
train_data.loc[Outliers_to_drop]
for df in all_df:
df['Cabin'] = df['Cabin'].astype(str ).str[0]
test_df['Cabin']<groupby>
train_data = train_data.drop(Outliers_to_drop, axis = 0 ).reset_index(drop=True )
train_df.groupby(['Survived', 'Cabin'])['PassengerId'].sum()<feature_engineering>
y_train = train_data["Survived"]
X_train = data.drop(labels = ["Survived"],axis = 1 )
for df in all_df:
df.loc[(df['Cabin'] == 'A'), 'Cabin'] = 0
df.loc[(df['Cabin'] == 'B'), 'Cabin'] = 1
df.loc[(df['Cabin'] == 'C'), 'Cabin'] = 1
df.loc[(df['Cabin'] == 'D'), 'Cabin'] = 1
df.loc[(df['Cabin'] == 'E'), 'Cabin'] = 1
df.loc[(df['Cabin'] == 'F'), 'Cabin'] = 0
df.loc[(df['Cabin'] == 'G'), 'Cabin'] = 0
df.loc[(df['Cabin'] == 'T'), 'Cabin'] = 2
df.loc[(df['Cabin'] == 'X'), 'Cabin'] = 3
train_df.head()<define_variables>
test = test.select_dtypes(include=[np.number] ).interpolate().dropna()
test = test[X_train.columns]
guess_df = np.zeros(( 3,2))
guess_df<categorify>
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
 test = sc.transform(test )
for df in all_df:
for i in range(0,3):
for j in range(0,2):
guess = df[(df['Pclass']==i+1)&(df['Sex']==j)]['Age'].dropna()
guess_df[i,j] = guess.median()
for i in range(0,3):
for j in range(0,2):
df.loc[(df['Age'].isna())&(df['Pclass']==i+1)&(df['Sex']==j), 'Age'] = guess_df[i,j]
df['Age'] = df['Age'].dropna().astype(int)
 train_df.head()<feature_engineering>
kfold = StratifiedKFold(n_splits=10 )
for df in all_df:
df.loc[(df['Age'] <= 16), 'Age'] = 0
df.loc[(df['Age'] > 16)&(df['Age'] <= 32), 'Age'] = 1
df.loc[(df['Age'] > 32)&(df['Age'] <= 48), 'Age'] = 2
df.loc[(df['Age'] > 48)&(df['Age'] <= 64), 'Age'] = 3
df.loc[(df['Age'] > 64), 'Age'] = 4
train_df.head()<sort_values>
ExtC = ExtraTreesClassifier()
ex_param_grid = {"max_depth": [n for n in range(9, 14)],
"max_features": [1, 3, 10],
"min_samples_split": [n for n in range(4, 11)],
"min_samples_leaf": [n for n in range(2, 5)],
"bootstrap": [False],
"n_estimators" :[n for n in range(10, 60, 10)],
"criterion": ["gini"]}
gsExtC = GridSearchCV(ExtC,param_grid = ex_param_grid, cv=kfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 gsExtC.fit(X_train,y_train)
 ExtC_best = gsExtC.best_estimator_
gsExtC.best_score_
train_df.groupby('Age', as_index=False)['Survived'].mean().sort_values(by='Age', ascending=True )<count_missing_values>
RFC = RandomForestClassifier()
rf_param_grid = {"max_depth": [n for n in range(9, 14)],
"max_features": [1, 3, 10],
"min_samples_split": [n for n in range(4, 11)],
"min_samples_leaf": [n for n in range(2, 5)],
"bootstrap": [False],
"n_estimators" :[n for n in range(10, 60, 10)],
"criterion": ["gini"]}
gsRFC = GridSearchCV(RFC,param_grid = rf_param_grid, cv=kfold, scoring="accuracy", n_jobs= 1, verbose = 1)
 gsRFC.fit(X_train,y_train)
 RFC_best = gsRFC.best_estimator_
gsRFC.best_score_
train_df.isna().sum()<drop_column>
DTC = DecisionTreeClassifier()
adaDTC = AdaBoostClassifier(DTC, random_state=7)
 ada_param_grid = {"base_estimator__criterion" : ["gini", "entropy"],
"base_estimator__splitter" : ["best", "random"],
"algorithm" : ["SAMME","SAMME.R"],
"n_estimators" :[30],
"learning_rate": [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3,1.5]}
gsadaDTC = GridSearchCV(adaDTC,param_grid = ada_param_grid, cv=kfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 gsadaDTC.fit(X_train,y_train)
 ada_best = gsadaDTC.best_estimator_
gsadaDTC.best_score_
train_df.drop(['AgeBand'], axis=1, inplace=True )<count_missing_values>
SVMC = SVC(probability=True)
 svc_param_grid = {'kernel': ['rbf'],
'gamma': [ 0.001, 0.01, 0.1, 1],
'C': [1, 10, 50, 100,200,300, 1000]}
gsSVMC = GridSearchCV(SVMC,param_grid = svc_param_grid, cv=kfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 gsSVMC.fit(X_train,y_train)
 SVMC_best = gsSVMC.best_estimator_
gsSVMC.best_score_
train_df.isna().sum()<count_missing_values>
GBC = GradientBoostingClassifier()
gb_param_grid = {'loss' : ["deviance"],
'n_estimators' : [n for n in range(10, 60, 10)],
'learning_rate': [0.1, 0.05, 0.01],
'max_depth': [n for n in range(9, 14)],
'min_samples_leaf': [n for n in range(2, 5)],
'max_features': [0.3, 0.1]
}
gsGBC = GridSearchCV(GBC,param_grid = gb_param_grid, cv=kfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 gsGBC.fit(X_train,y_train)
 GBC_best = gsGBC.best_estimator_
gsGBC.best_score_
test_df.isna().sum()<correct_missing_values>
votingC = VotingClassifier(estimators=[('rfc', RFC_best),('extc', ExtC_best),('svm',SVMC_best), 
('gbc',GBC_best)], voting='soft', n_jobs=4)
 votingC = votingC.fit(X_train, y_train)
<categorify><EOS> 
test_Survived = pd.Series(votingC.predict(test), name="Survived")
 Submission = pd.concat([IDtest,test_Survived],axis=1)
 Submission.to_csv("submission.csv",index=False )