# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df_train=pd.read_csv('../input/titanic/train.csv')
df_test=pd.read_csv('../input/titanic/test.csv')
PassengerId=df_test['PassengerId']
df_train.drop(['PassengerId','Name','Ticket'],axis=1,inplace=True)
df_test.drop(['PassengerId','Name','Ticket'],axis=1,inplace=True)
df_train.isnull().sum()/len(df_train)*100
# Removing Cabin
df_train.drop(['Cabin'],axis=1,inplace=True)
df_test.drop(['Cabin'],axis=1,inplace=True)
# Removing rows where embarked is null

df_train.dropna(subset=['Embarked'],inplace=True)

df_train['Embarked'].isnull().sum()
age_train_series=df_train.groupby(['Pclass','Sex'])['Age'].transform('median')
age_test_series=df_test.groupby(['Pclass','Sex'])['Age'].transform('median')
df_train['Age']=df_train['Age'].fillna(age_train_series)
df_test['Age']=df_test['Age'].fillna(age_test_series)
df_test.isnull().sum()
Fare_test_series=df_test.groupby(['Pclass'])['Fare'].transform('median')
df_test['Fare']=df_test['Fare'].fillna(Fare_test_series)
df_train=pd.get_dummies(df_train,drop_first=True)

df_test=pd.get_dummies(df_test,drop_first=True)
from sklearn.model_selection import train_test_split
X = df_train.drop('Survived',axis=1)

# Putting response variable to y
y = df_train['Survived']

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=42)
X_train.shape, X_test.shape
"""
### RANDOM FOREST WITHOUT HYPERPARAMETER TUNING
"""
 from sklearn.ensemble import RandomForestClassifier
rf=RandomForestClassifier(random_state=42,n_estimators=100,max_depth=4,min_samples_leaf=15,max_features=3)
rf.fit(X_train,y_train)
from sklearn.metrics import accuracy_score
accuracy_score(y_test,rf.predict(X_test))
predictions=rf.predict(df_test)
titanic_4=pd.DataFrame({'PassengerId':PassengerId,'Survived':predictions})
titanic_4.to_csv('My_4th_submission',index=False)

pd.read_csv('My_4th_submission')