Advertisement

【机器学习】基于多变量特征的乳腺癌自动诊断

阅读量:

本文介绍通过多变量特征实现乳腺癌诊断的方法,基于WDBC数据集进行代码实现。数据集包含569条样本,其中357条为良性样本,212条为恶性样本,每个样本有32个特征(包括ID和标签)。文章详细描述了数据读取、标准化、过采样(SMOTE)以及随机森林模型的调参过程。通过网格搜索法调整了nestimators、maxdepth、max_features和criterion等参数,最终获得最佳模型参数。模型在测试集上的准确率为0.958,精度为0.948,召回率为0.973,FPR为0.059,特异度为0.941,F1值为0.961。此外,还比较了其他五类机器学习算法在乳腺癌诊断任务中的性能。

多变量特征实现乳腺癌诊断

本文主要基于多分类模型达成乳腺癌诊断目标,基于Breast Cancer Wisconsin (Diagnostic) Data Set(WDBC)数据集进行代码开发。

数据集简介

数据集来源:WDBC乳腺癌数据集

  • 数据集属性:多元的。
    • 样本容量:569例,其中357例为良性(benign),212例为恶性(malignant)。
    • 每条样本特征数:每条样本包含32个特征,包括ID、标签以及30个实值输入特征。
    • 相关任务:主要任务是进行分类。

其中,数据集的样本特征中,不包括ID和标签,主要由半径、纹理、周长、面积等细胞核实值特征构成,共计30项。在此基础上,无需逐一详细说明,所有内容均已包含在总体程序压缩包中。

以下以Random Forest模型举例:

数据读取

复制代码
    # 读取数据
    import numpy as np
    import pandas as pd
    from sklearn import preprocessing
    # 原始数据以csv格式读取
    dataset = pd.read_csv(r'C:\Users\Lenovo\Desktop\analyse\wdbc.data.csv',header=None)
    # 标签化diagnosis列
    encoder = preprocessing.LabelEncoder().fit(dataset['diagnosis'])
    dataset['diagnosis'] = encoder.transform(dataset['diagnosis'])
    print('标签: %s' % encoder.classes_)

读取数据

复制代码
    # 获取数据
    X = np.array(dataset)
    dataset_target = X[:, 1] # 原始标签数据
    dataset_data = X[:, 2:32] # 原始输入数据
    # 数据集计数
    from collections import Counter
    Counter(dataset_target)
    # Counter({1.0: 212, 0.0: 357})

样本均衡

复制代码
    # SMOTE过采样,平衡正负样本数据量,提升识别效果
    from imblearn.over_sampling import SMOTE
    smo = SMOTE()
    # 得到采样后的数据样本
    dataset_data_sm, dataset_target_sm = smo.fit_resample(dataset_data, dataset_target)
    Counter(dataset_target_sm)

数据标准化

复制代码
    from sklearn import preprocessing
    import numpy as np
    dataset_data_scaled = preprocessing.scale(dataset_data_sm)

模型调参

复制代码
    # 通过网格搜索法得出最优的参数组合
    # 随机森林是由多个决策树集成而成。n_estimators为弱学习器的个数,n_estimators太小,容易欠拟合,太大,容易过拟合。max_depth决定了决策树的最大深度。
    from sklearn.model_selection import GridSearchCV
    
    param =  {'n_estimators': np.arange(2, 20, 2),'max_depth': np.arange(2, 11)}
    gc = GridSearchCV(model, param_grid=param, cv=5)
    gc.fit(X_train,y_train)
    print("最佳参数组合:",gc.best_params_)
    depth = gc.best_params_['max_depth']
    estimator = gc.best_params_['n_estimators']
    # 最佳参数组合: {'max_depth': 7, 'n_estimators': 16}
    # 确定n_estimators和max_depth参数,调整其他两个参数
    # max_features的增加一般能提升模型性能,在每个节点,选择更多。但是会降低算法的速度,需要适当平衡选择最佳的值。
    # criterion为CART树做划分时对特征的评价标准,分类模型和回归模型的损失函数是不一样的
    model = RandomForestClassifier(random_state=90, max_depth=depth, n_estimators=estimator)
    param =  {"max_features":np.arange(2,30,2),"criterion":['gini','entropy']}
    gc = GridSearchCV(model, param_grid=param, cv=5)
    gc.fit(X_train,y_train)
    print("最佳参数组合:",gc.best_params_)
    criterion = gc.best_params_['criterion']
    feature = gc.best_params_['max_features']
    # 最佳参数组合: {'criterion': 'gini', 'max_features': 14}

诊断效果评估

复制代码
    model = RandomForestClassifier(random_state=90, max_depth=depth, n_estimators=estimator,criterion=criterion,max_features=feature)
    model.fit(X_train, y_train)
    # 基于测试数据进行预测
    y_pred = model.predict(X_test)
    # 得到预测结果,四舍五入去掉小数点位
    predictions = [round(value) for value in y_pred] 
    # 评估准确率效果
    accuracy = accuracy_score(y_test, predictions)
    precision = precision_score(y_test, predictions)
    recall = recall_score(y_test, predictions)
    f1 = f1_score(y_test,predictions)
    tn, fp, fn, tp = confusion_matrix(y_test, y_pred,labels=[0,1]).ravel()
    fpr = fp/(fp+tn)
    spe = tn/(fp+tn)
    
    #准确率: 0.958041958041958
    #精度: 0.948051948051948
    #召回率: 0.9733333333333334
    #FPR: 0.058823529411764705
    #特异度: 0.9411764705882353
    #F1值: 0.9605263157894737

采用以下几种算法:Bayes、KNN、SVM、决策树及随机森林,对模型参数进行优化调整,以乳腺癌诊断任务为目标。资源代码已集成:**CSV格式数据集、数据集介绍文档、以及(五类机器学习算法的乳腺癌诊断Python实现)

全部评论 (0)

还没有任何评论哟~