"""
#                                         TITANIC PROBLEM KAGGLE

### This is a Classifier Supervised Machine Learning Project in which we have to successfully identify whether a person survived the Titanic Ship tragedy or not, given features about them such as their name, age, sex, ticket fare, cabin, etc.
"""
"""
## This Notebook will focus on Data Cleaning and Preprocessing.
"""
#Importing Basic ML Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#Now we load our training and test datasets in order to visualise and know our features better.
#train = pd.read_csv(r"C:\Users\hp\Desktop\titanic problem\New folder\train.csv")
train = pd.read_csv("../input/titanic/train.csv")
test = pd.read_csv("../input/titanic/test.csv")

#We visualise our data.
train
test

"""
### In our data we have 11 different features and we have to classify whether a person survived or not based on these.

### Now first of all we have to do Data Cleaning and Data Preprocessing. It considers the following:
### i) Dealing with missing values or NAN values.
### ii) converting textual data into a format that can be deployed in our model.
### iii) Handling Categorical data.

### Before we do that we need to combine the Test and Training Datasets so that we can apply all the changes to them simultanously and won't have to do it twice
"""
#clubbing the test and training data
df = pd.concat((train, test), axis = 0)
#NAN Values
df.info()

"""
### So 4 features contain NAN values with them.
### lis = ('Age', 'Fare, 'Cabin', 'Embarked')
### Now we visualise with a heat map too.
"""
sns.heatmap(df.isnull(), yticklabels = False, cbar = False)
# Replacing NAN values with either mean values or mode values. 
# Also Feature 'Cabin' has only 295 non null values out of 1309, so we basically drop the whole column for now.
df = df.drop(['Cabin'], axis = 1)
df["Age"] = df["Age"].fillna(df["Age"].mean())
df["Fare"] = df["Fare"].fillna(df["Fare"].mean())
df["Embarked"] = df["Embarked"].fillna(df["Embarked"].mode()[0])

sns.heatmap(df.isnull(), yticklabels = False, cbar = False)
"""
### So There is no NAN values left.

### Now we need to take care of the categorical data.
### col = (Pclass, Sex, SibSp, Parch, Embarked) are the features which contain categorical data.
### Note that there are two kind of Categorical Data: Nominal and Ordinal data. All these column are Nominal Categorical Data.
### We deal with Nominal Data By creating Dummmy variables which are binary columns basically.
"""
#Nominal Categorical Data
col = ['Pclass', 'Sex', 'SibSp', 'Parch', 'Embarked']
def one_hot_encod(col, df):
    j = 0
    df_final = df
    for i in col:
        df1 = pd.get_dummies(df[i], drop_first = True)
        df.drop([i], axis = 1, inplace= True)
        
        if j == 0:
            df_final = df1
        else:
            df_final = pd.concat((df_final, df1), axis = 1)
        j += 1
    df_final = pd.concat((df_final, df), axis = 1)
    return df_final
final_df = one_hot_encod(col, df)    
    
final_df

"""
### Note that while creating dummy variables and using get_dummies method, we use a parameter drop_first = True. What it does is that it creates k-1 dummies for K categories. For eg: for Sex, we had two categories male and female. so it removed female dummy column and wherever it is 0 for male, it is automatically assumed it is female.
"""
"""
### Now we deal with textual and mix data.
### In this problem only Name and Ticket columns are of textual type left. Rest all columns are dealt with.
"""
df["Ticket"].value_counts()

"""
### So we see that there is no specific order within the Ticket column. So we discard it for now for our initial iteration. Also Name column should also be droped for our initial iteration. 
"""
final_df = final_df.drop(["Name"], axis = 1)
final_df = final_df.drop(["Ticket"], axis = 1)
final_df
# Now we split our training and test datasets as we have done basic Data Cleaning to be used in our model.

clean_train = final_df.iloc[:len(train)]
clean_test = final_df.iloc[len(train):]
clean_train.to_csv(r"C:\Users\hp\Desktop\titanic problem\New folder\Notebook\clean_train.csv", index = False)
clean_test.to_csv(r"C:\Users\hp\Desktop\titanic problem\New folder\Notebook\clean_test.csv", index = False)

"""
### This is a code only for Data Cleaning and basic data Preprocessing. If you like the code please upvote it.
"""