• Python机器学习实战-建立Gradient Boosting模型预测肾脏疾病(附源码和实现效果)


    实现功能

    建立Gradient Boosting模型预测肾脏疾病

    实现代码

    1. import pandas as pd
    2. import warnings
    3. warnings.filterwarnings("ignore")
    4. pd.set_option('display.max_columns', 26)
    5. #==========================读取数据======================================
    6. df = pd.read_csv("E:\数据杂坛\datasets\kidney_disease.csv")
    7. df=pd.DataFrame(df)
    8. pd.set_option('display.max_rows', None)
    9. pd.set_option('display.width', None)
    10. df.drop("id",axis=1,inplace=True)
    11. print(df.head())
    12. print(df.dtypes)
    13. df["classification"] = df["classification"].apply(lambda x: x if x == "notckd" else "ckd")
    14. # 分类型变量名
    15. cat_cols = [col for col in df.columns if df[col].dtype == "object"]
    16. # 数值型变量名
    17. num_cols = [col for col in df.columns if df[col].dtype != "object"]
    18. # ========================缺失值处理============================
    19. def random_value_imputate(col):
    20. """
    21. 函数:随机填充方法(缺失值较多的字段)
    22. """
    23. # 1、确定填充的数量;在取出缺失值随机选择缺失值数量的样本
    24. random_sample = df[col].dropna().sample(df[col].isna().sum())
    25. # 2、索引号就是原缺失值记录的索引号
    26. random_sample.index = df[df[col].isnull()].index
    27. # 3、通过loc函数定位填充
    28. df.loc[df[col].isnull(), col] = random_sample
    29. def mode_impute(col):
    30. """
    31. 函数:众数填充缺失值
    32. """
    33. # 1、确定众数
    34. mode = df[col].mode()[0]
    35. # 2、fillna函数填充众数
    36. df[col] = df[col].fillna(mode)
    37. for col in num_cols:
    38. random_value_imputate(col)
    39. for col in cat_cols:
    40. if col in ['rbc','pc']:
    41. # 随机填充
    42. random_value_imputate('rbc')
    43. random_value_imputate('pc')
    44. else:
    45. mode_impute(col)
    46. # ======================特征编码============================
    47. from sklearn.preprocessing import MinMaxScaler
    48. mms = MinMaxScaler()
    49. df[num_cols] = mms.fit_transform(df[num_cols])
    50. from sklearn.preprocessing import LabelEncoder
    51. led = LabelEncoder()
    52. for col in cat_cols:
    53. df[col] = led.fit_transform(df[col])
    54. print(df.head())
    55. #===========================数据集划分===============================
    56. X = df.drop("classification",axis=1)
    57. y = df["classification"]
    58. from sklearn.utils import shuffle
    59. df = shuffle(df)
    60. from sklearn.model_selection import train_test_split
    61. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 0)
    62. #===========================建模=====================================
    63. from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
    64. def create_model(model):
    65. # 模型训练
    66. model.fit(X_train, y_train)
    67. # 模型预测
    68. y_pred = model.predict(X_test)
    69. # 准确率acc
    70. acc = accuracy_score(y_test, y_pred)
    71. # 混淆矩阵
    72. cm = confusion_matrix(y_test, y_pred)
    73. # 分类报告
    74. cr = classification_report(y_test, y_pred)
    75. print(f"Test Accuracy of {model} : {acc}")
    76. print(f"Confusion Matrix of {model}: \n{cm}")
    77. print(f"Classification Report of {model} : \n {cr}")
    78. from sklearn.ensemble import GradientBoostingClassifier
    79. gb = GradientBoostingClassifier()
    80. create_model(gb)

    实现效果

    本人读研期间发表5篇SCI数据挖掘相关论文,现在某研究院从事数据挖掘相关科研工作,对数据挖掘有一定认知和理解,会结合自身科研实践经历不定期分享关于python机器学习、深度学习、数据挖掘基础知识与案例。

    致力于只做原创,以最简单的方式理解和学习,关注我一起交流成长。

    邀请三个朋友关注V订阅号:数据杂坛,即可在后台联系我获取相关数据集和源码,送有关数据分析、数据挖掘、机器学习、深度学习相关的电子书籍。

  • 相关阅读:
    flutter系列之:构建Widget的上下文环境BuildContext详解
    【第01天】A + B | 基础输入输出,开启学习Java旅程
    uniApp开发小程序基础教程(一)
    【SQL刷题】Day11----SQL通配符专项练习
    论企业IPV4和IPV6网络融合互通网络规划设计
    2304. 网格中的最小路径代价-297地平线周赛回顾
    WGAN(1)——为什么不能直接拟合原始分布Pr
    python二分查找
    【漏洞复现】Apache OFBiz 路径遍历导致RCE漏洞(CVE-2024-36104)
    Mysql索引原理
  • 原文地址:https://blog.csdn.net/sinat_41858359/article/details/132830806