import pandas as pd
import numpy as np
from sklearn import model_selection
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
df = pd.read_csv("../input/titanic/train.csv")
df.head()
# Let's check the distribution of Survived
df["Survived"].value_counts()
"""
## We'll use Stratified KFold as cross validation
"""
# adding a kfold column
df = pd.read_csv("../input/titanic/train.csv")
df["kfold"] = -1
df = df.sample(frac=1).reset_index(drop=True)
y = df["Survived"].values
kf = model_selection.StratifiedKFold(n_splits=5)

for fold, (t, v) in enumerate(kf.split(X=df, y=y)):
    df.loc[v, "kfold"] = fold

# save the csv
df.to_csv(f"titanic_folds.csv", index=False)
df = pd.read_csv("./titanic_folds.csv")
df.head()
df.info()
"""
# Analysing features

"""
"""
## 1. Passenger ID
- is unique for every passenger
- therefore, of no use
"""
"""
## 2. Survived
- target variable
"""
y = df["Survived"].values
print(y.shape)
"""
## 3. Pclass
"""
# 216 + 184 + 491 = 891
# Hence no null values
# This feature will be considered
print(np.unique(df["Pclass"], return_counts=True))
"""
## 4. Name
- may be not as important
- will not consider for now
"""
"""
## 5. Sex
"""
# This feature will be considered
print(np.unique(df["Sex"], return_counts = True))
lbl = LabelEncoder()
df["Sex"] = lbl.fit_transform(df["Sex"])
df[(df["Survived"] == 1)]["Sex"].hist(bins=10, edgecolor='white')
"""
## More Females survived than Males
"""
"""
## 6. Age
"""
df[(df["Survived"] == 1)]["Age"].hist(bins=10, edgecolor='white')
plt.show()
"""
### More Children survived
"""
df[df["Survived"] == 0]["Age"].hist(bins=10, edgecolor='white')
plt.show()
"""
## 7. Sibsp
"""
# This feature will be considered
print(np.unique(df["SibSp"], return_counts = True))
"""
## 8. Parch
"""
# This feature will be considered
print(np.unique(df["Parch"], return_counts = True))
"""
## 9. Ticket
- is (almost) unique for every passenger
- ignored
"""
"""
## 10. Fare
"""
plt.style.use('seaborn')
plt.hist(df["Fare"], bins=40, edgecolor='white')
plt.show()
"""
## 11. Cabin
"""
cabin_dict = {}

for cbn in df["Cabin"]:
    
    if cbn in cabin_dict:
        cabin_dict[cbn] += 1
        
    else:
        cabin_dict[cbn] = 1
        
print(cabin_dict)
cabin_df = pd.DataFrame(columns=["Cabin_name", "Count"])
cabin_df["Cabin_name"] = cabin_dict.keys()
cabin_df["Count"] = cabin_dict.values()

cabin_df.head()
cabin_df.sort_values(by="Count").reset_index(drop=True)
"""
### out of 891 passengers, 687 cabin names are unknown/missing
"""
df[df["Cabin"].isnull()]["Survived"].value_counts()
df[df["Cabin"].isnull() == False]["Survived"].value_counts()
df = pd.read_csv("../input/titanic/train.csv").copy()
# df.Cabin.fillna("NONE")
df["Cabin"] = df["Cabin"].astype(str)
for i, cbn in enumerate(df["Cabin"]):
  
    # if the passenger has a cabin assign A
    if cbn != "nan":
        df.loc[i, "Cabin"] = "A"
        
    else:
        df.loc[i, "Cabin"] = "NONE"
df["Cabin"]
temp = df.groupby(
    [
        "Sex",
        "Cabin",
        "Survived"
    ]
)["Survived"].count().reset_index(name="Survived_count")
temp.head(n=8)
"""
## Some Conclusions
- Males without a Cabin didn't survive (mostly)
- Females with a cabin have survived (mostly)
"""
"""
## 12. Embarked
"""
df.Embarked.fillna("NONE")
print(df[df["Embarked"].isnull()])

df["Embarked"] = df["Embarked"].astype(str)
# Embarked is not a very informative feature I see
# Gonna ignore it in the model initially
df.info()