Kaggle competition: Titanic - Machine Learning from Disaster
<drop_column><EOS> 
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv')
 test_data['Survived'] = np.nan
df = pd.concat([train_data, test_data], ignore_index=True, sort=False)
 df.info()
sns.barplot(x='Sex', y='Survived', data=df, palette='Set3')
 plt.show()
age_df = df[['Age', 'Pclass','Sex','Parch','SibSp']]
age_df=pd.get_dummies(age_df)
 known_age = age_df[age_df.Age.notnull() ].values
unknown_age = age_df[age_df.Age.isnull() ].values
X = known_age[:, 1:]
y = known_age[:, 0]
rfr = RandomForestRegressor(random_state=0, n_estimators=100, n_jobs=-1)
 rfr.fit(X, y)
 predictedAges = rfr.predict(unknown_age[:, 1::])
 df.loc[(df.Age.isnull()), 'Age'] = predictedAges
facet = sns.FacetGrid(df[0:890], hue="Survived",aspect=2)
 facet.map(sns.kdeplot,'Age',shade= True)
 facet.set(xlim=(0, df.loc[0:890,'Age'].max()))
facet.add_legend()
plt.show()
df['Title'] = df['Name'].map(lambda x: x.split(', ')[1].split('.')[0])
 df['Title'].replace(['Capt', 'Col', 'Major', 'Dr', 'Rev'], 'Officer', inplace=True)
 df['Title'].replace(['Don', 'Sir', 'the Countess', 'Lady', 'Dona'], 'Royalty', inplace=True)
 df['Title'].replace(['Mme', 'Ms'], 'Mrs', inplace=True)
 df['Title'].replace(['Mlle'], 'Miss', inplace=True)
 df['Title'].replace(['Jonkheer'], 'Master', inplace=True)
 sns.barplot(x='Title', y='Survived', data=df, palette='Set3')
 df['Surname'] = df['Name'].map(lambda name:name.split(',')[0].strip())
 df['FamilyGroup'] = df['Surname'].map(df['Surname'].value_counts())
 Female_Child_Group=df.loc[(df['FamilyGroup']>=2)&(( df['Age']<=16)|(df['Sex']=='female')) ]
Female_Child_Group=Female_Child_Group.groupby('Surname')['Survived'].mean()
print(Female_Child_Group.value_counts())
 Male_Adult_Group=df.loc[(df['FamilyGroup']>=2)&(df['Age']>16)&(df['Sex']=='male')]
Male_Adult_List=Male_Adult_Group.groupby('Surname')['Survived'].mean()
print(Male_Adult_List.value_counts())
 Dead_list=set(Female_Child_Group[Female_Child_Group.apply(lambda x:x==0)].index)
 Survived_list=set(Male_Adult_List[Male_Adult_List.apply(lambda x:x==1)].index)
 print('Dead_list = ', Dead_list)
 print('Survived_list = ', Survived_list)
 df.loc[(df['Survived'].isnull())&(df['Surname'].apply(lambda x:x in Dead_list)) ,\
['Sex','Age','Title']] = ['male',28.0,'Mr']
df.loc[(df['Survived'].isnull())&(df['Surname'].apply(lambda x:x in Survived_list)) ,\
['Sex','Age','Title']] = ['female',5.0,'Mrs']
fare=df.loc[(df['Embarked'] == 'S')&(df['Pclass'] == 3), 'Fare'].median()
df['Fare']=df['Fare'].fillna(fare)
 df['Family']=df['SibSp']+df['Parch']+1
df.loc[(df['Family']>=2)&(df['Family']<=4), 'Family_label'] = 2
df.loc[(df['Family']>=5)&(df['Family']<=7)|(df['Family']==1), 'Family_label'] = 1
df.loc[(df['Family']>=8), 'Family_label'] = 0
Ticket_Count = dict(df['Ticket'].value_counts())
 df['TicketGroup'] = df['Ticket'].map(Ticket_Count)
 sns.barplot(x='TicketGroup', y='Survived', data=df, palette='Set3')
 plt.show()
df.loc[(df['TicketGroup']>=2)&(df['TicketGroup']<=4), 'Ticket_label'] = 2
df.loc[(df['TicketGroup']>=5)&(df['TicketGroup']<=8)|(df['TicketGroup']==1), 'Ticket_label'] = 1
df.loc[(df['TicketGroup']>=11), 'Ticket_label'] = 0
sns.barplot(x='Ticket_label', y='Survived', data=df, palette='Set3')
 plt.show()
df['Cabin'] = df['Cabin'].fillna('Unknown')
 df['Cabin_label']=df['Cabin'].str.get(0)
 sns.barplot(x='Cabin_label', y='Survived', data=df, palette='Set3')
 plt.show()
df['Embarked'] = df['Embarked'].fillna('S')
 df = df[['Survived','Pclass','Sex','Age','Fare','Embarked','Title','Family_label','Cabin_label','Ticket_label']]
df = pd.get_dummies(df)
 train = df[df['Survived'].notnull() ]
test = df[df['Survived'].isnull() ].drop('Survived',axis=1)
 X = train.values[:,1:]
y = train.values[:,0]
test_x = test.values
select = SelectKBest(k = 20)
 clf = RandomForestClassifier(random_state = 10,
warm_start = True,
n_estimators = 26,
max_depth = 6,
max_features = 'sqrt')
 pipeline = make_pipeline(select, clf)
 pipeline.fit(X, y)
 cv_result = cross_validate(pipeline, X, y, cv= 10)
 print('mean_score = ', np.mean(cv_result['test_score']))
print('mean_std = ', np.std(cv_result['test_score']))
mask= select.get_support()
list_col = list(df.columns[1:])
 for i, j in enumerate(list_col):
print('No'+str(i+1), j,'=', mask[i])
 X_selected = select.transform(X)
 print('X.shape={}, X_selected.shape={}'.format(X.shape, X_selected.shape))
PassengerId=test_data['PassengerId']
predictions = pipeline.predict(test_x)
 submission = pd.DataFrame({"PassengerId": PassengerId, "Survived": predictions.astype(np.int32)})
 submission.to_csv("my_submission.csv", index=False )