<jupyter_start><jupyter_text>Exploratory Data Analysis (EDA)  Load the dataset<jupyter_code>import pandas as pd

df=pd.read_csv('https://raw.githubusercontent.com/parvatijay2901/Machine-Learning-with-the-Titanic-dataset-on-Azure/main/Training_data.csv')<jupyter_output><empty_output><jupyter_text>Data analysis<jupyter_code>df.head(8)
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
PassengerId    891 non-null int64
Survived       891 non-null int64
Pclass         891 non-null int64
Name           891 non-null object
Sex            891 non-null object
Age            714 non-null float64
SibSp          891 non-null int64
Parch          891 non-null int64
Ticket         891 non-null object
Fare           891 non-null float64
Cabin          204 non-null object
Embarked       889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB<jupyter_text>Impute missing values<jupyter_code>import numpy as np

def Impute_missing_values(df):
    df.drop(columns=['Cabin'],inplace=True)
    df['Age'].fillna(df['Age'].median(), inplace=True)
    df['Embarked'].fillna('S', inplace=True) 
Impute_missing_values(df)
df.info()
df.describe()<jupyter_output><empty_output><jupyter_text>Univariate analysis  Survived<jupyter_code>!pip install seaborn
import seaborn as sns

sns.countplot(df['Survived'])
death_percent=(df['Survived'].value_counts().values[0]/891)*100

print("Out of 891 passengers, {}% died in the accident".format(death_percent))<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>Pclass<jupyter_code>sns.countplot(df['Pclass'])

print((df['Pclass'].value_counts()/891)*100)<jupyter_output>3    55.106622
1    24.242424
2    20.650954
Name: Pclass, dtype: float64<jupyter_text>Sex<jupyter_code>sns.countplot(df['Sex'])

print((df['Sex'].value_counts()/891)*100)<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>SibSp<jupyter_code>sns.countplot(df['SibSp'])

print((df['SibSp'].value_counts()/891)*100)<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>Parch<jupyter_code>sns.countplot(df['Parch'])

print((df['Parch'].value_counts()/891)*100)<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>Embarked<jupyter_code>sns.countplot(df['Embarked'])

print((df['Embarked'].value_counts()/891)*100)<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>Age<jupyter_code>sns.distplot(df['Age'])
print("Skew: ",df['Age'].skew())
print("Kurtosis: ",df['Age'].kurt())
sns.boxplot(df['Age'])<jupyter_output><empty_output><jupyter_text>Fare<jupyter_code>sns.distplot(df['Fare'])
print("Skew: ",df['Fare'].skew())
print("Kurtosis: ",df['Fare'].kurt())
sns.boxplot(df['Fare'])<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>Bivariate analysis  Sex - Survived<jupyter_code>sns.countplot(df['Survived'],hue=df['Sex'])
pd.crosstab(df['Sex'],df['Survived']).apply(lambda r: round((r/r.sum())*100,1), axis=1)<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>Pclass - Survived<jupyter_code>sns.countplot(df['Survived'],hue=df['Pclass'])
pd.crosstab(df['Pclass'],df['Survived']).apply(lambda r: round((r/r.sum())*100,1), axis=1)<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>Embarked - Survived<jupyter_code>sns.countplot(df['Survived'],hue=df['Embarked'])
pd.crosstab(df['Embarked'],df['Survived']).apply(lambda r: round((r/r.sum())*100,1), axis=1)<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>Age - Survived<jupyter_code>import matplotlib.pyplot as plt
plt.figure(figsize=(15,6))
sns.distplot(df[df['Survived']==0]['Age']) #blue
sns.distplot(df[df['Survived']==1]['Age']) #orange<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>Fare - Survived<jupyter_code>plt.figure(figsize=(15,6))
sns.distplot(df[df['Survived']==0]['Fare']) #blue
sns.distplot(df[df['Survived']==1]['Fare']) #orange<jupyter_output>/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>Multivariate analysis<jupyter_code>sns.pairplot(df)
sns.heatmap(df.corr())<jupyter_output><empty_output><jupyter_text>Data preprocessing  Creating categories<jupyter_code>def Family_type(number):
    if number==0:
        return 'Alone'
    elif number>0 and number<=4:
        return 'Medium'
    else:
        return 'Large'
def Transform_data(df):
    df['Family_size']=df['Parch']+df['SibSp']
    df['Family_type']=df['Family_size'].apply(Family_type)
    df.drop(columns=['SibSp', 'Parch', 'Family_size'], inplace=True)
    df.loc[ df['Age'] <= 16, 'Age'] = 1
    df.loc[(df['Age'] > 16) & (df['Age'] <= 26), 'Age'] = 2
    df.loc[(df['Age'] > 26) & (df['Age'] <= 36), 'Age'] = 3
    df.loc[(df['Age'] > 36) & (df['Age'] <= 62), 'Age'] = 4
    df.loc[ df['Age'] > 62, 'Age'] = 5
    df.loc[df['Fare'] <= 17, 'Fare'] = 1,
    df.loc[(df['Fare'] > 17) & (df['Fare'] <= 30), 'Fare'] = 2,
    df.loc[(df['Fare'] > 30) & (df['Fare'] <= 100), 'Fare'] = 3,
    df.loc[ df['Fare'] > 100, 'Fare'] = 4
Transform_data(df)

df.head()
df=pd.get_dummies(data=df, columns=['Age' ,'Fare',  'Pclass', 'Sex', 'Embarked', 'Family_type'], drop_first=True)
plt.figure(figsize=(15,6))
sns.heatmap(df.corr())
df.drop(columns=['Ticket', 'PassengerId', 'Name','Age_5.0'],inplace=True)
df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1380191693290735, 'fraction_numerical': 0.0349307774227902, 'mean_word_length': 6.029191616766467, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 1, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '227028', 'license': 'Apache-2.0', 'n_tokens_mistral': 3495, 'n_tokens_neox': 3211, 'n_words': 744, 'path': 'Files/EDA.ipynb', 'repo_name': 'parvatijay2901/Machine-Learning-with-the-Titanic-dataset-on-Azure', 'size': '9390'}