import numpy as np 
import pandas as pd 
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
train = pd.read_csv('/kaggle/input/titanic/train.csv')
test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
labels = train.Survived
train.info()
"""
#### So there are three columns with missing value that is Age, Cabin and Embarked. Among them Cabin has very less values so I might reject it all together. 
"""
train.head()
"""
## Data-Visualisation
"""
ax = sns.countplot(train['Survived'])
for p in ax.patches:
        ax.annotate(format(p.get_height()), (p.get_x()+0.35, p.get_height()+1))
"""
So aparently, more number of people died and less people were able to survive. Percentage wise ****(549/891)*100 = 61%***** of the people were not able to survive 
"""
def bar_chart(feature):
    survived = train[train['Survived']==1][feature].value_counts()
    dead = train[train['Survived']==0][feature].value_counts()
    df = pd.DataFrame([survived,dead])
    df.index = ['Survived','Dead']
    df.plot(kind='bar',stacked=True, figsize=(10,5))
sns.countplot(train['Sex'],hue ='Survived',data=train)
bar_chart('Sex')
"""
#### Insights from this:- 
1. More than 450 men have died
2. Around 100 men were able to survive
3. Around 250 female survived 
4. Less than 100 females died 
5. Chances of survival for females were more than that of male .
"""
bar_chart('Pclass')
ax =  sns.countplot(train['Pclass'],hue ='Survived',data=train)
for p in ax.patches:
        ax.annotate(format(p.get_height()), (p.get_x()+0.15, p.get_height()+1))
print("Survival percentage of passenger in 1st class - ", (134/(134+80)*100))
print("Survival percentage of passenger in 2nd class - ", (87/(87+97)*100))
print("Survival Percentage of passenger in 3rd class - ", (119/(119+372)*100)) 
"""
Insights from this:- 
1. Passengers had much higher chance of survival around 63 %  if they travelled in first class .
2. Passengers who travelled in second class had a survival chance of 47%
3. Passengers had much lower chance of survival around 24 % if they travelled in third class .
"""
plt.figure(figsize=(20,5))
sns.set(style="darkgrid")
sns.distplot(train['Age'], bins = 40)
def age_pie(x,y,title):
    total_010 = train.loc[(train.Age >= x) & (train.Age <= y) ]['Survived'].count()
    survived_010 = train.loc[(train.Age <= y) & (train.Age >= x)]['Survived'].sum()
    fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
    ax.pie(x=[total_010 - survived_010,survived_010],labels = ['dead','Survived'], autopct='%1.1f%%')
    ax.set_title(title)
age_pie(0,10,'percentage of people died and survived represented in a pie chart between the age of 0 - 10 ')

age_pie(10,20,'percentage of people died and survived represented in a pie chart between the age of 10 - 20 ')
age_pie(20,30,'percentage of people died and survived represented in a pie chart between the age of 20 - 30 ')

age_pie(30,40,'percentage of people died and survived represented in a pie chart between the age of 30 - 40 ')
age_pie(40,50,'percentage of people died and survived represented in a pie chart between the age of 40 - 50 ')
age_pie(50,60,'percentage of people died and survived represented in a pie chart between the age of 50 - 60 ')
"""
Insights from this - 
1. People aged 1 to people aged 80 were in the titanic
2. Most of the people was between the range of 16 to 40 
3. Maximum number of people were 23 or 24 years old. 
4. Chances of death was maximum betwwen 20 - 30 had a percentage of 65 % 
5. In the other age groups the chances of death was around 60 % 
"""
"""
Here SibSp stands for siblings or spouse and ParCh stands for parents and children. This column shows whether the passenger had any parent or spouse or children or siblings
"""
ax = sns.countplot(train['SibSp'],hue ='Survived',data=train)
for p in ax.patches:
        ax.annotate(format(p.get_height()), (p.get_x(), p.get_height()+1))

ax = sns.countplot(train['Parch'],hue ='Survived',data=train)
for p in ax.patches:
        ax.annotate(format(p.get_height()), (p.get_x(), p.get_height()+1))

plt.figure(figsize=(8,4))
plt.subplot(122)
sns.barplot(train.SibSp, train.Survived)
plt.subplot(122)
sns.barplot(train.Parch, train.Survived)
plt.subplots_adjust(wspace=0.5)

"""
Insights from here - 
1. Most of the people travelled alone or with 1 or 2 sibling or 1 spouse. 
2. People travelling alone had more chances of not surviving than people travelling with 1 or 2 person. 
3. Around 75 % of the people travelled alone without parents or children.
"""
"""
Lets analyze the fare column and then we will move on to data pre processing 
"""
plt.figure(figsize=(20,5))
sns.set(style="darkgrid")
sns.distplot(train['Fare'])
"""
The kernel density estimation curve shows the distribution of the fare that the passenger has given. 
"""
facet = sns.FacetGrid(train, hue="Survived",aspect=4)
facet.map(sns.kdeplot,'Fare',shade= True)
facet.set(xlim=(0, train['Fare'].max()))
facet.add_legend()
"""
Insights from this - 
1. So fair prices were more or less around 0 to 50 
2. Maximum number of people paid a fare of  10 
3. Very few people paid a fare more than  500 
"""
"""
So after the exploration I have selected 6 features which are Pclass, Age, Gender, fare, SibSp(sibling or spouse ) and Parch ( parent or children)

Features Ignored - 
1. passengerID - I don't think passenger Id has much importance in the deciding whether the passenger died or not 
2. Name - Same reason as above 
3. Cabin - I rejected it as most of the cabin value is missing. 
4. Embarked - Embarked means from where the passenger has boarded the titanic,it may have some indirect effect but surely will not effect the prediction directly
"""
"""
## Data cleaning and processing
"""
plt.figure(figsize=(12,8))
sns.heatmap(train.isnull(),cbar=False, yticklabels=False, cmap='viridis')
train.Age.mean()
"""
Lets start handling the missing values from the Age column
"""
train.Age.isnull().sum()
"""
Now there are lots of ways to handle the missing values, for example I can replace the missing values with the help of mean or median, or can replace with random samples or we replace it with some values at the end of the distribution. 
In this one what I am going to do is basically replace the age with the help of mean but based on their Pclass.
For example, the mean of the Age of people in class 1 was around 37 so the person whose age was missing and he was in class 1 I will replace it with 37 instead of the overall mean age which is 29. 
"""
train.groupby('Pclass').Age.mean()
def inpute_age(cols):
    Age = cols[0]
    Pclass = cols[1]
    
    if pd.isnull(Age):
        if Pclass == 1:
            return 37
        elif Pclass == 2:
            return 29
        else: return 24
    else: return Age
train['Age']=train[['Age','Pclass']].apply(inpute_age, axis=1)
plt.figure(figsize=(12,8))
sns.heatmap(train.isnull(),cbar=False, yticklabels=False, cmap='viridis')
"""
#### So I have cleaned the Age column.
Next what I will do is basically drop the embarked column because there is not much direct correlation to it with the survival of the passenger. 
"""
train.drop('Embarked', axis=1, inplace=True)
plt.figure(figsize=(12,6))
sns.heatmap(train.isnull(),cbar=False, yticklabels=False, cmap='viridis')
"""
Now lets move onto the Cabin column which is bit trcky to deal with because there is a correlation between the missing values and survival. The first question that comes to the mind is that why such a huge number of data for this column is missing. This is cannot be because of human error or miss calculation. But the if we replace the missing values with the highest number of occuring one then it will make the dataset imbalanced. And will not have much advantage.

"""
train.Cabin.value_counts()
"""
Now the column is a categorical variable so i cannot make mean median imputation. So instead I will replace the missing values with random imputation. 
"""
train.drop('Cabin', axis=1, inplace=True)
plt.figure(figsize=(12,6))
sns.heatmap(train.isnull(),cbar=False, yticklabels=False, cmap='viridis')
train.info()

train['Male'] = pd.get_dummies(train['Sex'], drop_first=True)
train.drop(['PassengerId', 'Name', 'Sex', 'Ticket'], axis=1, inplace=True)
train.head()
train.info()
train.info()
#Seperate the feature columns from the target column
X = train.drop('Survived', axis=1)
y = train['Survived']
X.info()
y
x_train, x_test,y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=0)
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_jobs=-1, n_estimators=14)
clf.fit(x_train,y_train)
score = clf.score(x_test,y_test)
print("accuracy", round(score,2)*100)
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
clf = make_pipeline(StandardScaler(), SVC(gamma='auto'))
clf.fit(x_train, y_train)
score = clf.score(x_test,y_test)
print("accuracy", round(score,2)*100)
test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
#Clean the test data the same way we did the training data
test_df['Age']=test_df[['Age','Pclass']].apply(inpute_age, axis=1)
test_df.drop('Cabin', axis=1, inplace=True)
test_df['Male'] = pd.get_dummies(test_df['Sex'], drop_first=True)
test_df.drop(['PassengerId', 'Name', 'Ticket', 'Embarked'], axis=1, inplace=True)
test_df.info()
test_df.drop(['Sex'], axis=1, inplace=True)
test_df.info()
test_df.isnull().sum()
mean = test_df.Fare.mean()
test_df['Fare'] = test_df.Fare.fillna(mean)
test_df.Fare.isnull().sum()
y_prediction = clf.predict(test_df)
test = pd.read_csv('/kaggle/input/titanic/test.csv')
pass_ids = test['PassengerId']
submission = pd.DataFrame({
        "PassengerId": pass_ids,
        "Survived": y_prediction
    })
submission.to_csv('titanic.csv', index=False)
"""
If you have made it this far then thank you so much for your time, if it helped you or you liked please considering upvoting 
And if you have some suggestions or things that I can improve, please let me know in the comments 

"""