<jupyter_start><jupyter_text>Titanic Dataset Analysis  Data Visualization<jupyter_code>DATA_LOCATION = "../data/"<jupyter_output><empty_output><jupyter_text>Gender survival plots<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')
import warnings
warnings.filterwarnings('ignore')
data= pd.read_csv( DATA_LOCATION + 'raw/train.csv')
data

f,ax=plt.subplots(1,2,figsize=(18,8))
data[['Sex','Survived']].groupby(['Sex']).mean().plot.bar(ax=ax[0])
ax[0].set_title('Survived vs Sex')
sns.countplot('Sex',hue='Survived',data=data,ax=ax[1])
ax[1].set_title('Sex:Survived vs Dead')

plt.show()

# plt.savefig('gender_survival_plots.png')<jupyter_output><empty_output><jupyter_text>Survival plots<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')
import warnings
warnings.filterwarnings('ignore')

data= pd.read_csv( DATA_LOCATION + 'raw/train.csv')

f,ax=plt.subplots(1,2,figsize=(18,8))
data['Survived'].value_counts().plot.pie(explode=[0,0.1],autopct='%1.1f%%',ax=ax[0],shadow=True)
ax[0].set_title('Survived')
ax[0].set_ylabel('')
sns.countplot('Survived',data=data,ax=ax[1])
ax[1].set_title('Survived')
plt.show()

plt.savefig('survival_plots.png')


import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use('fivethirtyeight')
import warnings

warnings.filterwarnings('ignore')

data= pd.read_csv( DATA_LOCATION + 'raw/train.csv')

f, ax = plt.subplots(1, 2, figsize=(18, 8))
data['Pclass'].value_counts().plot.bar(color=['#CD7F32', '#FFDF00', '#D3D3D3'], ax=ax[0])
ax[0].set_title('Number Of Passengers By Pclass')
ax[0].set_ylabel('Count')
sns.countplot('Pclass', hue='Survived', data=data, ax=ax[1])
ax[1].set_title('Pclass:Survived vs Dead')
# plt.show()

plt.savefig('passenger_pclass_plots.png')<jupyter_output><empty_output><jupyter_text>Feature Engineering  Derive features<jupyter_code>import pandas as pd
import warnings
warnings.filterwarnings('ignore')

data= pd.read_csv( DATA_LOCATION + 'raw/train.csv')

data['Initial']=0
for i in data:
    data['Initial']=data.Name.str.extract('([A-Za-z]+)\.') #lets extract the Salutations

data['Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don'],['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr'],inplace=True)

data.head()<jupyter_output><empty_output><jupyter_text>Process NaN<jupyter_code>import pandas as pd
import warnings
warnings.filterwarnings('ignore')

# data= pd.read_csv( DATA_LOCATION + 'raw/train.csv')

data['Embarked'].fillna('S',inplace=True)

data.head()<jupyter_output><empty_output><jupyter_text>Process Age<jupyter_code>import warnings
warnings.filterwarnings('ignore')


## Assigning the NaN Values with the Ceil values of the mean ages
data.loc[(data.Age.isnull())&(data.Initial=='Mr'),'Age']=33
data.loc[(data.Age.isnull())&(data.Initial=='Mrs'),'Age']=36
data.loc[(data.Age.isnull())&(data.Initial=='Master'),'Age']=5
data.loc[(data.Age.isnull())&(data.Initial=='Miss'),'Age']=22
data.loc[(data.Age.isnull())&(data.Initial=='Other'),'Age']=46

data['Age_band']=0
data.loc[data['Age']<=16,'Age_band']=0
data.loc[(data['Age']>16)&(data['Age']<=32),'Age_band']=1
data.loc[(data['Age']>32)&(data['Age']<=48),'Age_band']=2
data.loc[(data['Age']>48)&(data['Age']<=64),'Age_band']=3
data.loc[data['Age']>64,'Age_band']=4

data.head()<jupyter_output><empty_output><jupyter_text>Process Family<jupyter_code>import pandas as pd
import warnings
warnings.filterwarnings('ignore')

data['Family_Size']=0
data['Family_Size']=data['Parch']+data['SibSp']
data['Alone']=0
data.loc[data.Family_Size==0,'Alone']=1

data.head()<jupyter_output><empty_output><jupyter_text>Process Fare<jupyter_code>import pandas as pd
import warnings
warnings.filterwarnings('ignore')

data['Fare_Range']=pd.qcut(data['Fare'],4)

data['Fare_cat']=0
data.loc[data['Fare']<=7.91,'Fare_cat']=0
data.loc[(data['Fare']>7.91)&(data['Fare']<=14.454),'Fare_cat']=1
data.loc[(data['Fare']>14.454)&(data['Fare']<=31),'Fare_cat']=2
data.loc[(data['Fare']>31)&(data['Fare']<=513),'Fare_cat']=3


data.head()<jupyter_output><empty_output><jupyter_text>Replace features<jupyter_code>import pandas as pd
import warnings
warnings.filterwarnings('ignore')

data['Sex'].replace(['male','female'],[0,1],inplace=True)
data['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True)
data['Initial'].replace(['Mr','Mrs','Miss','Master','Other'],[0,1,2,3,4],inplace=True)

data.drop(['Name','Age','Ticket','Fare','Cabin','Fare_Range','PassengerId'],axis=1,inplace=True)


data.head()<jupyter_output><empty_output><jupyter_text>Test train analysis<jupyter_code>import pandas as pd
from sklearn.model_selection import train_test_split
import warnings
warnings.filterwarnings('ignore')


train,test=train_test_split(data,test_size=0.3,random_state=0,stratify=data['Survived'])
train_X=train[train.columns[1:]]
train_X.to_csv("train_X.csv")
train_X
train_Y=train[train.columns[:1]]
train_Y.to_csv("train_Y.csv")
train_Y
test_X=test[test.columns[1:]]
test_X.to_csv("test_X.csv")
test_X
test_Y=test[test.columns[:1]]
test_Y.to_csv("test_Y.csv")
test_Y
X=data[data.columns[1:]]
X.to_csv("X.csv")
X
Y=data['Survived']
Y.to_csv("Y.csv")
Y<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21376336021001313, 'fraction_numerical': 0.023626476654790923, 'mean_word_length': 10.959641255605382, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '660763', 'license': 'MIT', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2135, 'n_words': 246, 'path': 'notebooks/survival_analysis.ipynb', 'repo_name': 'abhi18av/nextflow-datascience-titanic-survival-analysis', 'size': '5333'}