首页 > 其他分享 >对美国心脏病数据集进行大数据分析

对美国心脏病数据集进行大数据分析

时间:2022-12-23 02:55:06浏览次数:36  
标签:数据分析 heart HeartDisease plt df 心脏病 sns ax 数据

一、选题背景

       本数据来源于美国2020 年 40 万成年人与其健康状况相关的 CDC 年度调查数据。心脏病是美国大多数种族(非洲裔美国人、美洲印第安人和阿拉斯加原住民以及白人)的主要死因之一。在美国心脏病的负担日渐加重,这不仅仅是美国重大的公共卫生问题也是我们国家重大的公共卫生问题。我们可以对美国患上心脏病的数据集进行数据分析,从中找到诱发心脏病的因素,而我们可以在这吸取教训经验以达到预防自己国家的人民心脏病发生的概率。

二、大数据分析设计方案

1.本数据集的数据内容与数据特征分析

本数据集共319795条数据,其中心脏病患者人数为27373,未患病人数为292422。

各字段详细信息如下:                   

字段名称

字段类型

字段说明

HeartDisease

int

心脏病(1=是,0=否)

BMI

float

身体质量指数

Smoking

int

吸烟(1=是,0=否)

AlcoholDrinking

int

酗酒(1=是,0=否)

HeartDisease

int

心脏病(1=是,0=否)

Stroke

int

中风(1=是,0=否)  

DiffWalking

int

不能行走(1=是,0=否)

Sex

object

性别(Female:女,male:男)

AgeCategory

int

年龄

Diabetic

int

糖尿病(1=是,0=否)

PhysicalActivity

int

身体活动(1=是,0=否)

GenHealth

object

综合健康(Poor:差,Fair:正常,Excellent:良好,Good:好,Very good:非常好)

SleepTime

int

睡眠时间

Asthma

int

气喘(1=是,0=否)

KidneyDisease

int

肾脏疾病(1=是,0=否)

SkinCancer

int

皮肤癌(1=是,0=否)

2.数据分析的课程设计方案概述

    (1)先对数据集的数据进行数据清洗,再对所需要的字段或者数据进行处理,并计算数据集中各种数据与心脏病的相关性。

    (2)对数据集每一种数据与’HeartDisease'的关系进行python可视化处理,判断其是否为影响心脏病患病的因素。

三、数据分析步骤

1.数据源

本次课程设计的数据集来源于DataCastle(中国数据科学竞赛平台)。

附上网址:https://www.datacastle.cn/

2.数据清洗

导入数据集

1 import numpy as np
2 import pandas as pd
3 import scipy.stats as stats
4 import seaborn as sns6 heart_df = pd.read_csv('D:/dataset/heart_2020.csv')
7 heart_df.head(11)

结果

删除无效列

1 heart_df.drop(labels = ['PhysicalHealth','MentalHealth','Race'], axis = 1, inplace = True)
2 heart_df.head(11)

结果

删除重复列

1 heart_df = heart_df.drop_duplicates()
2 heart_df.head()

结果

查看是否有缺失值

heart_df.isnull().sum()

查看结果

 3.大数据分析过程

显示数据集长度

1 heart_df.shape

查看结果

使用describe()方法查看该数据集中的各数值字段的统计信息

1 heart_df.describe()

查看结果

绘制热图,整体看一下相关性

1 plt.figure(figsize = (12,10))
2 sns.heatmap(heart_df.corr(), annot =True) 
3 plt.show()

查看结果

从上图可以看到是否患病与AlcoholDrinking(酗酒)、PhysicalActivity(身体活动)呈负相关。与BMI、Smoking、Stroke等呈正相关,其中HeartDisease(心脏病)与AgeCategory(年龄)呈正相关最高,为0.24,其次是Stroke(中风)为0.19、DiffWalking(不能行走)为0.18和Diabetic(糖尿病)为0.15、KidneyDisease(肾脏疾病)为0.14 等,最低正相关为SleepTime(睡眠时间)为0.013。

热图看可能还不太直观,再用柱状图和饼图直观的总览下各数据中不同项在其中的占比为多少

 1 import numpy as np
 2 import pandas as pd
 3 import matplotlib.pyplot as plt
 4 import seaborn as sns
 5 import warnings
 6 fig,ax=plt.subplots(1,2,figsize=(15,3))
 7 sns.countplot(x='HeartDisease',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
 8 ax[0].set_xlabel("心脏病")
 9 ax[0].set_ylabel('是否心脏病人数')
10 heart_df.HeartDisease.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
11 ax[1].set_title("心脏病")
12 # 心脏病占比
13 fig,ax=plt.subplots(1,2,figsize=(15,3))
14 sns.countplot(x='Smoking',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
15 ax[0].set_xlabel("抽烟")
16 ax[0].set_ylabel('是否抽烟人数')
17 heart_df.Smoking.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
18 ax[1].set_title("抽烟")
19 # 抽烟占比
20 fig,ax=plt.subplots(1,2,figsize=(15,3))
21 sns.countplot(x='AlcoholDrinking',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
22 ax[0].set_xlabel("酗酒")
23 ax[0].set_ylabel('是否酗酒人数')
24 heart_df.AlcoholDrinking.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
25 ax[1].set_title("酗酒")
26 # 酗酒占比
27 fig,ax=plt.subplots(1,2,figsize=(15,3))
28 sns.countplot(x='DiffWalking',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
29 ax[0].set_xlabel("不能行走")
30 ax[0].set_ylabel('是否能行走人数')
31 heart_df.DiffWalking.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
32 ax[1].set_title("不能行走")
33 # 不能行走占比
34 fig,ax=plt.subplots(1,2,figsize=(15,3))
35 sns.countplot(x='Stroke',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
36 ax[0].set_xlabel("中风")
37 ax[0].set_ylabel('是否中风人数')
38 heart_df.Stroke.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
39 ax[1].set_title("中风")
40 # 中风占比
41 fig,ax=plt.subplots(1,2,figsize=(15,3))
42 sns.countplot(x='Sex',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
43 ax[0].set_xlabel("性别")
44 ax[0].set_ylabel('人数')
45 heart_df.Sex.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
46 ax[1].set_title("性别")
47 # 性别占比
48 fig,ax=plt.subplots(1,2,figsize=(15,3))
49 sns.countplot(x='Diabetic',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
50 ax[0].set_xlabel("糖尿病")
51 ax[0].set_ylabel('是否糖尿病人数')
52 heart_df.Diabetic.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
53 ax[1].set_title("糖尿病")
54 # 糖尿病占比
55 fig,ax=plt.subplots(1,2,figsize=(15,3))
56 sns.countplot(x='PhysicalActivity',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
57 ax[0].set_xlabel("身体活动")
58 ax[0].set_ylabel('有无身体活动人数')
59 heart_df.PhysicalActivity.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
60 ax[1].set_title("身体活动")
61 # 身体活动占比
62 fig,ax=plt.subplots(1,2,figsize=(15,3))
63 sns.countplot(x='GenHealth',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
64 ax[0].set_xlabel("综合健康")
65 ax[0].set_ylabel('人数')
66 heart_df.GenHealth.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01,0.01,0.01,0.01],shadow=True, cmap='OrRd')
67 ax[1].set_title("综合健康")
68 # 综合健康占比
69 fig,ax=plt.subplots(1,2,figsize=(15,3))
70 sns.countplot(x='Asthma',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
71 ax[0].set_xlabel("气喘")
72 ax[0].set_ylabel('是否气喘人数')
73 heart_df.Asthma.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
74 ax[1].set_title("气喘")
75 # 气喘占比
76 fig,ax=plt.subplots(1,2,figsize=(15,3))
77 sns.countplot(x='SkinCancer',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
78 ax[0].set_xlabel("皮肤癌")
79 ax[0].set_ylabel('是否有皮肤癌人数')
80 heart_df.SkinCancer.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
81 ax[1].set_title("皮肤癌")
82 # 皮肤癌占比
83 fig,ax=plt.subplots(1,2,figsize=(15,3))
84 sns.countplot(x='KidneyDisease',data=heart_df,hue='HeartDisease',palette='Set3',ax=ax[0])
85 ax[0].set_xlabel("肾脏疾病")
86 ax[0].set_ylabel('是否患有肾脏疾病人数')
87 heart_df.KidneyDisease.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01,0.01],shadow=True, cmap='OrRd')
88 ax[1].set_title("肾脏疾病")# 肾脏疾病占比
89 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
90 plt.rcParams['axes.unicode_minus'] = False
91 warnings.filterwarnings('ignore')
92 plt.show()

查看结果

 心脏病患病比例

 抽烟比例

 酗酒比例

 

 不能行走比例

 

 中风比例

男女比例

 糖尿病比例

 

 身体活动比例

 

 综合健康比例

 

 气喘比例

 

 肾脏疾病比例

 皮肤癌比例 

从上面各占比图可以看出美国人中患病的人都没有酗酒行为,由此数据集可以得出酗酒不是引发心脏病的原因。我们可以再单个或者多个字段一起研究与其心脏病的关系

查看各个字段的数据分布情况

1 heart_df.hist(figsize=(20,16))
2 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
3 plt.rcParams['axes.unicode_minus'] = False
4 plt.show()

查看结果

从上图可以看出这个数据集年龄大部分是在50-80岁,BMI大部分是在20-40,SleepTime大部分是5-10

我们对患病、未患病,男性、女性占比进行了一个分析,先用数值直观的感受下:

 1 No_HeartDisease = len(heart_df[heart_df.HeartDisease == 0])
 2 Yes_HeartDisease = len(heart_df[heart_df.HeartDisease == 1])
 3 countFemale = len(heart_df[heart_df.Sex == 'Female'])
 4 countMale = len(heart_df[heart_df.Sex == 'Male'])
 5 print(f'没患病人数:{No_HeartDisease }',end=' ,')
 6 print("没患病比率: {:.2f}%".format((No_HeartDisease / (len(heart_df.HeartDisease))*100)))
 7 print(f'有患病人数:{Yes_HeartDisease }',end=' ,')
 8 print("有患病比率: {:.2f}%".format((Yes_HeartDisease / (len(heart_df.HeartDisease))*100)))
 9 print(f'女性人数:{countFemale }',end=' ,')
10 print("女性比例: {:.2f}%".format((countFemale / (len(heart_df.Sex))*100)))
11 print(f'男性人数:{countMale }',end=' ,')
12 print("男性比例: {:.2f}%".format((countMale   / (len(heart_df.Sex))*100)))

查看结果

得出患心脏病的人数为27107,未患心脏病人数为240355,女性人数为141183,男性人数为126279。为了看得更加直观,用柱状图显示

1 fig, ax = plt.subplots(1,2)  
2 fig.set_size_inches(w=15,h=5)  
3 sns.countplot(x="Sex", data=heart_df,ax=ax[0])
4 plt.xlabel("性别 (‘Female’= 男, ‘Male’= 女)")
5 sns.countplot(x="HeartDisease", data=heart_df,ax=ax[1])
6 plt.xlabel("是否患病 (0 = 未患病, 1 = 患病)")
7 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
8 plt.rcParams['axes.unicode_minus'] = False
9 plt.show()

查看结果

 用柱状图来观察性别是否会影响心脏病的患病几率

1 pd.crosstab(heart_df.Sex,heart_df.HeartDisease).plot(kind="bar",figsize=(15,6),color=['dodgerblue','crimson' ])
2 plt.title('男女患病占比')
3 plt.xlabel('性别 (’Female’= 男, ‘Male’= 女)')
4 plt.xticks(rotation=0)
5 plt.legend(["没有患病", "患有心脏病"])
6 plt.ylabel('人数')
7 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
8 plt.rcParams['axes.unicode_minus'] = False
9 plt.show()

查看结果

 从图中可以看到美国中男、女数量和患病、没有患病的数量相差不是很明显,所以可以看出性别并不是影响心血管疾病患病几率的因素。

之前热图可以看出AgeCategory(年龄)和HeartDisease(心脏病)正相关最高。

 先用柱状图来观察年龄是否会影响心脏病的患病几率

1 pd.crosstab(heart_df.AgeCategory,heart_df.HeartDisease).plot(kind="bar",figsize=(25,8))
2 plt.title('患病变化随年龄分布图')
3 plt.xlabel('年龄')
4 plt.ylabel('占比')
5 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
6 plt.rcParams['axes.unicode_minus'] = False
7 plt.show()

查看结果

从图中可以看到年龄越大心脏病患病的比例会相对高一些,所以可以得出结论年龄是引发心脏病的原因之一。

再用箱型图查看年龄、性别、患病三者之间有无关联

1 fig,ax=plt.subplots(figsize=(16,6))
2 plt.subplot(121)
3 age_sex_HeartDisease=sns.boxenplot(x='Sex',y='AgeCategory',hue='HeartDisease',data=heart_df,palette='YlGn')
4 age_sex_HeartDisease.set_title("年龄-性别-患病")
5 plt.ylabel("年龄")

查看结果

通过箱型图可以看到男女性患病大部分都在45-80岁之间,由此可以验证前面的结论心脏病与性别无关,与年龄息息相关。

一些年长的人喜欢抽烟,所以我们用箱型图查看年龄、抽烟、患病之间的关系

1 fig,ax=plt.subplots(figsize=(16,6))
2 plt.subplot(121)
3 age_Smoking_HeartDisease=sns.boxenplot(x='Smoking',y='AgeCategory',hue='HeartDisease',data=heart_df,palette='YlGn')
4 age_Smoking_HeartDisease.set_title("年龄-抽烟-患病")
5 plt.ylabel("年龄")

显示结果

这里可以看出抽烟与性别类似,并不是引起心脏病的诱因。

用折线和提琴图查看年龄、患病、BMI三者之间已经心脏病与BMI的关系

 1 import warnings
 2 plt.figure(figsize=(15, 7))
 3 plt.subplot(2,2,1)
 4 sns.pointplot(x='AgeCategory',y='BMI',data=heart_df,color='Lime',hue='HeartDisease')
 5 plt.ylabel("身体质量指数")
 6 plt.subplot(2, 2, 2)
 7 sns.violinplot(x=heart_df.HeartDisease, y=heart_df.BMI, data=heart_df)
 8 plt.ylabel("身体质量指数")
 9 plt.subplot(2, 2, 3)
10 sns.distplot(heart_df.BMI)
11 plt.subplot(2, 2, 4)
12 sns.regplot(x='BMI', y='HeartDisease', data=heart_df)
13 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
14 plt.rcParams['axes.unicode_minus'] = False
15 warnings.filterwarnings('ignore')
16 plt.show()

显示结果

上图可以看到患病的人BMI都偏高,可以得出结论BMI也是影响心脏病发生的原因之一。如果年龄大再加上BMI(身体质量指数)也大,那患上心脏病的概率更大。意思就是身体较肥胖的老年人更容易患上心脏病。

绘制年龄、睡眠、患病三者之间的关系图

 1 import warnings
 2 plt.figure(figsize=(15,7))
 3 plt.subplot(2,2,1)
 4 sns.pointplot(x='AgeCategory',y='SleepTime',data=heart_df,color='Lime',hue='HeartDisease')
 5 plt.ylabel("睡眠时间")
 6 plt.subplot(2,2,2)
 7 sns.violinplot(x=heart_df.HeartDisease,y=heart_df.SleepTime,data=heart_df)
 8 plt.ylabel("睡眠时间")
 9 plt.subplot(2,2,3)
10 sns.distplot(heart_df.SleepTime)
11 plt.subplot(2,2,4)
12 sns.regplot(x='SleepTime',y='HeartDisease',data=heart_df)
13 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
14 plt.rcParams['axes.unicode_minus'] = False
15 warnings.filterwarnings('ignore')
16 plt.show()

显示结果

 从图中可以看出无论是年纪大还是年纪小患上心脏的人睡眠时间并不比没患心脏病人睡眠的时间短,可以得出结论心脏病与睡眠时间无关。

之前通过热图获取到Stroke(中风)与HeartDisease(心脏病)的关系相关性可以排到第二,为0.19。所以绘制Stroke(中风)与HeartDisease(心脏病)的关系图

 1 plt.figure(figsize=(15,7))
 2 plt.subplot(2,2,1)
 3 sns.regplot(x='Stroke',y='HeartDisease',data=heart_df)
 4 plt.subplot(2,2,2)
 5 sns.countplot(y=heart_df.Stroke,hue=heart_df.HeartDisease)
 6 plt.subplot(2,2,3)
 7 sns.boxplot(x=heart_df.Stroke,y=heart_df.AgeCategory,data=heart_df)
 8 plt.subplot(2,2,4)
 9 sns.boxplot(x=heart_df.Stroke,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)
10 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
11 plt.rcParams['axes.unicode_minus'] = False
12 warnings.filterwarnings('ignore')
13 plt.show()

显示结果

从上图可以看出,中风也是引发心脏病的因素之一,其次年龄较大得了中风的话更易患上心脏病

DiffWalking(不能行走)的与HeartDisease(心脏病)的相关性仅次于Stroke(中风)为0.18,所以绘制DiffWalking(不能行走)与HeartDisease(心脏病)的关系图

 1 plt.figure(figsize=(15,7))
 2 plt.subplot(2,2,1)
 3 sns.regplot(x='DiffWalking',y='HeartDisease',data=heart_df)
 4 plt.subplot(2,2,2)
 5 sns.countplot(y=heart_df.DiffWalking,hue=heart_df.HeartDisease)
 6 plt.subplot(2,2,3)
 7 sns.boxplot(x=heart_df.DiffWalking,y=heart_df.AgeCategory,data=heart_df)
 8 plt.subplot(2,2,4)
 9 sns.boxplot(x=heart_df.DiffWalking,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)
10 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
11 plt.rcParams['axes.unicode_minus'] = False
12 warnings.filterwarnings('ignore')
13 plt.show()

显示结果

 把上图与中风图相对比发现与之相似,这里我们可以得出结论,不能行走也是引发心脏病的因素之一,其次年龄较大不能行走的话也易患上心脏病。

与心脏病正相关排名前三的还有一个Diabetic(糖尿病),我们也绘图看一下

1 plt.figure(figsize=(15,7))
2 plt.subplot(2,2,1)
3 sns.regplot(x='Diabetic',y='HeartDisease',data=heart_df)
4 plt.subplot(2,2,2)
5 sns.countplot(y=heart_df.Diabetic,hue=heart_df.HeartDisease)
6 plt.subplot(2,2,3)
7 sns.boxplot(x=heart_df.Diabetic,y=heart_df.AgeCategory,data=heart_df)
8 plt.subplot(2,2,4)
9 sns.boxplot(x=heart_df.Diabetic,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)

查看结果

 由上图可以看出糖尿病也是心脏病的诱因之一,年龄患上糖尿病更应注意。

 Diabetic(糖尿病)后其次是KidneyDisease(肾脏疾病),绘图查看肾脏疾病和心脏病的关系

 1 plt.figure(figsize=(15,7))
 2 plt.subplot(2,2,1)
 3 sns.regplot(x='KidneyDisease',y='HeartDisease',data=heart_df)
 4 plt.subplot(2,2,2)
 5 sns.countplot(y=heart_df.KidneyDisease,hue=heart_df.HeartDisease)
 6 plt.subplot(2,2,3)
 7 sns.boxplot(x=heart_df.KidneyDisease,y=heart_df.AgeCategory,data=heart_df)
 8 plt.subplot(2,2,4)
 9 sns.boxplot(x=heart_df.KidneyDisease,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)
10 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
11 plt.rcParams['axes.unicode_minus'] = False
12 warnings.filterwarnings('ignore')
13 plt.show()

查看结果

此图我们可以看出肾脏疾病也是心脏病的诱因之一,年龄越大加上有肾脏疾病更易患上心脏病

气喘和心脏病的关系

1 plt.figure(figsize=(15,7))
2 plt.subplot(2,2,1)
3 sns.regplot(x='Asthma',y='HeartDisease',data=heart_df)
4 plt.subplot(2,2,2)
5 sns.countplot(y=heart_df.Asthma,hue=heart_df.HeartDisease)
6 plt.subplot(2,2,3)
7 sns.boxplot(x=heart_df.Asthma,y=heart_df.AgeCategory,data=heart_df)
8 plt.subplot(2,2,4)
9 sns.boxplot(x=heart_df.Asthma,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)

显示结果

上图可以看出气喘和心脏病关系不大

皮肤癌和心脏病的关系

1 plt.figure(figsize=(15,7))
2 plt.subplot(2,2,1)
3 sns.regplot(x='SkinCancer',y='HeartDisease',data=heart_df)
4 plt.subplot(2,2,2)
5 sns.countplot(y=heart_df.SkinCancer,hue=heart_df.HeartDisease)
6 plt.subplot(2,2,3)
7 sns.boxplot(x=heart_df.SkinCancer,y=heart_df.AgeCategory,data=heart_df)
8 plt.subplot(2,2,4)
9 sns.boxplot(x=heart_df.SkinCancer,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)

查看结果

上图可以看出皮肤癌也是心脏病的诱因之一,在加上年龄大更容易患上心脏病。

身体活动和心脏病的关系

1 plt.figure(figsize=(15,7))
2 plt.subplot(2,2,1)
3 sns.regplot(x='PhysicalActivity',y='HeartDisease',data=heart_df)
4 plt.subplot(2,2,2)
5 sns.countplot(y=heart_df.PhysicalActivity,hue=heart_df.HeartDisease)
6 plt.subplot(2,2,3)
7 sns.boxplot(x=heart_df.PhysicalActivity,y=heart_df.AgeCategory,data=heart_df)
8 plt.subplot(2,2,4)
9 sns.boxplot(x=heart_df.PhysicalActivity,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)

查看结果

上图我们可以看出身体活动和心脏病无关

再将综合健康同年龄、中风、不能行走、糖尿病、身体健康指数、肾脏疾病、皮肤癌等结合起来看

1 sns.pairplot(heart_df[heart_df.GenHealth == 'poor'], vars=['AgeCategory', 'Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue='HeartDisease', palette="Accent", corner=True)
2 sns.pairplot(heart_df[heart_df.GenHealth == 'Fair'], vars=['AgeCategory', 'Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue='HeartDisease', palette="Accent", corner=True)
3 sns.pairplot(heart_df[heart_df.GenHealth == 'Excellent'], vars=['AgeCategory', 'Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue='HeartDisease', palette="Accent", corner=True)
4 sns.pairplot(heart_df[heart_df.GenHealth == 'Good'], vars = ['AgeCategory','Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue = 'HeartDisease', palette="Accent", corner=True)
5 sns.pairplot(heart_df[heart_df.GenHealth == 'Very good'], vars = ['AgeCategory','Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue = 'HeartDisease', palette="Accent", corner=True)
6 plt.show

查看结果

综合健康-差

 综合健康-正常

 综合健康-良好

 综合健康-好

 

 综合健康-非常好

从上面几张图中我们能更清晰认识心脏病疾病在老年人,肥胖,中风人群、不能行走、糖尿病、肾脏疾病、皮肤癌等人群中占比更大。更易患上心脏病

4.完整代码:

  1 import matplotlib.pyplot as plt
  2 import seaborn as sns
  3 import numpy as np
  4 import pandas as pd
  5 import warnings
  6 heart_df = pd.read_csv('D:/dataset/heart_2020.csv')
  7 heart_df.head(11)
  8 # 数据清洗
  9 heart_df.drop(labels=['PhysicalHealth', 'MentalHealth', 'Race'], axis=1, inplace=True)
 10 heart_df.head(11)
 11 heart_df = heart_df.drop_duplicates()
 12 heart_df.head()
 13 # 查看有无缺失值
 14 heart_df.isnull().sum()
 15 heart_df.shape
 16 heart_df.describe()
 17 plt.figure(figsize=(12,10))
 18 sns.heatmap(heart_df.corr(), annot=True)
 19 plt.show()
 20 fig, ax = plt.subplots(1,2,figsize=(15, 3))
 21 sns.countplot(x='HeartDisease', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 22 ax[0].set_xlabel("心脏病")
 23 ax[0].set_ylabel('是否心脏病人数')
 24 heart_df.HeartDisease.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01, 0.01], shadow=True, cmap='OrRd')
 25 ax[1].set_title("心脏病")
 26 # 心脏病占比
 27 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 28 sns.countplot(x='Smoking', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 29 ax[0].set_xlabel("抽烟")
 30 ax[0].set_ylabel('是否抽烟人数')
 31 heart_df.Smoking.value_counts().plot.pie(ax=ax[1], utopct='%1.1f%%', explode=[0.01, 0.01],shadow=True, cmap='OrRd')
 32 ax[1].set_title("抽烟")
 33 # 抽烟占比
 34 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 35 sns.countplot(x='AlcoholDrinking',data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 36 ax[0].set_xlabel("酗酒")
 37 ax[0].set_ylabel('是否酗酒人数')
 38 heart_df.AlcoholDrinking.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01,0.01],shadow=True, cmap='OrRd')
 39 ax[1].set_title("酗酒")
 40 # 酗酒占比
 41 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 42 sns.countplot(x='DiffWalking', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 43 ax[0].set_xlabel("不能行走")
 44 ax[0].set_ylabel('是否能行走人数')
 45 heart_df.DiffWalking.value_counts().plot.pie(ax=ax[1],autopct='%1.1f%%',explode=[0.01, 0.01],shadow=True, cmap='OrRd')
 46 ax[1].set_title("不能行走")
 47 # 不能行走占比
 48 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 49 sns.countplot(x='Stroke', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 50 ax[0].set_xlabel("中风")
 51 ax[0].set_ylabel('是否中风人数')
 52 heart_df.Stroke.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01, 0.01],shadow=True, cmap='OrRd')
 53 ax[1].set_title("中风")
 54 # 中风占比
 55 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 56 sns.countplot(x='Sex', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 57 ax[0].set_xlabel("性别")
 58 ax[0].set_ylabel('人数')
 59 heart_df.Sex.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01, 0.01],shadow=True, cmap='OrRd')
 60 ax[1].set_title("性别")
 61 # 性别占比
 62 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 63 sns.countplot(x='Diabetic',data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 64 ax[0].set_xlabel("糖尿病")
 65 ax[0].set_ylabel('是否糖尿病人数')
 66 heart_df.Diabetic.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01, 0.01, 0.01, 0.01], shadow=True, cmap='OrRd')
 67 ax[1].set_title("糖尿病")
 68 # 糖尿病占比
 69 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 70 sns.countplot(x='PhysicalActivity', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 71 ax[0].set_xlabel("身体活动")
 72 ax[0].set_ylabel('有无身体活动人数')
 73 heart_df.PhysicalActivity.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01, 0.01], shadow=True, cmap='OrRd')
 74 ax[1].set_title("身体活动")
 75 # 身体活动占比
 76 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 77 sns.countplot(x='GenHealth', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 78 ax[0].set_xlabel("综合健康")
 79 ax[0].set_ylabel('人数')
 80 heart_df.GenHealth.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01,0.01,0.01,0.01,0.01],shadow=True, cmap='OrRd')
 81 ax[1].set_title("综合健康")
 82 # 综合健康占比
 83 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 84 sns.countplot(x='Asthma', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 85 ax[0].set_xlabel("气喘")
 86 ax[0].set_ylabel('是否气喘人数')
 87 heart_df.Asthma.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01,0.01], shadow=True, cmap='OrRd')
 88 ax[1].set_title("气喘")
 89 # 气喘占比
 90 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 91 sns.countplot(x='SkinCancer', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 92 ax[0].set_xlabel("皮肤癌")
 93 ax[0].set_ylabel('是否有皮肤癌人数')
 94 heart_df.SkinCancer.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01,0.01],shadow=True, cmap='OrRd')
 95 ax[1].set_title("皮肤癌")
 96 # 皮肤癌占比
 97 fig, ax = plt.subplots(1, 2, figsize=(15, 3))
 98 sns.countplot(x='KidneyDisease', data=heart_df, hue='HeartDisease', palette='Set3', ax=ax[0])
 99 ax[0].set_xlabel("肾脏疾病")
100 ax[0].set_ylabel('是否患有肾脏疾病人数')
101 heart_df.KidneyDisease.value_counts().plot.pie(ax=ax[1], autopct='%1.1f%%', explode=[0.01,0.01],shadow=True, cmap='OrRd')
102 ax[1].set_title("肾脏疾病")
103 # 肾脏疾病占比
104 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
105 plt.rcParams['axes.unicode_minus'] = False
106 warnings.filterwarnings('ignore')
107 plt.show()
108 heart_df.hist(figsize=(20,16))
109 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
110 plt.rcParams['axes.unicode_minus'] = False
111 plt.show()
112 # 男女比例
113 No_HeartDisease = len(heart_df[heart_df.HeartDisease == 'No'])
114 Yes_HeartDisease = len(heart_df[heart_df.HeartDisease == 'Yes'])
115 countFemale = len(heart_df[heart_df.Sex == 'Female'])
116 countMale = len(heart_df[heart_df.Sex == 'Male'])
117 print(f'没患病人数:{No_HeartDisease }', end=' ,')
118 print("没患病比率: {:.2f}%".format((No_HeartDisease / (len(heart_df.HeartDisease))*100)))
119 print(f'有患病人数:{Yes_HeartDisease }', end=' ,')
120 print("有患病比率: {:.2f}%".format((Yes_HeartDisease / (len(heart_df.HeartDisease))*100)))
121 print(f'女性人数:{countFemale }', end=' ,')
122 print("女性比例: {:.2f}%".format((countFemale / (len(heart_df.Sex))*100)))
123 print(f'男性人数:{countMale }', end=' ,')
124 print("男性比例: {:.2f}%".format((countMale / (len(heart_df.Sex))*100)))
125 fig, ax = plt.subplots(1, 2)
126 fig.set_size_inches(w=15, h=5)
127 sns.countplot(x="Sex", data=heart_df, ax=ax[0])
128 plt.xlabel("性别 (‘Female’= 男, ‘Male’= 女)")
129 sns.countplot(x="HeartDisease", data=heart_df,ax=ax[1])
130 plt.xlabel("是否患病 (‘No’ = 未患病, ‘Yes’= 患病)")
131 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
132 plt.rcParams['axes.unicode_minus'] = False
133 plt.show()
134 pd.crosstab(heart_df.Sex, heart_df.HeartDisease).plot(kind="bar", figsize=(15, 6), color=['dodgerblue','crimson' ])
135 plt.title('男女患病占比')
136 plt.xlabel('性别 (’Female’= 男, ‘Male’= 女)')
137 plt.xticks(rotation=0)
138 plt.legend(["没有患病", "患有心脏病"])
139 plt.ylabel('人数')
140 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
141 plt.rcParams['axes.unicode_minus'] = False
142 plt.show()
143 pd.crosstab(heart_df.AgeCategory, heart_df.HeartDisease).plot(kind="bar", figsize=(25,8))
144 plt.title('患病变化随年龄分布图')
145 plt.xlabel('年龄')
146 plt.ylabel('占比')
147 plt.savefig('heartDiseaseAndAges.png')
148 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
149 plt.rcParams['axes.unicode_minus'] = False
150 plt.show()
151 # 性别相关性
152 fig, ax = plt.subplots(figsize=(16, 6))
153 plt.subplot(121)
154 age_sex_HeartDisease = sns.boxenplot(x='Sex', y='AgeCategory', hue='HeartDisease', data=heart_df,palette='YlGn')
155 age_sex_HeartDisease.set_title("年龄-性别-患病")
156 plt.ylabel("年龄")
157 # 抽烟相关性
158 fig, ax = plt.subplots(figsize=(16, 6))
159 plt.subplot(121)
160 age_Smoking_HeartDisease=sns.boxenplot(x='Smoking',y='AgeCategory',hue='HeartDisease',data=heart_df,palette='YlGn')
161 age_Smoking_HeartDisease.set_title("年龄-抽烟-患病")
162 plt.ylabel("年龄")
163 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
164 plt.rcParams['axes.unicode_minus'] = False
165 plt.show()
166 # BMI与心脏病的相关性
167 plt.figure(figsize=(15, 7))
168 plt.subplot(2,2,1)
169 sns.pointplot(x='AgeCategory',y='BMI',data=heart_df,color='Lime',hue='HeartDisease')
170 plt.ylabel("身体质量指数")
171 plt.subplot(2, 2, 2)
172 sns.violinplot(x=heart_df.HeartDisease, y=heart_df.BMI, data=heart_df)
173 plt.ylabel("身体质量指数")
174 plt.subplot(2, 2, 3)
175 sns.distplot(heart_df.BMI)
176 plt.subplot(2, 2, 4)
177 sns.regplot(x='BMI', y='HeartDisease', data=heart_df)
178 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
179 plt.rcParams['axes.unicode_minus'] = False
180 warnings.filterwarnings('ignore')
181 plt.show()
182 # 睡眠时间与心脏病的相关性
183 plt.figure(figsize=(15,7))
184 plt.subplot(2,2,1)
185 sns.pointplot(x='AgeCategory',y='SleepTime',data=heart_df,color='Lime',hue='HeartDisease')
186 plt.ylabel("睡眠时间")
187 plt.subplot(2,2,2)
188 sns.violinplot(x=heart_df.HeartDisease,y=heart_df.SleepTime,data=heart_df)
189 plt.ylabel("睡眠时间")
190 plt.subplot(2,2,3)
191 sns.distplot(heart_df.SleepTime)
192 plt.subplot(2,2,4)
193 sns.regplot(x='SleepTime',y='HeartDisease',data=heart_df)
194 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
195 plt.rcParams['axes.unicode_minus'] = False
196 warnings.filterwarnings('ignore')
197 plt.show()
198 # 中风与心脏病的相关性
199 plt.figure(figsize=(15, 7))
200 plt.subplot(2, 2, 1)
201 sns.regplot(x='Stroke', y='HeartDisease', data=heart_df)
202 plt.subplot(2, 2, 2)
203 sns.countplot(y=heart_df.Stroke, hue=heart_df.HeartDisease)
204 plt.subplot(2, 2, 3)
205 sns.boxplot(x=heart_df.Stroke, y=heart_df.AgeCategory, data=heart_df)
206 plt.subplot(2, 2, 4)
207 sns.boxplot(x=heart_df.Stroke, y=heart_df.AgeCategory, hue=heart_df.HeartDisease, data=heart_df)
208 # 不能行走与心脏病的相关性
209 plt.figure(figsize=(15,7))
210 plt.subplot(2, 2, 1)
211 sns.regplot(x='DiffWalking', y='HeartDisease', data=heart_df)
212 plt.subplot(2, 2, 2)
213 sns.countplot(y=heart_df.DiffWalking, hue=heart_df.HeartDisease)
214 plt.subplot(2, 2, 3)
215 sns.boxplot(x=heart_df.DiffWalking, y=heart_df.AgeCategory, data=heart_df)
216 plt.subplot(2, 2, 4)
217 sns.boxplot(x=heart_df.DiffWalking, y=heart_df.AgeCategory, hue=heart_df.HeartDisease, data=heart_df)
218 # 糖尿病和心脏病的关系
219 plt.figure(figsize=(15,7))
220 plt.subplot(2,2,1)
221 sns.regplot(x='Diabetic',y='HeartDisease',data=heart_df)
222 plt.subplot(2,2,2)
223 sns.countplot(y=heart_df.Diabetic,hue=heart_df.HeartDisease)
224 plt.subplot(2,2,3)
225 sns.boxplot(x=heart_df.Diabetic,y=heart_df.AgeCategory,data=heart_df)
226 plt.subplot(2,2,4)
227 sns.boxplot(x=heart_df.Diabetic,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)
228 # 肾脏疾病与心脏病的相关性
229 plt.figure(figsize=(15,7))
230 plt.subplot(2,2,1)
231 sns.regplot(x='KidneyDisease',y='HeartDisease',data=heart_df)
232 plt.subplot(2,2,2)
233 sns.countplot(y=heart_df.KidneyDisease,hue=heart_df.HeartDisease)
234 plt.subplot(2,2,3)
235 sns.boxplot(x=heart_df.KidneyDisease,y=heart_df.AgeCategory,data=heart_df)
236 plt.subplot(2,2,4)
237 sns.boxplot(x=heart_df.KidneyDisease,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)
238 # 气喘与心脏病的相关性
239 plt.figure(figsize=(15,7))
240 plt.subplot(2,2,1)
241 sns.regplot(x='Asthma',y='HeartDisease',data=heart_df)
242 plt.subplot(2,2,2)
243 sns.countplot(y=heart_df.Asthma,hue=heart_df.HeartDisease)
244 plt.subplot(2,2,3)
245 sns.boxplot(x=heart_df.Asthma,y=heart_df.AgeCategory,data=heart_df)
246 plt.subplot(2,2,4)
247 sns.boxplot(x=heart_df.Asthma,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)
248 # 皮肤癌与心脏病的相关性
249 plt.figure(figsize=(15,7))
250 plt.subplot(2,2,1)
251 sns.regplot(x='SkinCancer',y='HeartDisease',data=heart_df)
252 plt.subplot(2,2,2)
253 sns.countplot(y=heart_df.SkinCancer,hue=heart_df.HeartDisease)
254 plt.subplot(2,2,3)
255 sns.boxplot(x=heart_df.SkinCancer,y=heart_df.AgeCategory,data=heart_df)
256 plt.subplot(2,2,4)
257 sns.boxplot(x=heart_df.SkinCancer,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)
258 # 身体活动和皮肤癌的关系
259 plt.figure(figsize=(15,7))
260 plt.subplot(2,2,1)
261 sns.regplot(x='PhysicalActivity',y='HeartDisease',data=heart_df)
262 plt.subplot(2,2,2)
263 sns.countplot(y=heart_df.PhysicalActivity,hue=heart_df.HeartDisease)
264 plt.subplot(2,2,3)
265 sns.boxplot(x=heart_df.PhysicalActivity,y=heart_df.AgeCategory,data=heart_df)
266 plt.subplot(2,2,4)
267 sns.boxplot(x=heart_df.PhysicalActivity,y=heart_df.AgeCategory,hue=heart_df.HeartDisease,data=heart_df)
268 plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
269 plt.rcParams['axes.unicode_minus'] = False
270 warnings.filterwarnings('ignore')
271 plt.show()
272 # 综合
273 sns.pairplot(heart_df[heart_df.GenHealth == 'poor'], vars=['AgeCategory', 'Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue='HeartDisease', palette="Accent", corner=True)
274 sns.pairplot(heart_df[heart_df.GenHealth == 'Fair'], vars=['AgeCategory', 'Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue='HeartDisease', palette="Accent", corner=True)
275 sns.pairplot(heart_df[heart_df.GenHealth == 'Excellent'], vars=['AgeCategory', 'Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue='HeartDisease', palette="Accent", corner=True)
276 sns.pairplot(heart_df[heart_df.GenHealth == 'Good'], vars = ['AgeCategory','Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue = 'HeartDisease', palette="Accent", corner=True)
277 sns.pairplot(heart_df[heart_df.GenHealth == 'Very good'], vars = ['AgeCategory','Stroke', 'DiffWalking', 'Diabetic', 'BMI', 'KidneyDisease', 'SkinCancer'], hue = 'HeartDisease', palette="Accent", corner=True)
278 plt.show()

四、总结

       通过对数据的分析和挖掘,达到了我们预期的目标,可以看出在美国年龄是影响心脏病患病的重要因素之一,其次BMI也是影响心脏病患病的因素之一,我们应该注意饮食,少吃油腻、热量多的食物,控制好自己的体重。同时我注意到有中风、不能行走、糖尿病、肾脏疾病、皮肤癌等并发症的人更容易患上心脏病,所以自身有患上这些疾病的人要多加注意,特别是上了年纪的人更应该注意,老年人患心血管疾病的几率本就偏大,如果再有上述并发症那么患上心脏病的概率将大大增加。

       在完成此设计时,我熟练的掌握了数据可视化中各种图表的运用和设计方法。但是没有运用机器学习的方法,要是运用到机器对此数据集的分析能更准确,更能分析出心脏病的诱因。

标签:数据分析,heart,HeartDisease,plt,df,心脏病,sns,ax,数据
From: https://www.cnblogs.com/lky0101/p/16990989.html

相关文章