# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Analyze and prediction on the Kaggle's Titanic dataset

# +
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# %matplotlib inline
#Alterando configurações padrão dos plots
plt.style.use('fivethirtyeight')
plt.rcParams['figure.figsize'] = (10, 5)
# -

df_train = pd.read_csv('dataset/train.csv')

# ## Dataset analyzes and cleaning

df_train.head()

df_train.describe()

df_train.dtypes

#setting passanger ID as the dataframe index
df_train.set_index('PassengerId', inplace=True, verify_integrity=True)

sns.pairplot(df_train, kind="scatter", plot_kws=dict(s=80, edgecolor="white", linewidth=2.5))
plt.show()

# **Ticket Class**: To better visualize the ticket class as a categorical variable, I created a new row where I name each class as *class-1, class-2* and *class-3*. This will help to label the following visualizations.

df_train['Class_label'] = df_train['Pclass'].map({1:'class-1', 2:'class-2', 3:'class-3'})

# ### Age NaN values
# There are missing values for Age and at that point I don't know if it can intefere in the prediction. To gain some insight about this issue I've done explorations over the theme to help me understand the problem.
#
# ***Note:*** The test dataset can have NaN values as well so i think that some data normalization can be needed before validating the model

# Age distribution
df_train['Age'].plot(kind='hist', title='Age Distribution')
plt.show()
#df_train.plot(kind='hist', y='Age', x='Pclass')

#Age distribution by Sex
# referencia: https://medium.com/@lorran.rodr/criando-a-pir%C3%A2mide-et%C3%A1ria-dos-acidentes-de-trabalho-no-brasil-com-seaborn-cd960f56ffa2
df_age = df_train[['Sex', 'Age']].copy()

df_age['age_group'] = pd.cut(df_age.Age, [0, 10, 20, 30, 40, 50, 60, 70, 80])

df_age['count'] = 1

pv = pd.pivot_table(data=df_age, values='count', columns='Sex', index='age_group', aggfunc=np.sum, dropna=True).fillna(0)

pv

df_age['piram_age'] = df_age.apply(lambda x: x['Age']*(-1) if x['Sex'] == 'male' else x['Age'], axis=1)

df_age.sort_values('Age', ascending=False)

df_age.head(50)

bar_order = df_age.Age.unique()[::-1]

sns.barplot(x='PassengerId', y='Sex', data=df_age.loc[df_age['Sex']=='male',:])

sb.violinplot(data=df_train, x='Class_label', y='Age', inner=None, order=[ "class-1", "class-2", "class-3"]).set_title('Age distribuition by Ticket class')
plt.show()



#Passangers per class
df_train.groupby(['Class_label'])['Class_label'].count().plot(kind='pie', autopct='%.1f%%', )
plt.show()

# TO DOs
# -Criar grafico de distribui sobreposto para idade por sexo
# -Ordenar gráfico de violino pelas classes

# + active=""
# # Null values for Age
# df_train.loc[df_train['Age'].isnull() == True]
# -



StarcoderdataJupyterScriptsDedupFiltered