Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_test_metric>
train_data = pd.read_csv('/kaggle/input/train.csv')
 test_data = pd.read_csv('/kaggle/input/test.csv' )
def bestThresshold(y_train,train_preds):
tmp = [0,0,0]
delta = 0
for tmp[0] in tqdm(np.arange(0.1, 0.501, 0.01)) :
tmp[1] = f1_score(y_train, np.array(train_preds)>tmp[0])
 if tmp[1] > tmp[2]:
delta = tmp[0]
tmp[2] = tmp[1]
print('best threshold is {:.4f} with F1 score: {:.4f}'.format(delta, tmp[2]))
return delta
delta = bestThresshold(y_train,train_preds )<save_to_csv>
full_data = [train_data, test_data]
for idx, dataset in enumerate(full_data):
dataset = pd.concat([pd.get_dummies(dataset['Pclass'], prefix='Pclass'), dataset], axis=1)
 dataset = dataset.drop(['Pclass', 'Pclass_3'], axis=1)
 dataset['Title'] = dataset['Name'].apply(get_title)
 dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 dataset = dataset.drop(['Name'], axis=1)
 dataset = pd.concat([pd.get_dummies(dataset['Sex'], prefix='Sex'), dataset], axis=1)
 dataset = dataset.drop(['Sex', 'Sex_male'], axis=1)
 dataset['Age'].fillna(dataset['Age'].median() , inplace = True)
 dataset['AgeBin'] = pd.cut(dataset['Age'].astype(int), 5, labels=[1, 2, 3, 4, 5])
 dataset = dataset.drop(['Age'], axis=1)
 dataset = dataset.drop(['Embarked'], axis=1)
 dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
dataset['IsAlone'] = 1
dataset['IsAlone'].loc[dataset['FamilySize'] > 1] = 0
dataset = dataset.drop(['SibSp', 'Parch'], axis=1)
 dataset = dataset.drop(['Fare'], axis=1)
 dataset = dataset.drop(['Cabin'], axis=1)
 dataset = dataset.drop(['Ticket'], axis=1)
 if idx == 0:
X_train, y_train = dataset.loc[:, dataset.columns != 'Survived'], dataset.loc[:, dataset.columns == 'Survived']
else:
submission_id = dataset['PassengerId']
X_test = dataset
submission = df_test[['qid']].copy()
submission['prediction'] =(test_preds > delta ).astype(int)
 submission.to_csv('submission.csv', index=False )<load_from_csv>
kf = KFold(n_splits=5, random_state = 0)
 clf = clf = RandomForestClassifier(n_estimators=400, max_depth=4, min_samples_split=4, random_state=0)
 for train_index, test_index in kf.split(X_train):
__kf_X_train, __kf_X_test = X_train.values[train_index], X_train.values[test_index]
__kf_y_train, __kf_y_test = y_train.values[train_index].ravel() , y_train.values[test_index].ravel()
clf.fit(__kf_X_train, __kf_y_train)
 print(accuracy_score(__kf_y_test, clf.predict(__kf_X_test)) )
<feature_engineering><EOS> 
y_pred = pd.Series(clf.predict(X_test))
submission = pd.concat([submission_id, y_pred], axis=1)
 submission = submission.rename(columns={0:'Survived'})
 submission.to_csv('submisson.csv', index=False )