text_plain_output_1.png
from sklearn import metrics

from sklearn.ensemble import RandomForestClassifier

from sklearn.preprocessing import LabelEncoder

import numpy as np

import pandas as pd

import re

import pandas as pd
import numpy as np
import re
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from sklearn.model_selection import cross_val_score
train = pd.read_csv('/kaggle/input/titanic/train.csv')
test = pd.read_csv('/kaggle/input/titanic/test.csv')
example = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
train.columns = train.columns.str.lower()
test.columns = test.columns.str.lower()

general = pd.concat([train, test], axis=0)
general['fare'] = general.groupby('pclass')['fare'].transform(lambda x: x.fillna(x.median()))
general['embarked'] = general.groupby(['pclass'])['embarked'].transform(lambda x: x.fillna(x.value_counts().idxmax()))
general['age'] = general.groupby(['pclass', 'parch', 'sibsp'])['age'].transform(lambda x: x.fillna(x.median()))
general['age'] = general.groupby(['pclass'])['age'].transform(lambda x: x.fillna(x.median()))

genders = {'male': 1, 'female': 0}
general['sex_id'] = general['sex'].apply(lambda x: genders.get(x))
general['relatives'] = general['parch'] + general['sibsp']
embarkments = {'S': 1, 'C': 2, 'Q': 3}
general['embarked_id'] = general['embarked'].apply(lambda x: embarkments.get(x))
general['is_single'] = general['relatives'].apply(lambda x: 1 if x == 0 else 0)
decks = {'U': 0, 'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'T': 8}
general['deck_id'] = general['cabin'].fillna('U').apply(lambda c: decks.get(c[0]))
general['title'] = general['name'].apply(lambda x: re.search(' ([A-Za-z]+)\\.', x).group(1).lower())
general['cabin_id'] = general['cabin'].str.extract('(\\d+)').astype('float').fillna(0)
general['title_id'] = LabelEncoder().fit_transform(general['title'])

general.loc[general['deck_id'] == 0, 'deck_id'] = np.nan
general['deck_id'] = general.groupby(['pclass'])['deck_id'].transform(lambda x: x.fillna(x.median()))

train_filled = general.head(891)

test_filled = general.tail(418)
test_filled = test_filled.drop('survived', 1)
train_filled.info()

test_final = test_filled[['passengerid', 'pclass', 'age', 'sibsp', 'parch', 'fare', 'sex_id', 'relatives', 'embarked_id', 'is_single', 'deck_id', 'cabin_id', 'title_id']]
train_final = train_filled[['passengerid', 'survived', 'pclass', 'age', 'sibsp', 'parch', 'fare', 'sex_id', 'relatives', 'embarked_id', 'is_single', 'deck_id', 'cabin_id', 'title_id']]
train_final1 = train_final.drop('survived', axis=1)

alg_test = RandomForestClassifier(random_state=1, n_estimators=350, min_samples_split=6, min_samples_leaf=2)
alg_test.fit(train_final1, train_final['survived'])
predict_y = alg_test.predict(train_final1)
metrics.accuracy_score(train_final['survived'], predict_y)

alg_test = RandomForestClassifier(random_state=1, n_estimators=350, min_samples_split=6, min_samples_leaf=2)
alg_test.fit(train_final1, train_final['survived'])
predictions = alg_test.predict(test_final)
submission = pd.DataFrame({'PassengerId': test_final['passengerid'], 'Survived': predictions})
submission['Survived'] = submission['Survived'].astype(int)
print(submission.head(20))
print(example.head(20))
print(submission.info(5))
print(example.info(5))