import pandas as pd
train = pd.read_csv('/kaggle/input/titanic/train.csv', index_col=0)
test = pd.read_csv('/kaggle/input/titanic/test.csv', index_col=0)
"""
# data analysis
"""
train.groupby(['Sex', 'Survived']).size() * 100 / len(train)
"""
The value shown above are in percentage.
A lot of male died (52%). A lot of female survived (26%).
Hence by answering `male=died` and `female=survived` we can expect that it will be around 52+26 = 78 percent correct.

To improve our score, there are two things that need to be done:
1. Find the survived males (12%)
2. Find the died females (9%)
"""
"""
## 1. Find the survived males (12%)
"""
"""
- Can't find anything significant/interesting so far.
"""
"""
## 2. Find the died females (9%)
"""
"""
### 2.1. Pclass
"""
female = train[train.Sex=='female']
female.groupby('Pclass').Survived.mean() * 100
"""
Females on `Pclass=3` have 50% chance of surviving. Let's dig deeper to find which one is died.
"""
"""
In [part 1 of this notebook](https://www.kaggle.com/thariqnugrohotomo/without-machine-learning), we use the family size (`SubSp+Parch`) feature to find out which female is died. We'll use it again here.
"""
class3_female = female[female.Pclass==3].copy()
class3_female['fam'] = class3_female.SibSp + class3_female.Parch
class3_female.groupby('fam').Survived.mean() * 100
"""
The result is still hold. 

When female's `fam`$\leq3$, she has greater chance of surviving (51% until 83%). But when`fam`$\gt3$, then the chance is dropping ($\leq37.5$%).

**But**, how about different `Pclass`?
"""
nonclass3_female = female[female.Pclass!=3].copy()
nonclass3_female['fam'] = nonclass3_female.SibSp + nonclass3_female.Parch
nonclass3_female.groupby('fam').Survived.mean() * 100
"""
When `Pclass` is not `3`, then the family size doesn't matter anymore. They have a very high chance of surviving.
"""
"""
# Answering
"""
"""
Based on the obsevations above, we'll use the following pseudocode:
```python
if male:
    return 0
if female:
    if Pclass==3 and fam>3:
        return 0
    else:
        return 1
```
"""
test['fam'] = test.SibSp + test.Parch
def predict(passenger:pd.Series):
    if passenger.Sex == 'male':
        return 0
    else: # female
        if passenger.Pclass==3 and passenger.fam>3:
            return 0
        else:
            return 1
answer = [int(predict(test.iloc[i])) for i in range(len(test))]
answer = pd.DataFrame(answer, index=test.index, columns=['Survived'])
answer.to_csv('submission.csv')
answer