【机器学习】基于逻辑回归的癌症预测案例

  1 import pandas as pd
  2 import numpy as np
  3 from sklearn.preprocessing import StandardScaler  # 标准化
  4 from sklearn.model_selection import train_test_split  # 数据集拆分
  5 from sklearn.linear_model import LogisticRegression
  6 from sklearn.metrics import classification_report
  7 from sklearn.metrics import roc_auc_score
  8 
  9 # 加载数据
 10 data = pd.read_csv("./breast-cancer-wisconsin.data", header=None)
 11 # print("data:\n", data)
 12 # 指定其列索引名称
 13 columns = [
 14     "Sample code number",
 15     "Clump Thickness",
 16     "Uniformity of Cell Size",
 17     "Uniformity of Cell Shape",
 18     "Marginal Adhesion",
 19     "Single Epithelial Cell Size",
 20     "Bare Nuclei",
 21     "Bland Chromatin",
 22     "Normal Nucleoli",
 23     "Mitoses",
 24     "Class"
 25 ]
 26 # 设置列索引
 27 data.columns = columns
 28 # print("data:\n", data)
 29 
 30 # 1、筛选出特征与目标值,将不重要、无关的特征干掉
 31 # 将id_编码 给删除
 32 data = data.iloc[:, 1:]
 33 print("数据集:\n", data)
 34 
 35 # 2、检测缺失值,并处理缺失值
 36 # res_null = pd.isnull(data).sum()
 37 # print("检测缺失值的结果:\n", res_null)
 38 
 39 # 3、先将"?"替换成np.nan类型
 40 data.replace("?", np.nan, inplace=True)
 41 
 42 # 4、检测
 43 res_null = pd.isnull(data).sum()
 44 print("检测缺失值的结果:\n", res_null)
 45 
 46 # 5、处理缺失值
 47 # 删除法
 48 # 填充法
 49 # 插值法
 50 # 删除法
 51 data.dropna(how="any", axis=0, inplace=True)
 52 print("删除缺失值之后的结果为:\n", data.shape)
 53 
 54 # 数据集拆分---
 55 train_x, test_x, train_y, test_y = train_test_split(data.iloc[:, :9].values, data.iloc[:, 9].values, test_size=0.3,
 56                                                     random_state=1)
 57 
 58 # 6、标准化处理数据
 59 # 标准差标准化
 60 # (1)实例化对象
 61 stand = StandardScaler()
 62 # (2)计算指标并进行数据转化---只需要标准化特征值
 63 train_x = stand.fit_transform(train_x)
 64 test_x = stand.fit_transform(test_x)
 65 
 66 # 7、构建逻辑回归模型进行癌症预测
 67 # (1)构建算法实例
 68 lr = LogisticRegression()
 69 # (2)训练数据
 70 lr.fit(train_x, train_y)
 71 # (3)预测
 72 y_predict = lr.predict(test_x)
 73 
 74 # 获取准确率
 75 score = lr.score(test_x, test_y)  # 准确率为96.1% # 3.9%错误率
 76 
 77 # 获取权重与偏置
 78 weight = lr.coef_
 79 bias = lr.intercept_
 80 
 81 print("预测值:\n", y_predict)
 82 print("准确率:\n", score)
 83 print("权重:\n", weight)
 84 print("偏置:\n", bias)
 85 print("*" * 100)
 86 
 87 # 癌症患者---去体检---检测为健康---造成结果:错过最佳治疗时间,人命关天。---不能接受的
 88 # 健康的人--体检 ---检测为癌症 ----造成后果:复检。
 89 
 90 # 在 二分类情况下,两种结果的重要性程度不一样的情况下,准确率将不再是唯一的评判标准
 91 # 召回率 --对于分类的评估
 92 # 召回率= 预测为正例/真实类别为正例 # 越高越好--查的全
 93 res_report = classification_report(y_true=test_y, y_pred=y_predict, labels=[2, 4], target_names=["良性", "恶性"])
 94 print("res_report:\n", res_report)
 95 
 96 # 模型还不错 召回率为0.96  准确率为0.96
 97 # 比如说:样本数据100个样本,1个正常,99个癌症患者-----正例为正常的
 98 # 预测 100个全部为癌症患者 ----准确率99%
 99 # 召回率:0/1 = 0 召回率为0
100 # 此时以癌症患者为正例:准确率为99% 召回率为 99/99 = 1
101 # 应用的时候---全部预测为癌症患者
102 
103 # 样本不均衡情况下,召回率、准确率 又不能完全去衡量 结果
104 # AUC指标--只用在 样本不均衡的情况
105 # ROC曲线
106 # 在不同阈值的情况下,TPR(召回率) 与FPR 描绘出的曲线就是ROC曲线
107 # AUC ---ROC曲线与TPR与FPR 右下部分组成面积
108 # 对于好的模型---AUC指标>0.5
109 # AUC为 1 ,那么预测完全准确,那么这个模型叫最优模型(理想状态下)
110 # AUC最坏的情况下,AUC=0.5
111 # AUC --->(0.5,1)
112 # 意义:随机取一个正样本,预测为正例样本的概率 > 反例样本的概率
113 # 随机选一个反例样本,预测为正例的概率 < 阈值
114 # y_true  传真实类别,正例为1 反例为0
115 # y_score  --预测值
116 # 可以使用三目表达式
117 # 参数1  条件
118 # 参数2  条件成立执行它
119 # 参数3  条件为假执行它
120 # test_y = np.where(test_y > 3, 1, 0)
121 # print("test_y:\n", test_y)
122 # res_score = roc_auc_score(y_true=test_y, y_score=y_predict)
123 # print("AUC指标:\n", res_score)  # 0.95
124 
125 # 假设已经发现样本不均衡----将样本变为均衡的

猜你喜欢

转载自www.cnblogs.com/Tree0108/p/12116253.html