<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Convert Sex feature to numeric
Step1:  Fill missing Age values
The histogram is skewed towards 20-30s, so cannot use average age.
Step2:  Fill missing Embarked
Step3:  Feature Engineering
Since we know that Parch is the number of parents or children onboard, and SibSp is the number of siblings or spouses, we could collect those together as a FamilySize.
<ASSISTANT_TASK:>
Python Code:
df['Gender'] = df['Sex'].map( {'female': 0, 'male': 1} ).astype(int)
df.head()
Explanation: Convert Sex feature to numeric
End of explanation
df['Age'].dropna().hist(bins=16, range=(0,80), alpha = .5)
P.show()
median_ages = np.zeros((2,3))
median_ages
for i in range(0, 2):
    for j in range(0, 3):
        median_ages[i,j] = df[(df['Gender'] == i) & \
                              (df['Pclass'] == j+1)]['Age'].dropna().median()
 
median_ages
df['AgeFill'] = df['Age']
df.head()
df[df['Age'].isnull()][['Gender','Pclass','Age','AgeFill']].head(10)
for i in range(0, 2):
    for j in range(0, 3):
        df.loc[ (df.Age.isnull()) & (df.Gender == i) & (df.Pclass == j+1),\
                'AgeFill'] = median_ages[i,j]
df[ df['Age'].isnull() ][['Gender','Pclass','Age','AgeFill']].head(10)
df['AgeIsNull'] = pd.isnull(df.Age).astype(int)
df.describe()
Explanation: Fill missing Age values
The histogram is skewed towards 20-30s, so cannot use average age.
End of explanation
df[df['Embarked'].isnull()][['Fare','Pclass','AgeFill','Embarked']].head(10)
df['Port'] = df['Embarked']
df.loc[df.Embarked.isnull(), 'Port'] = ['S', 'S']
df['Port'] = df['Port'].map( {'S': 1, 'Q': 2, 'C': 3} ).astype(int)
df.head()
Explanation: Fill missing Embarked
End of explanation
df['FamilySize'] = df['SibSp'] + df['Parch']
df['Age*Class'] = df.AgeFill * df.Pclass
df.describe()
df['Age*Class'].hist(bins=16, alpha=.5)
P.show()
df.dtypes[df.dtypes.map(lambda x: x=='object')]
df = df.drop(['Name', 'Sex', 'Age', 'Ticket', 'Cabin', 'Embarked'], axis=1)
df.info()
df.describe()
train_data = df.values
train_data
df_test = pd.read_csv('data/titanic-kaggle/test.csv', header=0)
df_test.describe()
df_test['Gender'] = df_test['Sex'].map( {'female': 0, 'male': 1} ).astype(int)
# Create the random forest object which will include all the parameters
# for the fit
forest = RandomForestClassifier(n_estimators = 100)
# Fit the training data to the Survived labels and create the decision trees
forest = forest.fit(train_data[0::,1::],train_data[0::,0])
# Take the same decision trees and run it on the test data
output = forest.predict(test_data)
Explanation: Feature Engineering
Since we know that Parch is the number of parents or children onboard, and SibSp is the number of siblings or spouses, we could collect those together as a FamilySize.
End of explanation
<END_TASK>