Kaggle competition: Titanic - Machine Learning from Disaster
V4drop = ['V89', 'V256', 'V3', 'V1', 'V2', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'V10', 'V11', 'V46', 'V42', 'V43', 'V47',
'V41', 'V39', 'V36', 'V35', 'V51', 'V50', 'V49', 'V48', 'V88', 'V93', 'V85', 'V84', 'V83', 'V81', 'V90', 'V91',
'V92', 'V94', 'V80', 'V79', 'V75', 'V75']
for i in V4drop:
drop_features.append(i )<define_variables>
train= pd.read_csv("/kaggle/input/titanic/train.csv")
 train.head()
V5drop = ['V68', 'V27', 'V28', 'V53', 'V74', 'V73', 'V72', 'V66', 'V54', 'V67', 'V64', 'V63', 'V62', 'V61', 'V71', 'V69',
'V55', 'V60', 'V59', 'V58', 'V57', 'V65', 'V56', 'V70', 'V22', 'V23', 'V24', 'V34', 'V33', 'V32', 'V31', 'V30',
'V29', 'V26', 'V25', 'V15', 'V21', 'V14', 'V16', 'V17', 'V18', 'V19', 'V12', 'V20', 'V13', 'V301', 'V300', 'V296',
'V289', 'V288']
for i in V5drop:
drop_features.append(i )<drop_column>
test=pd.read_csv("/kaggle/input/titanic/test.csv")
 test.head()
V6drop = ['V114', 'V110', 'V105', 'V104', 'V103', 'V102', 'V101', 'V100', 'V95', 'V99', 'V98', 'V107', 'V111', 'V112', 'V106',
'V113', 'V108', 'V134', 'V133', 'V135', 'V132', 'V131', 'V130', 'V129', 'V126', 'V125', 'V124', 'V123', 'V122',
'V121', 'V120', 'V119', 'V118', 'V117', 'V116', 'V115', 'V109', 'V294']
for i in V6drop:
drop_features.append(i )<define_variables>
train.groupby("Sex")["Survived"].mean()
V7drop = ['V305', 'V304', 'V303', 'V302', 'V299', 'V298', 'V297', 'V295', 'V293', 'V292', 'V291', 'V290', 'V287', 'V286',
'V285', 'V289', 'V279', 'V309', 'V316', 'V318', 'V319']
for i in V7drop:
drop_features.append(i )<filter>
train.pivot_table("Survived",index='Sex',columns='Pclass' )
D_done = ['D7', 'D13', 'D14', 'D12', 'D6', 'D9', 'D8']
D_not_done = missing['features'].apply(lambda x: x if x[0]=='D' else 0)
 D_not_done = pd.DataFrame(D_not_done)
 D_not_done = D_not_done[D_not_done['features']!=0]
D_not_done = D_not_done[~D_not_done['features'].isin(D_done)]
D_not_done = D_not_done[~D_not_done['features'].isin(['DeviceInfo', 'DeviceType'])]
D = list(D_not_done['features'])
 D<create_dataframe>
train.isnull().sum()
C_not_done = missing['features'].apply(lambda x: x if x[0]=='C' else 0)
 C_not_done = pd.DataFrame(C_not_done)
 C_not_done = C_not_done[C_not_done['features']!=0]
C_not_done = list(C_not_done['features'])
 C_not_done<concatenate>
train.isnull().sum()
drop_features.append('C3' )<create_dataframe>
train.drop("Cabin", axis=1,inplace=True)
 test.drop("Cabin", axis=1,inplace=True )
id_done = ['id_24', 'id_25', 'id_08', 'id_07', 'id_21', 'id_26', 'id_27', 'id_23', 'id_22', 'id_18', 'id_04', 'id_03',
'id_33', 'id_09', 'id_30', 'id_32', 'id_32', 'id_34', 'id_14']
id_not_done = missing['features'].apply(lambda x: x if x[0]=='i' else 0)
 id_not_done = pd.DataFrame(id_not_done)
 id_not_done = id_not_done[id_not_done['features']!=0]
id_not_done = id_not_done[~id_not_done['features'].isin(id_done)]
id_not_done<concatenate>
test["Age"].fillna(round(test['Age'].median()),inplace=True )
check_features.append('id_10' )<concatenate>
most_commn='S'
data = [train, test]
for data in train:
train["Embarked"]= train["Embarked"].fillna(most_commn)
check_features.append('id_16' )<concatenate>
data = [train, test]
for d in data:
d['Fare'] = d['Fare'].fillna(d["Fare"].median())
 d['Fare'] = d['Fare'].astype(int )
check_features.append('id_28' )<concatenate>
gender = {"male": 0, "female": 1}
data = [train,test]
for d in data:
d['Sex'] = d['Sex'].map(gender )
check_features.append('id_29' )<concatenate>
boarding = {"S": 0, "C": 1, "Q": 2}
data = [train, test]
for d in data:
d['Embarked'] = d['Embarked'].map(boarding )
check_features.append('id_38' )<concatenate>
train.drop(['PassengerId','Name','SibSp','Parch','Ticket'],axis=1,inplace=True )
check_features.append('id_37' )<concatenate>
test.drop(['Name','SibSp','Parch','Ticket'],axis=1,inplace=True )
check_features.append('id_36' )<concatenate>
x= train.drop("Survived", axis=1)
 y= train["Survived"]
target = test.drop("PassengerId", axis=1 )
check_features.append('id_35' )<create_dataframe>
k_fold= KFold(n_splits=10,shuffle=True,random_state=0 )
c_not_done = missing['features'].apply(lambda x: x if x[0]=='c' else 0)
 c_not_done = pd.DataFrame(c_not_done)
 c_not_done = c_not_done[c_not_done['features']!=0]
list(c_not_done['features'] )<concatenate>
knn=KNeighborsClassifier(n_neighbors=13)
 score= cross_val_score(knn,x,y,cv=k_fold,n_jobs=1,scoring='accuracy')
 print(score )
check_features.append('card4' )<concatenate>
round(np.mean(score)*100,2 )
check_features.append('card6' )<create_dataframe>
dt=DecisionTreeClassifier()
score= cross_val_score(dt,x,y,cv=k_fold,n_jobs=1,scoring='accuracy')
 print(score )
M_not_done = missing['features'].apply(lambda x: x if x[0]=='M' else 0)
 M_not_done = pd.DataFrame(M_not_done)
 M_not_done = M_not_done[M_not_done['features']!=0]
list(M_not_done['features'] )<concatenate>
round(np.mean(score)*100,2 )
drop_features.append('M1' )<concatenate>
lr=LogisticRegression()
score= cross_val_score(lr,x,y,cv=k_fold,n_jobs=1,scoring='accuracy')
 print(score )
check_features.append('M2' )<concatenate>
round(np.mean(score)*100,2 )
check_features.append('M3' )<concatenate>
rf = RandomForestClassifier(n_estimators=50,max_depth=6,random_state=0)
 score = cross_val_score(rf,x,y, cv=k_fold, n_jobs=1, scoring='accuracy')
 print(score )
check_features.append('M5' )<concatenate>
round(np.mean(score)*100,2 )
check_features.append('M6' )<create_dataframe>
rf=RandomForestClassifier(n_estimators=30,max_depth=6, random_state=0)
 rf.fit(x,y)
 target_pred= rf.predict(target )
<feature_engineering><EOS> 
output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': target_pred})
 output.to_csv('Titanic_RF_Final.csv', index=False)
 print("submission was Successfull!" )