• python数据分析-糖尿病数据集数据分析预测


    一、研究背景和意义

    糖尿病是美国最普遍的慢性病之一,每年影响数百万美国人,并对经济造成重大的经济负担。糖尿病是一种严重的慢性疾病,其中个体失去有效调节血液中葡萄糖水平的能力,并可能导致生活质量和预期寿命下降。。。。

    本案例分析针对糖尿病数据集进行探索和分析:

    二、实证分析

    首先,导入需要的基础包:

    1. import numpy as np
    2. import pandas as pd
    3. import matplotlib.pyplot as plt
    4. %matplotlib inline
    5. plt.rcParams['font.sans-serif'] = ['KaiTi'] #中文
    6. plt.rcParams['axes.unicode_minus'] = False #负号
    7. import seaborn as sns

    读取数据文件

    1. ###读取文件数据
    2. df=pd.read_csv('data.csv')
    3. ###展示数据前15行
    4. df.head(15)

    数据集和代码

    报告代码数据

    ###各项特征名称

    年龄:13级年龄组(_AGEG5YR见密码本)

    1 = 18-24 / 2 = 25-29 / 3 = 30-34 / 4 = 35-39 / 5 = 40-44 / 6 = 45-49 / 7 = 50-54 / 8 = 55-59 / 9 = 60-64 / 10 = 65-69 / 11 = 70-74 / 12 = 75-79 / 13 = 80 岁或以上

    Sex:患者性别(1:男;0:女)

    HighChol:0 = 无高胆固醇 1 = 高胆固醇

    CholCheck:0 = 5 年内未进行胆固醇检查 1 = 5 年内进行了胆固醇检查

    BMI:身体质量指数

    吸烟者:您一生中至少吸过 100 支香烟吗? [注:5 包 = 100 支香烟] 0 = 否 1 = 是

    心脏病或发作:冠心病 (CHD) 或心肌梗塞 (MI) 0 = 否 1 = 是

    PhysActivity:过去 30 天的身体活动 - 不包括工作 0 = 否 1 = 是

    水果:每天吃水果 1 次或更多次 0 = 否 1 = 是

    蔬菜:每天吃蔬菜 1 次或更多次 0 = 否 1 = 是

    HvyAlcoholConsump:(成年男性每周 >=14 杯,成年女性每周 >=7 杯)0 = 否 1 = 是

    GenHlth:总体而言,您的健康状况是: 等级 1-5 1 = 极好 2 = 非常好 3 = 好 4 = 一般 5 = 差

    MentHlth:心理健康状况不佳的天数 1-30 天

    PhysHlth:过去 30 天的身体疾病或受伤天数 1-30

    DiffWalk:你走路或爬楼梯有严重困难吗? 0 = 否 1 = 是

    中风:您曾经中风。 0 = 否,1 = 是

    HighBP:0 = 不高,BP 1 = 高 BP

    糖尿病:0 = 无糖尿病,1 = 糖尿病

    发现数据量为七万多行,17个特征

    查看数据类型和形状

    接下来进行基本的统计性描述分析

    从上面结果可以看出,从描述中,我们观察到BMI,PhysHlth,MentHlth的标准差高于1, 

    最大值和最小值之间的差异相对较高 

    下来查看缺失值

    数据比较完整,无缺失值,若有的话可以可视化一下: 

    1. #观察缺失值可视化
    2. import missingno as msno
    3. msno.matrix(df)

     

    对特征分别进行可视化一下   比如各个特征的占比情况等等

    1. import seaborn as sb
    2. for i in df.columns:
    3. fig, ax = plt.subplots(1,1, figsize=(15, 6))
    4. sb.countplot(y = df[i],data=df, order=df[i].value_counts().index)
    5. plt.ylabel(i)
    6. plt.yticks(fontsize=13)
    7. plt.show()

     

    1. # 按性别分组,计算平均年龄和BMI
    2. grouped = df.groupby('Sex')[['Age', 'BMI']].mean()
    1. grouped['BMI'].plot(kind='bar')
    2. plt.title('Average BMI by Gender')
    3. plt.xlabel('Gender')
    4. plt.ylabel('Average BMI')
    5. plt.show()

    接下来看一下特征之间的相关系数

    从上面热力图可以看出,最大相关性在0.38左右

    再画出具体特征的分布

    sb.barplot(x=df['Diabetes'],y=df['HighBP'],color='red')

     

    下来用直方图表示

    1. df.hist(figsize=(20,20))
    2. plt.show()

    分别画出响应变量糖尿病与其他特征的关系

    接下来看一下糖尿病分布

    1. plt.figure(figsize=(12,5))
    2. sns.displot(x='PhysHlth', col='Diabetes' , data = df, kind="kde" ,color = 'pink')

     

    接下来进行标准化

    1. df1 = df
    2. cols = ['BMI', 'PhysHlth']
    3. for i in cols:
    4. df1[i] = (df1[i] - df1[i].min()) / (df1[i].max() - df1[i].min())

     下面开始机器学习部分

    1. ####划分训练集和验证集
    2. from sklearn.model_selection import train_test_split
    3. from sklearn.preprocessing import StandardScaler
    4. print('Non normalized dataset')
    5. x_train, x_test, y_train, y_test= train_test_split(x,y,test_size=0.25,random_state=101)
    6. print('Training: ', x_train.shape[0])
    7. print('Test: ', x_test.shape[0])
    8. st_x= StandardScaler()
    9. x_train= st_x.fit_transform(x_train)
    10. x_test= st_x.transform(x_test)
    11. print('Normalized dataset')
    12. x_train1, x_test1, y_train1, y_test1 = train_test_split(x1,y1,test_size=0.25,random_state=101)
    13. print('Training: ', x_train1.shape[0])
    14. print('Test: ', x_test1.shape[0])
    1. from sklearn.neighbors import KNeighborsClassifier
    2. import time
    3. from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
    4. exec = []
    5. exec1 = []
    6. st = time.time()
    7. knn = KNeighborsClassifier(n_neighbors=5)
    8. knn.fit(x_train, y_train)
    9. accuracy = []
    10. accuracy1 = []
    11. y_pred = knn.predict(x_test)
    12. cm = confusion_matrix(y_test, y_pred)
    13. print(cm)
    14. print('\n')
    15. print(classification_report(y_test,y_pred))
    16. print(accuracy_score(y_test, y_pred))
    17. accuracy.append(accuracy_score(y_test, y_pred))
    18. exec.append(time.time() - st)
    19. print('\n\nNormalized DataSet')
    20. st = time.time()
    21. knn.fit(x_train1, y_train1)

     

    使用其他模型试一下,最终结果如下

    1. #决策树
    2. from sklearn.tree import DecisionTreeClassifier
    3. model = DecisionTreeClassifier()
    4. model.fit(x_train, y_train)
    5. model.score(x_test, y_test)

     

    从以上结果可以看出,自适应提升Adaboost模型的效果还可以,达到了0.7486.其次是极端梯度提升,KNN以及最后的决策树。

    三、总结

    在这个项目中,我运用了机器学习的模型来预测一个人是否患有糖尿病,使用的模型包括自适应提升(AdaBoost)、K最近邻(KNN)和决策树(Decision Tree)等。自适应提升(AdaBoost)是一种集成学习方法.它通过不断迭代调整样本权重,训练出多个弱分类器,最终组合成一个强分类器。通过对不同算法的比较和分析,最终发现自适应提升最优的算法来进行预测,并根据预测结果来制定相应的医疗干预措施,以帮助预防和治疗糖尿病。。

    创作不易,希望大家多点赞关注评论!!!(类似代码或报告定制可以私信)

  • 相关阅读:
    五、kuternetes Pod介绍与配置
    Prometheus简单理解
    高版本Vivado和Linux 4.x内核移植Digilent Driver
    毕设 仓库管理系统
    JS数组at函数(获取最后一个元素的方法)介绍
    phpexcel 安装流程
    职场人的拖延症晚癌克星来啦 当当狸时间管理器
    上采样之反卷积操作
    LabVIEW使用VI Snippets存储和共享重用代码段
    React三属性之:props
  • 原文地址:https://blog.csdn.net/m0_62638421/article/details/139691964