0% found this document useful (0 votes)
9 views

Assign8.ipynb - Colab

The document loads and cleans the Titanic dataset, generates summary statistics and plots to analyze survival rates by variables like gender, class and embarkation port. It loads the data, handles missing values, computes descriptive statistics and creates count plots to visualize the distributions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assign8.ipynb - Colab

The document loads and cleans the Titanic dataset, generates summary statistics and plots to analyze survival rates by variables like gender, class and embarkation port. It loads the data, handles missing values, computes descriptive statistics and creates count plots to visualize the distributions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

4/17/24, 11:28 AM Assign 8.

ipynb - Colab

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

data = pd.read_csv("https://raw.githubusercontent.com/dphi-official/Datasets/master/titan
data

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 1/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket

Braund,
0 1 0 3 Mr. Owen male 22.0 1 0 A/5 21171 7
Harris

Cumings,
Mrs. John
Bradley
1 2 1 1 female 38.0 1 0 PC 17599 71
(Florence
Briggs
Th...

Heikkinen,
STON/O2.
2 3 1 3 Miss. female 26.0 0 0 7
3101282
Laina

Futrelle,
Mrs.
Jacques
3 4 1 1 female 35.0 1 0 113803 53
Heath
(Lily May
Peel)

Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450 8
Henry

... ... ... ... ... ... ... ... ... ...

Montvila,
886 887 0 2 Rev. male 27.0 0 0 211536 13
Juozas

Graham,
Miss.
887 888 1 1 female 19.0 0 0 112053 30
Margaret
Edith

Johnston,
Miss.
W./C.
888 889 0 3 Catherine female NaN 1 2 23
6607
Helen
"Carrie"

Behr, Mr.
889 890 1 1 Karl male 26.0 0 0 111369 30
Howell

Dooley,
890 891 0 3 Mr. male 32.0 0 0 370376 7
Patrick

891 rows × 12 columns

Next steps: Generate code with data


toggle_off View recommended plots

data.shape

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 2/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

(891, 12)

data.describe()

PassengerId Survived Pclass Age SibSp Parch Fa

count 891.000000 891.000000 891.000000 714.000000 891.000000 891.000000 891.0000

mean 446.000000 0.383838 2.308642 29.699118 0.523008 0.381594 32.2042

std 257.353842 0.486592 0.836071 14.526497 1.102743 0.806057 49.6934

min 1.000000 0.000000 1.000000 0.420000 0.000000 0.000000 0.0000

25% 223.500000 0.000000 2.000000 20.125000 0.000000 0.000000 7.9104

50% 446.000000 0.000000 3.000000 28.000000 0.000000 0.000000 14.4542

75% 668.500000 1.000000 3.000000 38.000000 1.000000 0.000000 31.0000

max 891.000000 1.000000 3.000000 80.000000 8.000000 6.000000 512.3292

data.describe(include="object")

Name Sex Ticket Cabin Embarked

count 891 891 891 204 889

unique 891 2 681 147 3

top Braund, Mr. Owen Harris male 347082 B96 B98 S

freq 1 577 7 4 644

data.isnull().sum()

PassengerId 0
Survived 0
Pclass 0
Name 0
Sex 0
Age 177
SibSp 0
Parch 0
Ticket 0
Fare 0
Cabin 687
Embarked 2
dtype: int64

data['Age'] = data['Age'].fillna(data['Age'].mean())

data['Cabin'] = data['Cabin'].fillna(data['Cabin'].mode()[0])

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 3/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

data['Embarked'] = data['Embarked'].fillna(data['Embarked'].mode()[0])

data.isnull().sum()

PassengerId 0
Survived 0
Pclass 0
Name 0
Sex 0
Age 0
SibSp 0
Parch 0
Ticket 0
Fare 0
Cabin 0
Embarked 0
dtype: int64

sns.countplot(data , x='Survived' , hue='Survived')

<Axes: xlabel='Survived', ylabel='count'>

sns.countplot(data , x='Pclass' , hue='Pclass')

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 4/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

<Axes: xlabel='Pclass', ylabel='count'>

sns.countplot(data , x='Embarked' , hue='Embarked')

<Axes: xlabel='Embarked', ylabel='count'>

sns.countplot(data , x='Sex' , hue='Sex')

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 5/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

<Axes: xlabel='Sex', ylabel='count'>

sns.boxplot(data , x='Age')

<Axes: xlabel='Age'>

sns.boxplot(data , x='Fare')

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 6/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

<Axes: xlabel='Fare'>

sns.boxplot(data , x='Pclass')

<Axes: xlabel='Pclass'>

sns.boxplot(data , x='SibSp')

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 7/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

<Axes: xlabel='SibSp'>

sns.catplot(x='Pclass' , y='Age' ,data=data , kind='violin' , hue='Pclass')

<seaborn.axisgrid.FacetGrid at 0x79901e8019f0>

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 8/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

sns.catplot(x='Pclass' , y='Fare' , hue='Pclass' , data=data , kind='strip')

<seaborn.axisgrid.FacetGrid at 0x79901e57bb80>

sns.displot(data['Age'] ,kind='kde')

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 9/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

<seaborn.axisgrid.FacetGrid at 0x79901e57b640>

sns.displot(data['Age'] , kde = True )

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 10/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

<seaborn.axisgrid.FacetGrid at 0x79901e2deaa0>

sns.displot(data['Fare'] , kde=True , bins=7)

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 11/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

<seaborn.axisgrid.FacetGrid at 0x79901e2dfbe0>

sns.displot(data['Pclass'] , kde = True )

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 12/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

<seaborn.axisgrid.FacetGrid at 0x79901e211780>

sns.jointplot(data=data , x='Age' , y='Fare' , hue='Sex')

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 13/14
4/17/24, 11:28 AM Assign 8.ipynb - Colab

<seaborn.axisgrid.JointGrid at 0x79901e2984f0>

sns.rugplot(data['Fare'])

<Axes: xlabel='Fare'>

https://colab.research.google.com/drive/1ljK4lkfUmkhUczqCUkXT8xSTzflyzoVu#scrollTo=gYqdouEKoPMF&printMode=true 14/14

You might also like