# 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)
import matplotlib.pyplot as plt
import seaborn as sns

# 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
"""
# 1. Data Cleaning
"""
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")

print(train_data.head())
length = train_data.shape[0]
print(train_data.isnull().sum())
print("Percentage of data missing in Age: {}".format((train_data["Age"].isnull().sum()/length)*100))
print("Percentage of data missing in Cabin: {}".format((train_data["Cabin"].isnull().sum()/length)*100))
# Since more than 77% of data is missing Cabin is droped

train_data.drop("Cabin",axis = 1,inplace = True)
print(train_data.isnull().sum())
# Since only 2 values of Embarked is missing it is filled with the maximum occuring value.

print(train_data["Embarked"].value_counts())
train_data["Embarked"].fillna("S",inplace = True)
print(train_data.isnull().sum())
# Seeing the Distribution of Age

sns.histplot(x = train_data["Age"],bins = 10)
"""
*We can impute age based on the Title of the Name to get better results*
"""
train_data[train_data["Age"].isna()]
split_name = train_data["Name"].str.split(", ",expand = True)
train_data["Title"] = split_name[1].str.split(".",expand = True)[0]
train_data["Title"].value_counts()
train_data.groupby(["Title"])["Age"].describe()
train_data[train_data["Age"].isna()]["Title"].value_counts()
"""
The above Title has missing Age values.
So for each title the age is imputed with its mean
"""
train_data.loc[(train_data["Age"].isna()) & (train_data["Title"] == "Mr"),"Age"]= train_data[train_data["Title"] == "Mr"]["Age"].mean(skipna = True)
train_data.loc[(train_data["Age"].isna()) & (train_data["Title"] == "Miss"),"Age"]= train_data[train_data["Title"] == "Miss"]["Age"].mean(skipna = True)
train_data.loc[(train_data["Age"].isna()) & (train_data["Title"] == "Mrs"),"Age"]= train_data[train_data["Title"] == "Mrs"]["Age"].mean(skipna = True)
train_data.loc[(train_data["Age"].isna()) & (train_data["Title"] == "Master"),"Age"]= train_data[train_data["Title"] == "Master"]["Age"].mean(skipna = True)
train_data.loc[(train_data["Age"].isna()) & (train_data["Title"] == "Dr"),"Age"]= train_data[train_data["Title"] == "Dr"]["Age"].mean(skipna = True)
print(train_data.isnull().sum())
# Dropping Columns that are not needed for model building

train_data.drop(["PassengerId","Name","Ticket","Title"],axis = 1,inplace = True)
train_data.info()
print(train_data["Sex"].unique())
print(train_data["Embarked"].unique())
# Encoding Sex and Embarked values

train_data["Sex"].replace({"male":0,"female":1},inplace = True)
train_data["Embarked"].replace({"S":0,"C":1,"Q":2},inplace = True)
train_data.info()
"""
# 2. Data Analysis
"""
sns.heatmap(train_data.corr(),annot = True)
sns.countplot(x = "Survived",hue = "Sex",data = train_data)
sns.countplot(x = "Survived",hue = "Pclass",data = train_data)
sns.countplot(x = "Sex",hue = "Pclass",data = train_data)
sns.countplot(x = "SibSp",hue = "Survived",data = train_data)
sns.countplot(x = "Parch",hue = "Survived",data = train_data)
sns.scatterplot(x = "Age",y = "Fare",hue = "Survived",data = train_data)
"""
We are dropping SibSp and Parch since it has less impact on the Target column.
"""
X = train_data.drop(["Survived","SibSp","Parch"],axis = 1)
y = train_data["Survived"]
# We shall use RandomForestClassifier

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn.metrics import accuracy_score,confusion_matrix
from sklearn.ensemble import RandomForestClassifier
# Scaling Fare Column.

scaler = MinMaxScaler()
X[["Fare"]] = scaler.fit_transform(X[["Fare"]])
X.head()
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.3)
model = RandomForestClassifier()
model.fit(X_train,y_train)
score = cross_val_score(model,X_train,y_train,cv = 10)
print("Cross Validation Score : {}".format(np.mean(score) * 100))
y_pred = model.predict(X_test)
print("Model Accuracy: {}".format(accuracy_score(y_pred,y_test)))
print(confusion_matrix(y_test,y_pred))
"""
# 3. Cleaning Test data and Predicting Value
"""
print(test_data.isnull().sum())
print(test_data.shape)
test_data.drop(["Cabin"],axis = 1,inplace = True)
split_name = test_data["Name"].str.split(", ",expand = True)
test_data["Title"] = split_name[1].str.split(".",expand = True)[0]
test_data["Title"].value_counts()
test_data.groupby(["Title"])["Age"].describe()
test_data[test_data["Age"].isna()]["Title"].value_counts()
test_data.loc[(test_data["Age"].isna()) & (test_data["Title"] == "Mr"),"Age"]= test_data[test_data["Title"] == "Mr"]["Age"].mean(skipna = True)
test_data.loc[(test_data["Age"].isna()) & (test_data["Title"] == "Miss"),"Age"]= test_data[test_data["Title"] == "Miss"]["Age"].mean(skipna = True)
test_data.loc[(test_data["Age"].isna()) & (test_data["Title"] == "Mrs"),"Age"]= test_data[test_data["Title"] == "Mrs"]["Age"].mean(skipna = True)
test_data.loc[(test_data["Age"].isna()) & (test_data["Title"] == "Master"),"Age"]= test_data[test_data["Title"] == "Master"]["Age"].mean(skipna = True)
test_data.loc[(test_data["Age"].isna()) & (test_data["Title"] == "Ms"),"Age"]= test_data[test_data["Title"] == "Miss"]["Age"].mean(skipna = True)
test_data[test_data["Fare"].isna()]
test_data["Fare"].fillna(test_data[test_data["Pclass"] == 3]["Fare"].mean(),inplace = True)
print(test_data.isnull().sum())
gender_submission = pd.DataFrame(index = test_data["PassengerId"])
t_data = test_data.drop(["PassengerId","Name","SibSp","Parch","Ticket","Title"],axis = 1)
t_data.head()
t_data["Sex"].replace({"male":0,"female":1},inplace = True)
t_data["Embarked"].replace({"S":0,"C":1,"Q":2},inplace = True)
t_data[["Fare"]] = scaler.fit_transform(t_data[["Fare"]])
t_data.head()
gender_submission["Survived"] = model.predict(t_data)
gender_submission.to_csv("submission.csv")