"""
# Introduction
the sinking of Titanic is one of the most notorious ship break in the history in 1912,during her voyage. The Titanic sank after colliding with an iceberg. Killing 1502 out of 2224 passengers and craw. 

<front color = "red">
Content: 

1. [Load and Check Data](#1)
1. [Veribale Describition](#2)
      * .[Univariate Varible Analysis](#3)
        * .[Categorical Varible](#4)
        * .[Numerical Varible](#5)
1. [Basic Data Analysis](#6)        
1. [Outlier Detection](#7)
1. [Missing Value](#8)
          * .[Find Missing Value](#9)
          * .[Fill Missing Value](#10)
"""
# 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 in 

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
plt.style.use("seaborn-whitegrid")
import seaborn as sns

from collections import Counter
import warnings
warnings.filterwarnings("ignore")


# Input data files are available in the "../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))

# Any results you write to the current directory are saved as output.
"""
<a id ="1"></a><br>
# Load and Check Data
"""
train_df = pd.read_csv("/kaggle/input/titanic/train.csv")
test_df = pd.read_csv("/kaggle/input/titanic/test.csv")
test_PassengerId = test_df["PassengerId"]

train_df.columns
train_df.head()
train_df.describe()
"""
<a id ="2"></a><br>
# Veribale Describition

1. PassengerId : ınique ıd number to each passenger
2. Survived : passenger survive(1) or die(0)
3. Pclass : passenger class
4. Name
5. Sex 
6. Age 
7. SibSp : number of siblings/spouses
8. Parch : number of parents or childeren
9. Ticket : ticket number
10. Fare : amount of money for spend on ticket
11. Cabin : cabin category
12. Embarked : port where passenger ambarked(c-s-q)

"""
train_df.info()
"""
<a id ="3"></a><br>

# Univariate Varible Analysis
* Categorical Varible : Survived, Sex ,Pclass, embarkerd, Cabin, Name ,Ticket, Sibsp and Parch
* Numerical Varible :,Fare, Age and PassengerId
"""
"""
<a id ="4"></a><br>

## Categorical Varible 

"""
def bar_plot(variable):
    """
    input= variable ex= sex
    output= bar plot & value count 

    """
    # get feature
    var= train_df[variable]
    # count number of catagorical varibale(value/sample)
    varValue= var.value_counts()
    
    # visualize
    plt.figure(figsize=(9,3))
    plt.bar(varValue.index, varValue)
    plt.xticks(varValue.index, varValue.index.values)
    plt.ylabel("Frequency")
    plt.title(variable)
    plt.show()
    print("{}:/n {}".format(variable,varValue) )
category1= ["Survived","Sex","Pclass","Embarked","SibSp","Parch"]
for c in category1:
    bar_plot(c)
category2=["Cabin","Name","Ticket"]
for c in category2:
    print("{} \n".format(train_df[c].value_counts()))
"""
<a id ="5"></a><br>

## Numerical Varible
"""
def plot_hist(variable):
    plt.figure(figsize = (9,3))
    plt.hist(train_df[variable])
    plt.xlabel=(variable)
    plt.ylabel=("frequency")
    plt.title("{} distribution with hist".format(variable))
    plt.show()
numericVar= ["Fare", "Age", "PassengerId"]
for n in numericVar:
    plot_hist(n)
    

"""
<a id ="6"></a><br>
# Basic Data Analysis

* Survived-Pclass
* Survived-Parch
"""
# Survived-Pclass
train_df[["Pclass","Survived"]].groupby(["Pclass"], as_index = False).mean().sort_values(by= "Survived",ascending = False)
# Survived-Parch
train_df[["Parch","Survived"]].groupby(["Parch"], as_index = False).mean().sort_values(by= "Survived",ascending = False)
"""
<a id ="7"></a><br>
# Outlier Detection
"""
def detect_outliers(df,features):
    outlier_indices=[]
    
    
    for c in features:
        #1st quarite
         Q1= np.percentile(df[c],25)
        #3st quarite
         Q3= np.percentile(df[c],75)
        #IQR
         IQR= Q3 - Q1
        #Outlier step
         outlier_step =IQR* 1.5
        #Detect outlier and their indeces
         outlier_list_col = df[(df(c) < Q1 - outlier_step) | (df(c) > Q3 + outlier_step )].index
        #Store indeces
         outlier_indices.extend(outlier_list_col)                       
                               
            
            
    outlier_indices = Counter(outlier_indices)
    multiple_outliers = list(i for i, v in outlier_indices.items() if v > 2)
    
    

    return multiple_outliers                             
"""
<a id ="8"></a><br>

# Missing Value
* Find Missing Value
* Fill Missing Value

          
"""
train_df_len()
train_df = pd.concat([train_df , test_df],axis=0).reset_index(drop=True)
train_df.head()
"""
<a id ="9"></a><br>

## Find Missing Value
"""
train_df.columns[train_df.isnull().any()]
train_df.isnull().sum()
"""
<a id ="10"></a><br>

## Fill Missing Value
* Embarked has 2 missing value
"""
train_df[train_df["Embarked"].isnull()]
train_df.boxplot(column="Fare",by="Embarked")
plt.show()
train_df["Embarked"] = train_df["Embarked"].fillna("C") 
train_df[train_df["Embarked"].isnull()]