0% found this document useful (0 votes)
15 views9 pages

B 4 Heart

The document analyzes a heart disease dataset using pandas and seaborn to clean the data, calculate summary statistics, and create visualizations exploring relationships between variables. Key visualizations include correlation heatmaps, line plots of various variables versus age grouped by disease output, and histograms of variables distribution by different groups.

Uploaded by

Suhani Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

B 4 Heart

The document analyzes a heart disease dataset using pandas and seaborn to clean the data, calculate summary statistics, and create visualizations exploring relationships between variables. Key visualizations include correlation heatmaps, line plots of various variables versus age grouped by disease output, and histograms of variables distribution by different groups.

Uploaded by

Suhani Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

4/14/24, 10:12 AM B_4_Heart

In [1]: import pandas as pd

In [2]: df = pd.read_csv('heart.csv')

Note: Here in Dataset "trtbps" assume it as RestBP


In [3]: df
# 303 rows × 14 columns

Out[3]: age sex cp trtbps chol fbs restecg thalachh exng oldpeak slp caa thall output

0 63 1 3 145 233 1 0 150 0 2.3 0 0 1 1

1 37 1 2 130 250 0 1 187 0 3.5 0 0 2 1

2 41 0 1 130 204 0 0 172 0 1.4 2 0 2 1

3 56 1 1 120 236 0 1 178 0 0.8 2 0 2 1

4 57 0 0 120 354 0 1 163 1 0.6 2 0 2 1

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

298 57 0 0 140 241 0 1 123 1 0.2 1 0 3 0

299 45 1 3 110 264 0 1 132 0 1.2 1 0 3 0

300 68 1 0 144 193 1 1 141 0 3.4 1 2 3 0

301 57 1 0 130 131 0 1 115 1 1.2 1 1 3 0

302 57 0 1 130 236 0 0 174 0 0.0 1 1 2 0

303 rows × 14 columns

In [4]: df = df.drop_duplicates()

In [5]: df

localhost:8888/nbconvert/html/Desktop/DSBDA GROP B/Assignment 4/B_4_Heart.ipynb?download=false 1/9


4/14/24, 10:12 AM B_4_Heart

Out[5]: age sex cp trtbps chol fbs restecg thalachh exng oldpeak slp caa thall output

0 63 1 3 145 233 1 0 150 0 2.3 0 0 1 1

1 37 1 2 130 250 0 1 187 0 3.5 0 0 2 1

2 41 0 1 130 204 0 0 172 0 1.4 2 0 2 1

3 56 1 1 120 236 0 1 178 0 0.8 2 0 2 1

4 57 0 0 120 354 0 1 163 1 0.6 2 0 2 1

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

298 57 0 0 140 241 0 1 123 1 0.2 1 0 3 0

299 45 1 3 110 264 0 1 132 0 1.2 1 0 3 0

300 68 1 0 144 193 1 1 141 0 3.4 1 2 3 0

301 57 1 0 130 131 0 1 115 1 1.2 1 1 3 0

302 57 0 1 130 236 0 0 174 0 0.0 1 1 2 0

302 rows × 14 columns

Duplicates removed
In [6]: df.isna().sum()
# No null values, it's clean

age 0
Out[6]:
sex 0
cp 0
trtbps 0
chol 0
fbs 0
restecg 0
thalachh 0
exng 0
oldpeak 0
slp 0
caa 0
thall 0
output 0
dtype: int64

Plots
In [7]: import seaborn as sns
import matplotlib.pyplot as plt

In [8]: df.columns

Index(['age', 'sex', 'cp', 'trtbps', 'chol', 'fbs', 'restecg', 'thalachh',


Out[8]:
'exng', 'oldpeak', 'slp', 'caa', 'thall', 'output'],
dtype='object')

In [9]: plt.figure(figsize=(10,6))
sns.heatmap(df.corr(),cmap = 'YlGnBu', annot = True)

localhost:8888/nbconvert/html/Desktop/DSBDA GROP B/Assignment 4/B_4_Heart.ipynb?download=false 2/9


4/14/24, 10:12 AM B_4_Heart
<Axes: >
Out[9]:

Line chart
In [10]: sns.lineplot(data=df,x=df.age,y=df.cp,hue='output')

<Axes: xlabel='age', ylabel='cp'>


Out[10]:

In [11]: sns.lineplot(data=df,x=df.age,y=df.trtbps,hue='output')

localhost:8888/nbconvert/html/Desktop/DSBDA GROP B/Assignment 4/B_4_Heart.ipynb?download=false 3/9


4/14/24, 10:12 AM B_4_Heart
<Axes: xlabel='age', ylabel='trtbps'>
Out[11]:

In [12]: sns.lineplot(data=df,x=df.age,y=df.chol,hue='output')

<Axes: xlabel='age', ylabel='chol'>


Out[12]:

In [13]: sns.lineplot(df,x=df.oldpeak,y=df.exng,hue='output')

<Axes: xlabel='oldpeak', ylabel='exng'>


Out[13]:

localhost:8888/nbconvert/html/Desktop/DSBDA GROP B/Assignment 4/B_4_Heart.ipynb?download=false 4/9


4/14/24, 10:12 AM B_4_Heart

In [14]: # Shows the Distribution of Heat Diseases with respect to male and female
sns.histplot(data=df,
x=df.output,
hue=df.sex)

<Axes: xlabel='output', ylabel='Count'>


Out[14]:

In [15]: # Shows the Distribution of cp types with respect to male and female
sns.histplot(data=df,

localhost:8888/nbconvert/html/Desktop/DSBDA GROP B/Assignment 4/B_4_Heart.ipynb?download=false 5/9


4/14/24, 10:12 AM B_4_Heart
x=df.cp,
hue=df.sex)

<Axes: xlabel='cp', ylabel='Count'>


Out[15]:

In [16]: sns.histplot(data=df,x=df.cp, hue='output')

<Axes: xlabel='cp', ylabel='Count'>


Out[16]:

localhost:8888/nbconvert/html/Desktop/DSBDA GROP B/Assignment 4/B_4_Heart.ipynb?download=false 6/9


4/14/24, 10:12 AM B_4_Heart

In [17]: # Shows the Distribution of age w.r.t output


sns.histplot(data=df,x=df['age'], hue='output')

<Axes: xlabel='age', ylabel='Count'>


Out[17]:

In [18]: sns.histplot(data=df,x=df.trtbps, hue='output')

<Axes: xlabel='trtbps', ylabel='Count'>


Out[18]:

localhost:8888/nbconvert/html/Desktop/DSBDA GROP B/Assignment 4/B_4_Heart.ipynb?download=false 7/9


4/14/24, 10:12 AM B_4_Heart

In [19]: temp_df = df[['age','cp', 'trtbps','chol','fbs','oldpeak','output']]


plt.figure(figsize=(15,10))
sns.pairplot(temp_df,hue="output")
plt.title("Looking for Insites in Data")
plt.legend("HeartDisease")
plt.tight_layout()
plt.plot()

C:\Users\Admin\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning: T
he figure layout has changed to tight
self._figure.tight_layout(*args, **kwargs)
C:\Users\Admin\AppData\Local\Temp\ipykernel_13800\1194111231.py:6: UserWarning: Th
e figure layout has changed to tight
plt.tight_layout()
[]
Out[19]:
<Figure size 1500x1000 with 0 Axes>

In [20]: plt.figure(figsize=(15,10))
for i,col in enumerate(temp_df.columns,1):
plt.subplot(4,3,i)
plt.title(f"Distribution of {col} Data")
sns.histplot(df[col],kde=True)
plt.tight_layout()
plt.plot()

localhost:8888/nbconvert/html/Desktop/DSBDA GROP B/Assignment 4/B_4_Heart.ipynb?download=false 8/9


4/14/24, 10:12 AM B_4_Heart

C:\Users\Admin\AppData\Local\Temp\ipykernel_13800\4266913939.py:6: UserWarning: Th
e figure layout has changed to tight
plt.tight_layout()
C:\Users\Admin\AppData\Local\Temp\ipykernel_13800\4266913939.py:6: UserWarning: Th
e figure layout has changed to tight
plt.tight_layout()
C:\Users\Admin\AppData\Local\Temp\ipykernel_13800\4266913939.py:6: UserWarning: Th
e figure layout has changed to tight
plt.tight_layout()
C:\Users\Admin\AppData\Local\Temp\ipykernel_13800\4266913939.py:6: UserWarning: Th
e figure layout has changed to tight
plt.tight_layout()
C:\Users\Admin\AppData\Local\Temp\ipykernel_13800\4266913939.py:6: UserWarning: Th
e figure layout has changed to tight
plt.tight_layout()
C:\Users\Admin\AppData\Local\Temp\ipykernel_13800\4266913939.py:6: UserWarning: Th
e figure layout has changed to tight
plt.tight_layout()

In [ ]:

localhost:8888/nbconvert/html/Desktop/DSBDA GROP B/Assignment 4/B_4_Heart.ipynb?download=false 9/9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy