Sie sind auf Seite 1von 98

globsyn

HEART DISEASE
ANALYSIS

Group Member
G

Prafull Kumar, Guru Nanak Institute Of Technology,


RegNo:161430110069
Rupak Dey, Guru Nanak Institute Of Technology, RegNo:161430110090
Priyam Halder, Guru Nanak Institute Of
Technology,RegNo:161430110074

4
Table of
Contents

• Acknowledgement
• Project Objective
• Project Scope
• Data Description
• Exploratory Data Analysis
• Analysis of Dataset on the basis of test cases
• Future Scope of Improvements
• Project Certificate

5
Acknowledgement

I take this opportunity to express my


profound gratitude and deep regards to
my faculty (Mr. Sourav Maitra) for his
exemplary guidance, monitoring and
constant encouragement throughout the
course of this project. The blessing, help
and guidance given by him/her time to
time shall carry me a long way in the
journey of life on which I am about to
embark.

I am obliged to my project team


members(Rupak Dey, Priyam Halder) for
the valuable information provided by
them in their respective fields. I am
grateful for their cooperation during the
6
period of my assignment.

(Prafull Kumar)

Project Objective
6
In Today’s world huge no of peoples are affected by heart
disease,and most of them don’t know the its.
The objective of this Heart Disease Analysis Project is to
improve the knowledge on the impact of different types of
heart disease factors, as well as their interactions, on the
occurrence, the evolution of heart disease on the basis of
age and gender in humans.
In this project we have taken different factors such as age
, sex , resting blood pressure ,resting echocardiographic
result etc , and we have analyzed the chance of having
heart disease on these factors

7
Project Scope
 The scope of this Heart Disease Analysis Project is to
make people aware about different issue, which
happens before heart disease.

 By observing the analysis of this project people can


know there current situation of heart disease by
looking their own blood pressure, resting blood sugar,
heart rate etc factors.

 So it will help people to get to know earlier about


these factors, It can save millions of life.

8
In [2]:

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

1. Data Description¶
In [3]:

hd=pd.read_csv ("heart_disease.csv")

In [4]:

HD=pd.DataFrame(hd)

In [5]:

HD.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1025 entries, 0 to 1024
Data columns (total 14 columns):
Age 1025 non-null int64
Sex 1025 non-null int64
Chest_pain_type 1025 non-null int64
resting_blood_pressure 1025 non-null int64
cholestoral 1025 non-null int64
fasting_blood_sugar 1025 non-null int64
resting_cardiographic_results 1025 non-null int64
maximum_heart_rate_achieved 1025 non-null int64
exercise_induced_angina 1025 non-null int64
depression_induced_by_excercise 1025 non-null float64
slope 1025 non-null int64
ca 1025 non-null int64
thal 1025 non-null int64
target 1025 non-null int64
dtypes: float64(1), int64(13)
memory usage: 112.2 KB

In [6]:

HD.head(10)

Out[6]:
Ag Se Chest_pain_ty resting_blood_press cholestor fasting_blood_su resting_cardiographic_
e x pe ure al gar sults

0 52 1 0 125 212 0 1

1 53 1 0 140 203 1 0

2 70 1 0 145 174 0 1
Ag Se Chest_pain_ty resting_blood_press cholestor fasting_blood_su resting_cardiographic_
e x pe ure al gar sults

3 61 1 0 145 203 0 1

4 62 0 0 138 294 1 1

5 58 0 0 100 248 0 0

6 58 1 0 114 318 0 2

7 55 1 0 160 289 0 0

8 46 1 0 120 249 0 0

9 54 1 0 122 286 0 0

In [7]:

HD.drop(['slope'],axis=1,inplace=True)

In [8]:

HD.sample(3)

Out[8]:
Ag Se Chest_pain_ty resting_blood_press cholestor fasting_blood_su resting_cardiographi
e x pe ure al gar sults

13
54 1 1 108 309 0 1
4

43
60 0 2 102 318 0 1
2

99 76 0 2 140 197 0 2

In [9]:

print('cp values are', HD['Chest_pain_type'].unique())


print('thal values are', HD['thal'].unique())
print('restecg values are', HD['resting_cardiographic_results'].unique())

cp values are [0 1 2 3]
thal values are [3 2 1 0]
restecg values are [1 0 2]

In [10]:

HD.describe()

Out[10]:
Chest_pain_t resting_blood_pr cholestor fasting_blood_s resting_ca
Age Sex
ype essure al ugar _re

coun 1025.0000 1025.0000 1025.0000


1025.000000 1025.000000 1025.000000 1025.00000
t 00 00 0

mea 54.434146 0.695610 0.942439 131.608780 246.00000 0.149268 0.529756


Chest_pain_t resting_blood_pr cholestor fasting_blood_s resting_ca
Age Sex
ype essure al ugar _re

std 9.072290 0.460373 1.029641 17.514228 51.59251 0.356527 0.527878

min 29.000000 0.000000 0.000000 94.000000 126.00000 0.000000 0.000000

25% 48.000000 0.000000 0.000000 120.000000 211.00000 0.000000 0.000000

50% 56.000000 1.000000 1.000000 130.000000 240.00000 0.000000 1.000000

75% 61.000000 1.000000 2.000000 140.000000 275.00000 0.000000 1.000000

max 77.000000 1.000000 3.000000 200.000000 564.00000 1.000000 2.000000

In [11]:

HD.loc[100]

Out[11]:

Age 43.0
Sex 0.0
Chest_pain_type 2.0
resting_blood_pressure 122.0
cholestoral 213.0
fasting_blood_sugar 0.0
resting_cardiographic_results 1.0
maximum_heart_rate_achieved 165.0
exercise_induced_angina 0.0
depression_induced_by_excercise 0.2
ca 0.0
thal 2.0
target 1.0
Name: 100, dtype: float64

2. Exploratary Data Analysis¶

2.1 Null Value Checking¶


In [12]:

HD.isnull().sum() ## the dataset is clean,no null values are


present

Out[12]:

Age 0
Sex 0
Chest_pain_type 0
resting_blood_pressure 0
cholestoral 0
fasting_blood_sugar 0
resting_cardiographic_results 0
maximum_heart_rate_achieved 0
exercise_induced_angina 0
depression_induced_by_excercise 0
ca 0
thal 0
target 0
dtype: int64

In [13]:

fig=plt.figure(figsize=(7,6))
sns.heatmap(HD.isnull())

Out[13]:

<matplotlib.axes._subplots.AxesSubplot at 0x1f1036cfb00>
2.2 Univarient Analysis¶
In [14]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.Age,color='blue',histtype='bar',rwidth=0.7)
plt.xlabel('Age')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()

In [15]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.Sex,color='green',histtype='bar',rwidth=0.7)
plt.xlabel('Sex')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [16]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.Chest_pain_type,color='#EF7BB1',histtype='bar',rwidth=0.7)
plt.xlabel('Chest pain')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [17]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.resting_blood_pressure,color='red',histtype='bar',rwidth=0.7)
plt.xlabel('resting_blood_pressure')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [18]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.cholestoral,color='blue',histtype='bar',rwidth=0.7)
plt.xlabel('cholestoral')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [19]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.fasting_blood_sugar,color='green',histtype='bar',rwidth=0.7)
plt.xlabel('fasting_blood_sugar')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [20]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.resting_cardiographic_results,color='red',histtype='bar',rwidth=0.7)
plt.xlabel('resting_cardiographic_results')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [21]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.maximum_heart_rate_achieved,color='black',histtype='bar',rwidth=0.7)
plt.xlabel('maximum_heart_rate_achieved')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [22]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.exercise_induced_angina,color='blue',histtype='bar',rwidth=0.7)
plt.xlabel('exercise_induced_angina')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [23]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.depression_induced_by_excercise,color='brown',histtype='bar',rwidth=0.7)
plt.xlabel('depression_induced_by_excercise')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [24]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.thal,color='blue',histtype='bar',rwidth=0.7)
plt.xlabel('thal')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
In [25]:

fig=plt.figure(figsize=(6,6))
plt.hist(HD.target,color='black',histtype='bar',rwidth=0.7)
plt.xlabel('target')
plt.ylabel('Count Type')
plt.title('Histogram Plot')
plt.show()
2.2.1 Variable transformation¶
In [36]:

HD.loc[HD['Sex'] == 1, 'Sex'] = 'Male'


HD.loc[HD['Sex'] == 0, 'Sex'] = 'Female'

In [37]:

HD.loc[HD['Chest_pain_type'] == 0, 'Chest_pain_type'] = 'typical angina'


HD.loc[HD['Chest_pain_type'] == 1, 'Chest_pain_type'] = 'atypical angina'
HD.loc[HD['Chest_pain_type'] == 2, 'Chest_pain_type'] = 'non-anginal pain'
HD.loc[HD['Chest_pain_type'] == 3, 'Chest_pain_type'] = 'asymptomatic'

In [38]:

HD.loc[HD['target'] == 1, 'target'] = 'Normal'


HD.loc[HD['target'] == 0, 'target'] = 'Suffering'

In [69]:

HD.loc[HD['exercise_induced_angina'] == 1, 'exercise_induced_angina'] = 'Have'


HD.loc[HD['exercise_induced_angina'] == 0, 'exercise_induced_angina'] = 'Not_Have'

In [40]:
HD.rename(columns={'thal':'Thalassemia'},inplace=True)

In [41]:

HD.loc[HD['Thalassemia'] == 0, 'Thalassemia'] = 'Normal'


HD.loc[HD['Thalassemia'] == 1, 'Thalassemia'] = 'Normal'
HD.loc[HD['Thalassemia'] == 2, 'Thalassemia'] = 'Fixed_Defect'
HD.loc[HD['Thalassemia'] == 3, 'Thalassemia'] = 'Reversable_Defect'

In [42]:

HD.rename(columns={'ca':'Cardiac_Arrest'},inplace=True)

In [43]:

HD.loc[HD['fasting_blood_sugar'] == 1, 'fasting_blood_sugar'] = 'Have'


HD.loc[HD['fasting_blood_sugar'] == 0, 'fasting_blood_sugar'] = 'Not_Have'

In [44]:

HD.loc[HD['Cardiac_Arrest'] == 0, 'Cardiac_Arrest'] = '1st'


HD.loc[HD['Cardiac_Arrest'] == 1, 'Cardiac_Arrest'] = '2nd'
HD.loc[HD['Cardiac_Arrest'] == 2, 'Cardiac_Arrest'] = '3rd'
HD.loc[HD['Cardiac_Arrest'] == 3, 'Cardiac_Arrest'] = '4th'
HD.loc[HD['Cardiac_Arrest'] == 4, 'Cardiac_Arrest'] = '5th'

2.2.2 Boxplot¶
In [45]:

plt.figure(figsize = (10,7))
ax = sns.boxplot(x="Age", y="cholestoral",hue='Sex', data=HD)
plt.title('Age vs Cholestoral',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Cholestoral',fontsize=16)

Out[45]:

Text(0, 0.5, 'Cholestoral')


In [46]:

plt.figure(figsize = (9,7))
ax = sns.boxplot(x="Age", y="Chest_pain_type",hue='Sex', data=HD)
plt.title('Age vs Chest Pain Type',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Chest Pain Type',fontsize=16)

Out[46]:

Text(0, 0.5, 'Chest Pain Type')


In [47]:

plt.figure(figsize = (9,7))
ax = sns.boxplot(x="Age", y="maximum_heart_rate_achieved",hue='Sex', data=HD)
plt.title('Age vs Maximum Heart Rate Achieved',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Maximum Heart Rate Achieved',fontsize=16)

Out[47]:

Text(0, 0.5, 'Maximum Heart Rate Achieved')


In [48]:

plt.figure(figsize = (9,7))
ax = sns.boxplot(x="Age", y="resting_blood_pressure",hue='Sex', data=HD)
plt.title('Age vs Resting Blood Pressure',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Resting Blood Pressure',fontsize=16)

Out[48]:

Text(0, 0.5, 'Resting Blood Pressure')


In [49]:

plt.figure(figsize = (9,7))
ax = sns.boxplot(x="Age", y="fasting_blood_sugar",hue='Sex', data=HD)
plt.title('Age vs Fasting Blood Sugar',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Fasting Blood Suagr',fontsize=16)

Out[49]:

Text(0, 0.5, 'Fasting Blood Suagr')


In [50]:

plt.figure(figsize = (9,7))
ax = sns.boxplot(x="Age", y="resting_cardiographic_results",hue='Sex', data=HD)
plt.title('Age vs Resting Cardiographic Results',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Resting Cardiographic Results',fontsize=16)

Out[50]:

Text(0, 0.5, 'Resting Cardiographic Results')


In [51]:

plt.figure(figsize = (9,7))
ax = sns.boxplot(x="Age", y="exercise_induced_angina",hue='Sex', data=HD)
plt.title('Age vs Exercise Induced Angina',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Exercise Induced Angina',fontsize=16)

Out[51]:

Text(0, 0.5, 'Exercise Induced Angina')


In [52]:

plt.figure(figsize = (9,7))
ax = sns.boxplot(x="Age", y="depression_induced_by_excercise",hue='Sex', data=HD)
plt.title('Age vs Depression Induced By Exercise',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Depression Induced By Exercise',fontsize=16)

Out[52]:

Text(0, 0.5, 'Depression Induced By Exercise')


In [53]:

plt.figure(figsize = (10,7))
ax = sns.boxplot(x="Age", y="Cardiac_Arrest",hue='Sex', data=HD)
plt.title('Age vs Cardiac Arrest',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Cardiac Arrest',fontsize=16)

Out[53]:

Text(0, 0.5, 'Cardiac Arrest')


In [54]:

plt.figure(figsize = (9,7))
ax = sns.boxplot(x="Age", y="Thalassemia",hue='Sex', data=HD)
plt.title('Age vs Thalassemia',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Thalassemia',fontsize=16)

Out[54]:

Text(0, 0.5, 'Thalassemia')


In [55]:

plt.figure(figsize = (9,7))
ax = sns.boxplot(x="Age", y="target",hue='Sex', data=HD)
plt.title('Age vs Target',fontsize=18,color='blue')
plt.xlabel('Age',fontsize=16)
plt.ylabel('Target',fontsize=16)

Out[55]:

Text(0, 0.5, 'Target')


3. Analysis of the dataset based on
different Test Cases¶

3.1 AgeDataAnalysis¶
In [38]:

HD.Age.value_counts()
#data age show value counts for age least 10

Out[38]:

58 68
57 57
54 53
59 46
52 43
51 39
56 39
62 37
60 37
44 36
64 34
63 32
41 32
67 31
61 31
55 30
65 27
43 26
53 26
42 26
66 25
45 25
48 23
46 23
50 21
47 18
49 17
35 15
70 14
39 14
38 12
68 12
40 11
71 11
69 9
34 6
37 6
29 4
76 3
74 3
77 3
Name: Age, dtype: int64

In [39]:

fig=plt.figure(figsize=(9,7))
sns.barplot(x=HD.Age.value_counts().index,y=HD.Age.value_counts().values)
plt.xlabel('Age')
plt.ylabel('Age Counter')
plt.title('Age Analysis System')
plt.show()
In [40]:

HD[HD.Age==58].count()

Out[40]:

Age 68
Sex 68
Chest_pain_type 68
resting_blood_pressure 68
cholestoral 68
fasting_blood_sugar 68
resting_cardiographic_results 68
maximum_heart_rate_achieved 68
exercise_induced_angina 68
depression_induced_by_excercise 68
Thalassemia 68
target 68
dtype: int64
What is the minimum,maximum and
average age of peoplefor given dataset?¶
In [41]:

#firstly find min and max ages


minAge=min(HD.Age)
maxAge=max(HD.Age)
meanAge=HD.Age.mean()
print('Min Age :',minAge)
print('Max Age :',maxAge)
print('Mean Age :',meanAge)

Min Age : 29
Max Age : 77
Mean Age : 54.43414634146342

What is the count of young age ,middle


age,elderly age people for given dataset?¶
In [43]:

young_ages=HD[(HD.Age>=29)&(HD.Age<40)]
middle_ages=HD[(HD.Age>=40)&(HD.Age<55)]
elderly_ages=HD[(HD.Age>55)]
print('no of Young Ages people :',len(young_ages))
print('no of Middle Ages people :',len(middle_ages))
print('no of Elderly Ages people:',len(elderly_ages))

no of Young Ages people : 57


no of Middle Ages people : 419
no of Elderly Ages people: 519

In [44]:

sns.barplot(x=['young ages','middle ages','elderly


ages'],y=[len(young_ages),len(middle_ages),len(elderly_ages)])
plt.xlabel('Age Range')
plt.ylabel('Age Counts')
plt.title('Ages State in Dataset')
plt.show()
Who has achieved maximum heart rate
either Male or Female in
YoungAge,MiddleAge and ElderAge?¶
In [48]:

elderly_ages.groupby(elderly_ages['Sex'])['maximum_heart_rate_achieved'].agg('sum')

Out[48]:

Sex
Female 25211
Male 48730
Name: maximum_heart_rate_achieved, dtype: int64

In [51]:

sns.barplot(x=elderly_ages.groupby(elderly_ages['Sex'])
['maximum_heart_rate_achieved'].agg('sum').index,y=elderly_ages.groupby(elderly_ages[
'Sex'])['maximum_heart_rate_achieved'].agg('sum').values)
plt.title("Gender Group maximum_heart_rate_achieved Show Sum
Time",fontsize=18,color='blue')
plt.ylabel('Heart rate sum')
plt.show()
In [52]:

sns.barplot(x=young_ages.groupby(young_ages['Sex'])
['maximum_heart_rate_achieved'].agg('sum').index,y=young_ages.groupby(young_ages['Sex
'])['maximum_heart_rate_achieved'].agg('sum').values)
plt.title("Gender Group maximum_heart_rate_achieved Show Sum
Time",fontsize=18,color='blue')
plt.ylabel('Heart rate sum')
plt.show()

In [53]:

sns.barplot(x=middle_ages.groupby(middle_ages['Sex'])
['maximum_heart_rate_achieved'].agg('sum').index,y=middle_ages.groupby(middle_ages['S
ex'])['maximum_heart_rate_achieved'].agg('sum').values)
plt.title("Gender Group maximum_heart_rate_achieved Show Sum
Time",fontsize=18,color='blue')
plt.ylabel('Heart rate sum')
plt.show()

What is the percentage of


YoungAge,MiddleAge and ElderAge People?

In [55]:

plt.figure(figsize = (7,5))
plt.pie([len(young_ages),len(middle_ages),len(elderly_ages)],labels=['young
ages','middle ages','elderly
ages'],explode=(0.4,0.1,0.1),colors=['yellow','green','blue'],startangle=90,shadow=Tr
ue,autopct='%1.2f%%')
plt.title('Age States',color = 'blue',fontsize = 15)
plt.show()
What is range of Exercise Induced Angina,
Fasting Blood Sugar, Resting Cardiographic
Results, Chest Pain Type for greater than or
equal to the age input by the user?¶
In [113]:

HD.loc[HD['exercise_induced_angina'] == 'Have', 'exercise_induced_angina'] = 1


HD.loc[HD['exercise_induced_angina'] == 'Not_Have', 'exercise_induced_angina'] = 0

AGE=int(input('Enter age: '))


age1=HD[HD.Age>AGE]
plt.figure(figsize=(9,7))
sns.barplot(age1.Age,age1.exercise_induced_angina)
plt.title('Age vs Exercise Induced Angina',fontsize=18)
plt.xlabel('Age',fontsize=16)
plt.ylabel('Angina',fontsize=16)
plt.show()

HD.loc[HD['exercise_induced_angina'] == 1, 'exercise_induced_angina'] = 'Have'


HD.loc[HD['exercise_induced_angina'] == 2, 'exercise_induced_angina'] = 'Not_Have'

Enter age: 40
In [114]:

HD[HD.Age==49][['Age','exercise_induced_angina']]

Out[114]:
Ag exercise_induced_ang
e ina

53 49 0

81 49 0

21
49 0
5

22
49 0
6

23
49 0
4

23
49 0
6

40 49 0
Ag exercise_induced_ang
e ina

45
49 0
1

45
49 0
3

51
49 0
5

51
49 0
8

61
49 0
8

63
49 0
1

76
49 0
1

83
49 0
5

93
49 0
9

95
49 0
6

In [56]:

HD.loc[HD['fasting_blood_sugar'] == 'Have', 'fasting_blood_sugar'] = 1


HD.loc[HD['fasting_blood_sugar'] == 'Not_Have', 'fasting_blood_sugar'] = 0

AGE=int(input('Enter age: ')) # age as input


age1=HD[HD.Age>AGE]
plt.figure(figsize=(10,10))
sns.barplot(age1.Age,age1.fasting_blood_sugar)
plt.title('Age vs Fasting Blood Suagr',fontsize=18)
plt.xlabel('Age',fontsize=14)
plt.ylabel('Blood Sugar',fontsize=14)
plt.show()

HD.loc[HD['fasting_blood_sugar'] == 1, 'fasting_blood_sugar'] = 'Have'


HD.loc[HD['fasting_blood_sugar'] == 0, 'fasting_blood_sugar'] = 'Not_Have'

Enter age: 44
In [72]:

AGE=int(input('Enter age: ')) # age as input


age1=HD[HD.Age>AGE]
plt.figure(figsize=(10,10))
sns.barplot(age1.Age,age1.resting_cardiographic_results)
plt.xlabel('Age')
plt.ylabel('Cardio')
plt.show()

Enter age: 50
In [73]:

HD.loc[HD['Chest_pain_type'] == 'typical angina', 'Chest_pain_type'] = 0


HD.loc[HD['Chest_pain_type'] == 'atypical angina', 'Chest_pain_type'] = 1
HD.loc[HD['Chest_pain_type'] == 'non-anginal pain', 'Chest_pain_type'] = 2
HD.loc[HD['Chest_pain_type'] == 'asymptomatic', 'Chest_pain_type'] = 3

AGE=int(input('Enter age: ')) # age as input


age1=HD[HD.Age>AGE]
plt.figure(figsize=(10,10))
sns.barplot(age1.Age,age1.Chest_pain_type)
plt.xlabel('Age')
plt.ylabel('Chest Pain')
plt.show()

HD.loc[HD['Chest_pain_type'] == 0, 'Chest_pain_type'] = 'typical angina'


HD.loc[HD['Chest_pain_type'] == 1, 'Chest_pain_type'] = 'atypical angina'
HD.loc[HD['Chest_pain_type'] == 2, 'Chest_pain_type'] = 'non-anginal pain'
HD.loc[HD['Chest_pain_type'] == 3, 'Chest_pain_type'] = 'asymptomatic'

Enter age: 40

In [74]:

sns.set(style="darkgrid")
In [75]:

sns.relplot(x="Age",y="resting_blood_pressure",col='target', data=HD)

Out[75]:

<seaborn.axisgrid.FacetGrid at 0x1a6cda72f98>

In [76]:

sns.relplot(x="Age", y="resting_cardiographic_results", col='target', data=HD)

Out[76]:

<seaborn.axisgrid.FacetGrid at 0x1a6ceca8da0>
In [77]:

g = sns.relplot(x="Age", y="resting_blood_pressure",
kind="line",estimator=None,data=HD)
How much is the Resting Blood Pressure
and Chest Pain Type having different Age
and Sex people either Suffering or not
Suffering from Heart Disease?¶
In [78]:

fig=plt.figure(figsize=(15,15))
sns.relplot(x="Age",y="resting_blood_pressure",hue='Chest_pain_type', col='target',
row='Sex', data=HD)
plt.xlabel('Age',fontsize=16)
plt.xlabel('resting_blood_pressure',fontsize=16)
sns.set(style="white")

<Figure size 1080x1080 with 0 Axes>


How much is the Resting Blood Pressure
and Chest Pain Type of different Age and
Sex people either Suffering or not Suffering
from Heart Disease and having Fasting
Blood Sugar or Not?¶
In [116]:

fig=plt.figure(figsize=(50,50))
sns.relplot(x="Age",y="resting_blood_pressure",hue='Chest_pain_type',style='fasting_b
lood_sugar', row='Sex', col='target', data=HD)
sns.set(style="white")

<Figure size 3600x3600 with 0 Axes>


In [80]:

sns.relplot(x="Age",y="maximum_heart_rate_achieved", data=HD)

Out[80]:
<seaborn.axisgrid.FacetGrid at 0x1a6ce8cf470>

In [81]:

sns.relplot(x="Age",y="maximum_heart_rate_achieved",hue='exercise_induced_angina',
data=HD)

Out[81]:

<seaborn.axisgrid.FacetGrid at 0x1a6cec27630>
How much is the Maximum Heart Rate
Achieved ,Exercise Induced Angina and
Chest Pain Type of different Age and Sex
people either Suffering or not Suffering
from Heart Disease?¶
In [82]:

sns.relplot(x="Age",y="maximum_heart_rate_achieved",hue='exercise_induced_angina',sty
le='Chest_pain_type',col='target', row='Sex', data=HD)

Out[82]:

<seaborn.axisgrid.FacetGrid at 0x1a6cec27668>
In [83]:

sns.relplot(x="Age", y="fasting_blood_sugar",estimator=None, kind='line', row='Sex',


col='target',data=HD)
Out[83]:

<seaborn.axisgrid.FacetGrid at 0x1a6cec007b8>
3.2 Sex(Gender)DataAnalysis¶
What is the count and percentage of male
and female for given datset?¶
In [84]:

total_genders_count=len(HD.Sex)
male_count=len(HD[HD['Sex']=='Male'])
female_count=len(HD[HD['Sex']=='Female'])
print('Total Genders :',total_genders_count)
print('Male Count :',male_count)
print('Female Count :',female_count)

Total Genders : 1025


Male Count : 713
Female Count : 312

In [85]:

sns.countplot(HD.Sex,palette=("dark"))
plt.show()

In [87]:

x=male_count / (total_genders_count)*100
y=female_count / (total_genders_count)*100

In [88]:

#Percentage ratios
print("Male State: {:.2f}%".format((male_count / (total_genders_count)*100)))
print("Female State: {:.2f}%".format((female_count / (total_genders_count)*100)))
Male State: 69.56%
Female State: 30.44%

In [89]:

colors = ['yellow','blue']
plt.figure(figsize = (5,5))
plt.pie([x,y],labels=['Male','Female'],colors=colors,explode
=(0.3,0),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Gender States',color = 'blue',fontsize = 15)
plt.show()

Who have higher cholesterol,Male or


Female?¶
In [90]:

# scatter plot between sex and cholestrol


plt.scatter(HD['Sex'],HD['cholestoral'],color='green',marker = "*",s=50)
plt.xlabel("Sex")
plt.ylabel("Cholestrol")
plt.show()
What is the count and percentage of male
and female, who is suffering from hert
disease or not?¶
In [108]:

#Now, we are going to analyze both the sex and the heart health situation.
#Male State & target Male & Female
male_andtarget_off=len(HD[(HD.Sex=='Male')&(HD['target']=='Normal')])
male_andtarget_on=len(HD[(HD.Sex=='Male')&(HD['target']=='Suffering')])
sns.barplot(x=['Male Target On','Male Target
Off'],y=[male_andtarget_on,male_andtarget_off],palette='bright')
plt.xlabel('Male and Target State',fontsize=14)
plt.ylabel('Count',fontsize=14)
plt.title('State of the Gender',fontsize=18,color='blue')
plt.show()
In [110]:

colors = ['#9914AD','#0AFFFF']
plt.figure(figsize = (5,5))
plt.pie([male_andtarget_on,male_andtarget_off],labels=['Male suffering from Heart
Disease ','Male Normal'],colors=colors,explode
=(0.2,0),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Gender States',color = 'blue',fontsize = 15)
plt.show()

In [109]:
female_andtarget_off=len(HD[(HD.Sex=='Female')&(HD['target']=='Normal')])
female_andtarget_on=len(HD[(HD.Sex=='Female')&(HD['target']=='Suffering')])
sns.barplot(x=['Female Target On','Female Target
Off'],y=[female_andtarget_on,female_andtarget_off],palette='bright')
plt.xlabel('Female and Target State',fontsize=14)
plt.ylabel('Count',fontsize=14)
plt.title('State of the Gender',fontsize=18,color='blue')
plt.show()

In [106]:

colors = ['#E83BDC','#828282']
plt.figure(figsize = (5,5))
plt.pie([female_andtarget_on,female_andtarget_off],labels=['Female suffering from
Heart Disease ','Female Normal'],colors=colors,explode
=(0.3,0),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Gender States',color = 'blue',fontsize = 15)
plt.show()
3.3 AgeRangeAnalysis¶
In [117]:

HD['AgeRange']=0
youngAge_index=HD[(HD.Age>=29)&(HD.Age<40)].index
middleAge_index=HD[(HD.Age>=40)&(HD.Age<=55)].index
elderlyAge_index=HD[(HD.Age>55)].index

In [118]:

for index in elderlyAge_index:


HD.loc[index,'AgeRange']='Elder_Age'

for index in middleAge_index:


HD.loc[index,'AgeRange']='Middle_Age'

for index in youngAge_index:


HD.loc[index,'AgeRange']='Young_Age'

What is the percentage of people Suffering


and Not Suffering in YoungAge,MiddleAge
and ElderAge?¶
In [119]:

Suffering_youngAge=len(HD[(HD.target=='Suffering')&(HD.AgeRange=='Young_Age')])
Normal_youngAge=len(HD[(HD.target=='Normal')&(HD.AgeRange=='Young_Age')])
In [121]:

colors = ['royalblue','#64AAA9']
explode = [0,0.1]
plt.figure(figsize = (5,5))
plt.pie([Suffering_youngAge,Normal_youngAge], explode=explode, labels=['Suffering in
Young Age','Normal in Young Age'], colors=colors, autopct='%1.2f%%')
plt.title('Target vs Age Range Young Age ',color = 'blue',fontsize = 15)
plt.show()

In [123]:

suffering_middleAge=len(HD[(HD.target=='Suffering')&(HD.AgeRange=='Middle_Age')])
Normal_middleAge=len(HD[(HD.target=='Normal')&(HD.AgeRange=='Middle_Age')])

In [125]:

colors = ['blue','yellow']
explode = [0.1,0]
plt.figure(figsize = (5,5))
plt.pie([suffering_middleAge,Normal_middleAge], explode=explode, labels=['Suffering
in Middle Age','Normal in Middle age'], colors=colors, autopct='%1.2f%%')
plt.title('Target vs Age Range Middle Age',color = 'blue',fontsize = 15)
plt.show()
In [126]:

suffering_elderAge=len(HD[(HD.target=='Suffering')&(HD.AgeRange=='Elder_Age')])
Normal_elderAge=len(HD[(HD.target=='Normal')&(HD.AgeRange=='Elder_Age')])

In [128]:

colors = ['#287728','#9914AD']
explode = [0,0.1]
plt.figure(figsize = (5,5))
plt.pie([suffering_elderAge,Normal_elderAge], explode=explode, labels=['Suffering in
Elder Age','Normal in Elder Age'], colors=colors, autopct='%1.2f%%')
plt.title('Target vs Age Range Elderly Age ',color = 'blue',fontsize = 15)
plt.show()
In [129]:

fig=plt.figure(figsize=(8,6))
sns.countplot(HD.AgeRange,hue=HD.Sex,palette='bright')
plt.show()
3.4 ChestPainDataAnalysis¶
How many people(Male & Female) are
affected by Typical , Atypical , Non-anginal ,
Asymptomatic Angina?¶
In [131]:

HD.Chest_pain_type.value_counts()

Out[131]:

typical angina 497


non-anginal pain 284
atypical angina 167
asymptomatic 77
Name: Chest_pain_type, dtype: int64

In [132]:

plt.figure(figsize = (7,5))
sns.countplot(HD.Chest_pain_type,hue=HD.Sex,palette='dark')
plt.xlabel('Chest Type')
plt.ylabel('Count')
plt.title('Chest Type vs Count',fontsize=18,color='blue')
plt.show()
What is the count and percentage of Male
& Female are suffering from different types
of Angina and Heart Disease?¶
In [142]:

ctarget_off=len(HD[(HD.Chest_pain_type=='typical angina')&(HD.Sex=='Male')])
ctarget_on=len(HD[(HD.Chest_pain_type=='typical angina')&(HD.Sex=='Female')])
sns.barplot(x=['Male','Female'],y=[ctarget_off,ctarget_on],palette='dark')
plt.ylabel('Count')
plt.title('Typical Angina')
plt.show()

In [71]:

ctarget_off=len(HD[(HD.Chest_pain_type=='typical angina')&(HD.target=='Normal')])
ctarget_on=len(HD[(HD.Chest_pain_type=='typical angina')&(HD.target=='Suffering')])
sns.barplot(x=['ctarget_off','ctarget_on'],y=[ctarget_off,ctarget_on],palette='dark')
plt.ylabel('Count')
plt.title('Typical Angina')
plt.show()
In [143]:

ctarget_off=len(HD[(HD.Chest_pain_type=='typical angina')&(HD.Sex=='Male')])
ctarget_on=len(HD[(HD.Chest_pain_type=='typical angina')&(HD.Sex=='Female')])

colors = ['#C34BA7','orange']
plt.figure(figsize = (7,5))
plt.pie([(ctarget_off),(ctarget_on)],labels=['Male having Typical Angina','Female
having Typical Angina'],colors=colors,explode
=(0.1,0.2),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Target States',color = 'blue',fontsize = 18)
plt.show()
In [144]:

ctarget_off=len(HD[(HD.Chest_pain_type=='atypical angina')&(HD.Sex=='Male')])
ctarget_on=len(HD[(HD.Chest_pain_type=='atypical angina')&(HD.Sex=='Female')])
sns.barplot(x=['Male','Female'],y=[ctarget_off,ctarget_on],palette='bright')
plt.ylabel('Count')
plt.title('Atypical Angina')
plt.show()

In [72]:

ctarget_off=len(HD[(HD.Chest_pain_type=='atypical angina')&(HD.target=='Normal')])
ctarget_on=len(HD[(HD.Chest_pain_type=='atypical angina')&(HD.target=='Suffering')])
sns.barplot(x=['ctarget_off','ctarget_on'],y=[ctarget_off,ctarget_on],palette='bright
')
plt.ylabel('Count')
plt.title('Atypical Angina')
plt.show()
In [145]:

ctarget_off=len(HD[(HD.Chest_pain_type=='atypical angina')&(HD.Sex=='Male')])
ctarget_on=len(HD[(HD.Chest_pain_type=='atypical angina')&(HD.Sex=='Female')])

colors = ['blue','yellow']
plt.figure(figsize = (7,5))
plt.pie([(ctarget_off),(ctarget_on)],labels=['Male having Atypical Angina','Female
having Atypical Angina'],colors=colors,explode
=(0.1,0.2),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Target States',color = 'blue',fontsize = 18)
plt.show()
In [146]:

ctarget_off=len(HD[(HD.Chest_pain_type=='non-anginal pain')&(HD.Sex=='Male')])
ctarget_on=len(HD[(HD.Chest_pain_type=='non-anginal pain')&(HD.Sex=='Female')])
sns.barplot(x=['Male','Female'],y=[ctarget_off,ctarget_on],palette='colorblind')
plt.ylabel('Count')
plt.title('Non-anginal Pain')
plt.show()

In [73]:

ctarget_off=len(HD[(HD.Chest_pain_type=='non-anginal pain')&(HD.target=='Normal')])
ctarget_on=len(HD[(HD.Chest_pain_type=='non-anginal pain')&(HD.target=='Suffering')])
sns.barplot(x=['ctarget_off','ctarget_on'],y=[ctarget_off,ctarget_on],palette='colorb
lind')
plt.ylabel('Count')
plt.title('Non-anginal Pain')
plt.show()
In [147]:

ctarget_off=len(HD[(HD.Chest_pain_type=='non-anginal pain')&(HD.Sex=='Male')])
ctarget_on=len(HD[(HD.Chest_pain_type=='non-anginal pain')&(HD.Sex=='Female')])

colors = ['royalblue','#99ff99']
plt.figure(figsize = (7,5))
plt.pie([(ctarget_off),(ctarget_on)],labels=['Male having Non-anginal Pain','Female
having Non-anginal Pain'],colors=colors,explode
=(0.1,0.2),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Target States',color = 'blue',fontsize = 18)
plt.show()
In [148]:

ctarget_off=len(HD[(HD.Chest_pain_type=='asymptomatic')&(HD.Sex=='Male')])
ctarget_on=len(HD[(HD.Chest_pain_type=='asymptomatic')&(HD.Sex=='Female')])
sns.barplot(x=['Male','Female'],y=[ctarget_off,ctarget_on],palette='deep')
plt.ylabel('Count')
plt.title('Asymptomatic Pain')
plt.show()

In [74]:

ctarget_off=len(HD[(HD.Chest_pain_type=='asymptomatic')&(HD.target=='Normal')])
ctarget_on=len(HD[(HD.Chest_pain_type=='asymptomatic')&(HD.target=='Suffering')])
sns.barplot(x=['ctarget_off','ctarget_on'],y=[ctarget_off,ctarget_on],palette='deep')
plt.ylabel('Count')
plt.title('Asymptomatic Pain')
plt.show()
In [149]:

ctarget_off=len(HD[(HD.Chest_pain_type=='asymptomatic')&(HD.Sex=='Male')])
ctarget_on=len(HD[(HD.Chest_pain_type=='asymptomatic')&(HD.Sex=='Female')])

colors = ['#9756EB','#EF7BB1']
plt.figure(figsize = (7,5))
plt.pie([(ctarget_off),(ctarget_on)],labels=['Male having Asymptomatic
Angina','Female having Asymptomtic Angina'],colors=colors,explode
=(0.1,0.2),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Target States',color = 'blue',fontsize = 18)
plt.show()
In [150]:

ta=HD[HD.Chest_pain_type=='typical angina']
aa=HD[HD.Chest_pain_type =='atypical angina']
np=HD[HD.Chest_pain_type =='non-anginal pain']
a=HD[HD.Chest_pain_type =='asymptomatic']
colors = ['yellow','blue','royalblue','magenta']
plt.figure(figsize = (7,5))
plt.pie([len(ta),len(aa),len(np),len(a)],labels=['typical angina','atypical
angina','non-anginal pain','asymptomatic'],colors=colors,explode
=(0.1,0.2,0.1,0.1),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Target States',color = 'blue',fontsize = 18)
plt.show()

3.5
MaximumHeartRateAchievedDataA
nalysis¶
What is the count of Maximum Heart Rate
Achieved by People?¶
In [151]:

HD.maximum_heart_rate_achieved.value_counts() # Basic Count

Out[151]:
162 35
160 31
163 29
152 28
173 28
144 26
132 26
150 25
125 25
143 23
169 22
172 21
140 21
156 21
142 19
158 19
182 18
174 17
147 17
170 17
168 17
154 17
165 17
179 16
161 16
157 15
178 15
130 15
131 14
155 14
..
180 6
123 6
97 4
117 4
118 4
95 4
202 4
124 4
129 4
190 4
134 4
71 4
127 4
128 3
137 3
88 3
90 3
194 3
192 3
99 3
188 3
187 3
106 3
185 3
121 3
177 3
167 3
113 3
195 3
184 3
Name: maximum_heart_rate_achieved, Length: 91, dtype: int64

In [152]:

plt.figure(figsize=(20,10))
sns.barplot(x=HD.maximum_heart_rate_achieved.value_counts().index,y=HD.maximum_heart_
rate_achieved.value_counts().values)
plt.xlabel('Maximum heart rate',fontsize=15)
plt.ylabel('Count',fontsize=15)
plt.title('Maximum heart rate Counts',fontsize=18,color='blue')
plt.xticks(rotation=45)
plt.show()
In [153]:

HD[HD.maximum_heart_rate_achieved == 162].count()

Out[153]:

Age 35
Sex 35
Chest_pain_type 35
resting_blood_pressure 35
cholestoral 35
fasting_blood_sugar 35
resting_cardiographic_results 35
maximum_heart_rate_achieved 35
exercise_induced_angina 35
depression_induced_by_excercise 35
Thalassemia 35
target 35
AgeRange 35
dtype: int64

What is the count of Maximum Heart Rate


achieved for different ages people?¶
In [154]:

age_unique=sorted(HD.Age.unique())
age_maxHeartRate_values=HD.groupby('Age')
['maximum_heart_rate_achieved'].count().values
mean_maxHeartRate=[]
for i,age in enumerate(age_unique):

mean_maxHeartRate.append(sum(HD[HD['Age']==age].maximum_heart_rate_achieved)/age_maxH
eartRate_values[i])

In [155]:

plt.figure(figsize=(10,5))
sns.pointplot(x=age_unique,y=mean_maxHeartRate,color='black',alpha=0.8)
plt.xlabel('Age',fontsize = 15,color='blue')
plt.xticks(rotation=45)
plt.ylabel('maximum Heart Rate',fontsize = 15,color='blue')
plt.title('Age vs Maximum Heart Rate',fontsize = 15,color='blue')
plt.grid()
plt.show()
What is the average Maximum Heart rate
Achieved for different ages of people?¶
In [157]:

age_range_maxHeartRate=HD.groupby('Age')['maximum_heart_rate_achieved'].mean()

In [158]:

plt.figure(figsize=(10,10))
sns.barplot(x=age_range_maxHeartRate.index,y=age_range_maxHeartRate.values)
plt.xlabel('Age Range Values')
plt.ylabel('Maximum Heart Rate By Age Range')
plt.title('Illustration of the Maximum Heart Rate to the age range')
plt.show()
In [159]:

HD[HD.Age==29][["Age","maximum_heart_rate_achieved"]]

Out[159]:
Ag maximum_heart_rate_achi
e eved

60 29 202

64 29 202

11 29 202
Ag maximum_heart_rate_achi
e eved

66
29 202
8

In [162]:

chestPain_maxHeartRate=HD.groupby('Chest_pain_type')
['maximum_heart_rate_achieved'].mean()

In [163]:

chestPain_maxHeartRate

Out[163]:

Chest_pain_type
asymptomatic 157.129870
atypical angina 162.179641
non-anginal pain 155.116197
typical angina 140.052314
Name: maximum_heart_rate_achieved, dtype: float64

What is the Average Maximum Heart rate


Achieved for different Chest Pain Types of
people?¶
In [164]:

plt.figure(figsize=(8,8))
sns.barplot(x=chestPain_maxHeartRate.index,y=chestPain_maxHeartRate.values,palette='b
right')
plt.xlabel('Chest Pain Types',fontsize=14)
plt.ylabel('Maximum Heart Rate By Chest Pain Types',fontsize=14)
plt.title('Illustration of Maximum Heart Rate to degree of chest
pain',fontsize=16,color='red')
plt.show()
3.6 CardiacArrestDataAnalysis¶
What is the count of Cardiac Arrest affected
people?¶
In [58]:

HD.Cardiac_Arrest.value_counts()

Out[58]:

1st 578
2nd 226
3rd 134
4th 69
5th 18
Name: Cardiac_Arrest, dtype: int64

What is the percentage of people having


different types of Cardiac Arrest?¶
In [59]:

a=len(HD[(HD.Cardiac_Arrest=='1st')&(HD.target=='Normal')&(HD.Sex=='Male')])
b=len(HD[(HD.Cardiac_Arrest=='1st')&(HD.target=='Suffering')&(HD.Sex=='Male')])

c=len(HD[(HD.Cardiac_Arrest=='1st')&(HD.target=='Normal')&(HD.Sex=='Female')])
d=len(HD[(HD.Cardiac_Arrest=='1st')&(HD.target=='Suffering')&(HD.Sex=='Female')])

colors = ['yellow','blue','#E69FF4','magenta']
plt.figure(figsize = (8,6))
plt.pie([(a),(b),(c),
(d)],labels=['Male_Normal','Male_Suffering','Female_Normal','Female_Suffering'],color
s=colors,explode =(0.1,0.2,0.1,0.1),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Cardiac Arrest of 1st Type',color = 'blue',fontsize = 18)
plt.show()

In [60]:
a=len(HD[(HD.Cardiac_Arrest=='2nd')&(HD.target=='Normal')&(HD.Sex=='Male')])
b=len(HD[(HD.Cardiac_Arrest=='2nd')&(HD.target=='Suffering')&(HD.Sex=='Male')])

c=len(HD[(HD.Cardiac_Arrest=='2nd')&(HD.target=='Normal')&(HD.Sex=='Female')])
d=len(HD[(HD.Cardiac_Arrest=='2nd')&(HD.target=='Suffering')&(HD.Sex=='Female')])

colors = ['#0AFFEB','blue','#E69FF4','yellow']
plt.figure(figsize = (8,6))
plt.pie([(a),(b),(c),
(d)],labels=['Male_Normal','Male_Suffering','Female_Normal','Female_Suffering'],color
s=colors,explode =(0.1,0.2,0.1,0.1),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Cardiac Arrest of 2nd Type',color = 'blue',fontsize = 18)
plt.show()

In [61]:

a=len(HD[(HD.Cardiac_Arrest=='3rd')&(HD.target=='Normal')&(HD.Sex=='Male')])
b=len(HD[(HD.Cardiac_Arrest=='3rd')&(HD.target=='Suffering')&(HD.Sex=='Male')])

c=len(HD[(HD.Cardiac_Arrest=='3rd')&(HD.target=='Normal')&(HD.Sex=='Female')])
d=len(HD[(HD.Cardiac_Arrest=='3rd')&(HD.target=='Suffering')&(HD.Sex=='Female')])

colors = ['#0AFFEB','blue','#E69FF4','yellow']
plt.figure(figsize = (8,6))
plt.pie([(a),(b),(c),
(d)],labels=['Male_Normal','Male_Suffering','Female_Normal','Female_Suffering'],color
s=colors,explode =(0.1,0.2,0.1,0.1),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Cardiac Arrest of 3rd Type',color = 'blue',fontsize = 18)
plt.show()

In [62]:

x=len(HD[(HD.Cardiac_Arrest=='4th')&(HD.target=='Normal')&(HD.Sex=='Male')])
y=len(HD[(HD.Cardiac_Arrest=='4th')&(HD.target=='Suffering')&(HD.Sex=='Male')])

z=len(HD[(HD.Cardiac_Arrest=='4th')&(HD.target=='Normal')&(HD.Sex=='Female')])
w=len(HD[(HD.Cardiac_Arrest=='4th')&(HD.target=='Suffering')&(HD.Sex=='Female')])

colors = ['#0AFFEB','blue','#E69FF4','yellow']
plt.figure(figsize = (8,6))
plt.pie([x,y,z,w],labels=['Male_Normal','Male_Suffering','Female_Normal','Female_Suff
ering'],colors=colors,explode
=(0.1,0.2,0.1,0.1),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Cardiac Arrest of 4th Type',color = 'blue',fontsize = 18)
plt.show()
In [63]:

HD[(HD.Cardiac_Arrest=='4th') & (HD.Sex=='Female') & (HD.target=='Normal')].count()

Out[63]:

Age 0
Sex 0
Chest_pain_type 0
resting_blood_pressure 0
cholestoral 0
fasting_blood_sugar 0
resting_cardiographic_results 0
maximum_heart_rate_achieved 0
exercise_induced_angina 0
depression_induced_by_excercise 0
Cardiac_Arrest 0
Thalassemia 0
target 0
dtype: int64

In [64]:

ctarget_off=len(HD[(HD.Cardiac_Arrest=='5th')&(HD.target=='Normal')&(HD.Sex=='Male')]
)
ctarget_on=len(HD[(HD.Cardiac_Arrest=='5th')&(HD.target=='Suffering')&(HD.Sex=='Male'
)])
sns.barplot(x=['Male_Normal','Male_Suffering'],y=[ctarget_off,ctarget_on],palette='br
ight')
plt.xlabel('Cardiac Arrest 5th Type',fontsize=14)
plt.ylabel('Count',fontsize=14)
plt.title('Cardiac Arrest',fontsize=18,color='blue')
plt.show()

In [65]:

# There is no Female candidate in Cardiac Arrest 5th type

What is the count of people(Male & Female)


affected by Cardiac Arrest?¶
In [66]:

plt.figure(figsize = (7,5))
sns.countplot(HD.Cardiac_Arrest,hue=HD.Sex,palette='bright')
plt.xlabel('Cardiac Arrest')
plt.ylabel('Count')
plt.title('Cardiac Arrest vs Count',fontsize=18,color='blue')
plt.show()
How much people(Male & female) of
different ages are having Chest Pain and
affected by Cardiac Arrest?¶
In [70]:

sns.set(style="darkgrid")
fig=plt.figure(figsize=(20,20))
sns.relplot(x="Cardiac_Arrest",y="Age",hue='Chest_pain_type',
style='exercise_induced_angina', col='target', row='Sex', data=HD)
plt.figure(dpi=5000)

Out[70]:

<Figure size 30000x20000 with 0 Axes>

<Figure size 1440x1440 with 0 Axes>


<Figure size 30000x20000 with 0 Axes>
3.7 ThalassemiaDataAnalysis¶
What is the count of male and female
having different types of Thalassemia?¶
In [165]:

HD.Thalassemia.value_counts()

Out[165]:

Fixed_Defect 544
Reversable_Defect 410
Normal 71
Name: Thalassemia, dtype: int64

In [166]:

plt.figure(figsize=(8,6))
sns.countplot(HD.Thalassemia,hue=HD.Sex,palette='bright')
plt.title('Illustration of Thalassemia Types',fontsize=18,color='blue')
plt.xlabel('Thalassemia Type',fontsize=16)
plt.ylabel('Count',fontsize=16)
plt.show()
3.6 TargetDataAnalysis¶
What is the count of Male and Female
having and not having Heart Disease?¶
In [167]:

plt.figure(figsize=(8,6))
sns.countplot(HD.target,hue=HD.Sex,palette='bright')
plt.xlabel('Target')
plt.ylabel('Count')
plt.title('Target & Sex Counter',fontsize=16,color='green')
plt.show()
In [168]:

age_target_1=[]
age_target_0=[]
for age in HD.Age.unique():
age_target_1.append(len(HD[(HD['Age']==age)&(HD.target=='Normal')]))
age_target_0.append(len(HD[(HD['Age']==age)&(HD.target=='Suffering')]))

In [169]:

plt.figure(figsize=(8,6))
plt.scatter(x=HD.Age.unique(),y=age_target_1,color='blue',label='Normal')
plt.scatter(x=HD.Age.unique(),y=age_target_0,color='red',label='Suffering')
plt.legend(loc='upper right',frameon=True)
plt.xlabel('Age')
plt.ylabel('Count')
plt.title('Normal & Suffering State')
plt.show()
Illustration of Age vs Target¶
In [173]:

HD.loc[HD['target'] == 'Normal', 'target'] =1


HD.loc[HD['target'] == 'Suffering', 'target'] = 0
age_unique=sorted(HD.Age.unique())
age_target_values=HD.groupby('Age')['target'].count().values
target_values=[]
for i,age in enumerate(age_unique):
target_values.append(sum(HD[HD['Age']==age].target)/age_target_values[i])

In [174]:

plt.figure(figsize=(12,7))
sns.pointplot(x=age_unique,y=target_values,color='red',alpha=0.8)
plt.xlabel('Age',fontsize = 15,color='black')
plt.xticks(rotation=45)
plt.ylabel('Target Type',fontsize = 15,color='black')
plt.title('Age vs Target Illustration',fontsize = 18,color='blue')
plt.grid()
plt.show()
HD.loc[HD['target'] == 1, 'target'] = 'Normal'
HD.loc[HD['target'] == 0, 'target'] = 'Suffering'
What is the percentage of Male and Female
are suffering and not suffering from Heart
Disease?¶
In [175]:

#x=HD[HD.target=='Normal']
#y=HD[HD.target=='Suffering']

x=len(HD[(HD.target=='Normal')&(HD.Sex=='Male')])
y=len(HD[(HD.target=='Suffering')&(HD.Sex=='Female')])
z=len(HD[(HD.target=='Normal')&(HD.Sex=='Female')])
w=len(HD[(HD.target=='Suffering')&(HD.Sex=='Male')])

colors = ['yellow','blue','red','orange']
plt.figure(figsize = (7,5))
plt.pie([(x),(y),(z),(w)],labels=['Normal Male','Suffering Female','Normal
Female','Suffering Male'],colors=colors,explode
=(0.2,0.1,0.1,0.1),startangle=90,shadow=True,autopct='%1.2f%%')
plt.title('Target States',color = 'blue',fontsize = 18)
plt.show()

In [ ]:

Das könnte Ihnen auch gefallen