Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<drop_column>
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1 )<data_type_conversions>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 full_data = [train, test]
category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes<categorify>
print(train.isnull().sum())
 print('_'*40)
 print(test.isnull().sum() )
encoder = OneHotEncoder()<categorify>
PassengerId = test['PassengerId']
for dataset in full_data:
dataset['Has_Cabin'] = dataset["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
dataset['Sex'] = dataset['Sex'].replace(['male', 'female'], [0, 1])
 master_ave = dataset.loc[dataset.Name.str.contains('Master'), 'Age'].mean()
mr_ave = dataset.loc[dataset.Name.str.contains('Mr'), 'Age'].mean()
miss_ave = dataset.loc[dataset.Name.str.contains('Miss'), 'Age'].mean()
mrs_ave = dataset.loc[dataset.Name.str.contains('Mrs'), 'Age'].mean()
dataset.loc[dataset.Name.str.contains('Mraster')& dataset.Age.isnull() , 'Age'] = master_ave
dataset.loc[dataset.Name.str.contains('Mr')& dataset.Age.isnull() , 'Age'] = mr_ave
dataset.loc[dataset.Name.str.contains('Miss')& dataset.Age.isnull() , 'Age'] = miss_ave
dataset.loc[dataset.Name.str.contains('Mrs')& dataset.Age.isnull() , 'Age'] = mrs_ave
dataset['Age'] = dataset['Age'].fillna(dataset['Age'].mean())
 dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4
dataset['Age'] = dataset['Age'].astype(int)
 dataset['Fare'] = dataset['Fare'].fillna(dataset['Fare'].mean())
 dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 dataset['Embarked'] = dataset['Embarked'].fillna('S')
 dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 dataset.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1, inplace=True )
X_data = encoder.fit_transform(category_df )<concatenate>
print(train.isnull().sum())
 print('_'*40)
 print(test.isnull().sum() )
X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()<feature_engineering>
trn_x = train.drop(['Survived'], axis=1, inplace=False)
 trn_y = train['Survived']
train_df.category_name = train_df.category_name.fillna('Nan/Nan/Nan')
 train_df['split_category_0'] = np.array([x for x in train_df.category_name.str.split('/', 2 ).values])[:,0]
train_df.split_category_0.value_counts()<prepare_x_and_y>
pred = np.zeros(( test.shape[0], 10))
for i in range(10):
model = RandomForestClassifier(
 max_depth = 10,
max_features =10,
min_samples_split = 15,
n_estimators = 10,
n_jobs = -1,
random_state = i)
 model.fit(trn_x, trn_y)
 pred[:,i] = model.predict_proba(test)[:,1].reshape(test.shape[0],)
 output = pred.mean(axis=1)
 output[output >= 0.5] = 1
output[output < 0.5] = 0
output = output.astype(int )
<define_variables><EOS> 
df_out = pd.DataFrame({ 'PassengerId': PassengerId,
'Survived': output })
 df_out.to_csv("submission.csv", index=False )