数据分析之乳腺癌预测

2023-05-16

零、定义问题

1.1 数据介绍

http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.names

#属性域


1.示例代码号码

2.块厚度1 - 10

3.细胞大小的一致性1 - 10

4.电池形状的均匀性1 - 10

5.边缘附着力1 - 10

6.单个上皮细胞大小1 - 10

7.裸核1 - 10

8.平淡的染色质1 - 10

9.正常核仁1 - 10

10.有丝分裂1 - 10

11.分类:(2为良性,4为恶性)

1.2 问题定义

     这是一个乳腺癌的数据集,主要通过训练来分出是否患有乳腺癌

一、导入数据

     1.1 导入类库

In [2]:

# 导入类库
from pandas import read_csv
import pandas as pd
from sklearn import datasets
from pandas.plotting import scatter_matrix
from matplotlib import pyplot
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LogisticRegression

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns #要注意的是一旦导入了seaborn,matplotlib的默认作图风格就会被覆盖成seaborn的格式
%matplotlib notebook
  

     1.2 导入数据集


  1. Sample code number id number
  2. Clump Thickness 1 - 10
  3. Uniformity of Cell Size 1 - 10
  4. Uniformity of Cell Shape 1 - 10
  5. Marginal Adhesion 1 - 10
  6. Single Epithelial Cell Size 1 - 10
  7. Bare Nuclei 1 - 10
  8. Bland Chromatin 1 - 10
  9. Normal Nucleoli 1 - 10
    1. Mitoses 1 - 10
    2. Class: (2 for benign, 4 for malignant)
In [3]:

# 导入数据
breast_cancer_data =pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data',header=None
                               ,names = ['C_D','C_T','U_C_Si','U_C_Sh','M_A','S_E_C_S'
                                        ,'B_N','B_C','N_N','M','Class'])
  

二、数据概述

     2.1 查看数据维度

In [4]:

#显示数据维度
print (breast_cancer_data.shape)
  

(699, 11)
  

     2.2 查看数据

In [5]:

breast_cancer_data.info()
  

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 699 entries, 0 to 698
Data columns (total 11 columns):
C_D        699 non-null int64
C_T        699 non-null int64
U_C_Si     699 non-null int64
U_C_Sh     699 non-null int64
M_A        699 non-null int64
S_E_C_S    699 non-null int64
B_N        699 non-null object
B_C        699 non-null int64
N_N        699 non-null int64
M          699 non-null int64
Class      699 non-null int64
dtypes: int64(10), object(1)
memory usage: 57.4+ KB
  
In [6]:

breast_cancer_data.head(25)  # 这里注意id 1057013 的B_N为空值,用?代替。
  
Out[6]:
C_DC_TU_C_SiU_C_ShM_AS_E_C_SB_NB_CN_NMClass
010000255111213112
1100294554457103212
210154253111223112
310162776881343712
410170234113213112
510171228101087109714
6101809911112103112
710185612121213112
810330782111211152
910330784211212112
1010352831111113112
1110361722111212112
1210418015333234414
1310439991111233112
14104457287510795544
1510476307464614314
1610486724111212112
1710498154111213112
181050670107764104124
1910507186111213112
201054590732105105444
211054593105536771014
2210567843111212112
23105701384512?7314
2410595521111213112

     2.2 数据统计描述

In [8]:

print(breast_cancer_data.describe())
  

                C_D         C_T      U_C_Si      U_C_Sh         M_A  \
count  6.990000e+02  699.000000  699.000000  699.000000  699.000000   
mean   1.071704e+06    4.417740    3.134478    3.207439    2.806867   
std    6.170957e+05    2.815741    3.051459    2.971913    2.855379   
min    6.163400e+04    1.000000    1.000000    1.000000    1.000000   
25%    8.706885e+05    2.000000    1.000000    1.000000    1.000000   
50%    1.171710e+06    4.000000    1.000000    1.000000    1.000000   
75%    1.238298e+06    6.000000    5.000000    5.000000    4.000000   
max    1.345435e+07   10.000000   10.000000   10.000000   10.000000   

          S_E_C_S         B_C         N_N           M       Class  
count  699.000000  699.000000  699.000000  699.000000  699.000000  
mean     3.216023    3.437768    2.866953    1.589413    2.689557  
std      2.214300    2.438364    3.053634    1.715078    0.951273  
min      1.000000    1.000000    1.000000    1.000000    2.000000  
25%      2.000000    2.000000    1.000000    1.000000    2.000000  
50%      2.000000    3.000000    1.000000    1.000000    2.000000  
75%      4.000000    5.000000    4.000000    1.000000    4.000000  
max     10.000000   10.000000   10.000000   10.000000    4.000000  
  

     2.2 数据分布情况

In [9]:

print(breast_cancer_data.groupby('Class').size())
  

Class
2    458
4    241
dtype: int64
  

     2.3 缺失数据处理

In [11]:

mean_value = breast_cancer_data[breast_cancer_data["B_N"] != "?"]["B_N"].astype(np.int).mean() # 计算异常值列的平均值
  
In [12]:

breast_cancer_data = breast_cancer_data.replace('?',mean_value) # na替换?
  
In [13]:

breast_cancer_data["B_N"] = breast_cancer_data["B_N"].astype(np.int64)
  

三、数据可视化

3.1单变量图表

In [16]:

# 箱线图
breast_cancer_data.plot(kind='box', subplots=True, layout=(3,4), sharex=False, sharey=False)
pyplot.show()
  
In [17]:

# 直方图
breast_cancer_data.hist()
pyplot.show()
  

3.1多变量图表

In [19]:

# 散点矩阵图
scatter_matrix(breast_cancer_data)
pyplot.show()
  

四、评估算法

      4.1分离数据集

In [52]:

# 分离数据集
array = breast_cancer_data.values
X = array[:, 1:9] # C_D为编号,与Y无相关性,过滤掉
Y = array[:, 10]


validation_size = 0.2
seed = 7
X_train, X_validation, Y_train, Y_validation = train_test_split(X, Y, test_size=validation_size, random_state=seed)
  

      4.2评估算法

In [55]:

# 算法审查
models = {}
models['LR'] = LogisticRegression()
models['LDA'] = LinearDiscriminantAnalysis()
models['KNN'] = KNeighborsClassifier()
models['CART'] = DecisionTreeClassifier()
models['NB'] = GaussianNB()
models['SVM'] = SVC()

num_folds = 10
seed = 7
kfold = KFold(n_splits=num_folds, random_state=seed)
# 评估算法
results = []
for name in models:
    result = cross_val_score(models[name], X_train, Y_train, cv=kfold, scoring='accuracy')
    results.append(result)
    msg = '%s: %.3f (%.3f)' % (name, result.mean(), result.std())
    print(msg)
    
# 图表显示
fig = pyplot.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
pyplot.boxplot(results)
ax.set_xticklabels(models.keys())
pyplot.show()
  

KNN: 0.973 (0.018)
LDA: 0.959 (0.030)
SVM: 0.953 (0.036)
NB: 0.962 (0.031)
CART: 0.941 (0.033)
LR: 0.961 (0.026)
  

五、实施预测

In [75]:

#使用评估数据集评估算法
knn = KNeighborsClassifier()
knn.fit(X=X_train, y=Y_train)
predictions = knn.predict(X_validation)
print(accuracy_score(Y_validation, predictions))
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))
  

0.971428571429
[[89  2]
 [ 2 47]]
             precision    recall  f1-score   support

          2       0.98      0.98      0.98        91
          4       0.96      0.96      0.96        49

avg / total       0.97      0.97      0.97       140

  

六、git与参考

git :若云机器学习公开仓库

参考:机器学习之Python - 魏贞原 - 科技科普 - 原创 | 豆瓣阅读

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

数据分析之乳腺癌预测 的相关文章

随机推荐

  • 高保链路分析——一看就会

    高保链路分析 本身接口 超时降级 调用本接口耗时超过规定时间立马返回超时报文 限流降级 调用本接口QPS超过规定数值立马返回限流报文 兜底返回 xff08 代码容错 xff09 trycatch未知异常 xff0c 兜底返回报文 业务逻辑
  • android应用层操作底层硬件

    app操作底层硬件没权限的解决办法 xff1a 1 若机器已经root过 xff0c 可直接在应用层中操作 xff1a String apkRoot 61 34 chmod 777 34 43 getPackageCodePath Syst
  • ARM 安装中文输入法

    TX2 为嵌入式开发板 xff0c 系统架构为ARM xff0c 普通的PC上的Linux系统安装中文输入法的方法不适用 xff0c 所以这里提供ARM上的中文输入法的安装方法 步骤1 xff1a 打开终端 xff0c 输入以下命令 sud
  • cordova 打包步骤

    年底了 xff0c 好多资料都在整理 xff0c 为了避免遗忘 xff0c 也为了利益他人吧 直接上步骤吧 xff1a 增加运行环境的模板 cordova platform add android 编译android的程序 cordova
  • BlueROV-7: Keep Learning Dronekit

    The motors can spin now by using the following program from dronekit import connect VehicleMode import sys import time C
  • BlueROV-8: Functions to Drive the Vehicle

    Some functions like goto function or speed setting function are limited because they are GPS dependent GPS is not availa
  • BlueROV-9: Driving Control

    Home location http python dronekit io guide vehicle state and parameters html The Home location isset when a vehicle fir
  • 人工智能发展简史

    人工智能发展简史 第一章 xff1a 起步期 20世纪50年代及以前1 1 计算机象棋博弈 xff08 Programming a computer for playing chess xff09 1 2 图灵测试 xff08 Turing
  • 对角度滤波时0-360度跳变的解决办法

    对角度滤波的过程中会发现 xff0c 视觉direction方向会发生0 360的数值跳变 xff0c 为了解决这个问题 xff0c 需要将0和360度之间的间断点变成连续可导的函数 xff0c 经过摸索 xff0c 想出一个办法 xff0
  • 电机控制 龙伯格观测器 永磁同步电机无传感器控制 全C代码程序

    电机控制 龙伯格观测器 永磁同步电机无传感器控制 全C代码程序 成熟产品方案 DSP28335 xff08 1 xff09 全C程序完成由电机参数 电流微分方程构建dq轴误差模型 控制参数逻辑变换 低通滤波器转速滤波和转子角度积分等控制环节
  • ROS学习笔记-1: 构建工作空间-创建catkin包-编写发布器与订阅器

    1 ROS文件系统介绍 http wiki ros org cn ROS Tutorials NavigatingTheFilesystem 2 Installing catkin http wiki ros org catkin Inst
  • 信息安全重点知识

    一 信息安全概述 网络空间安全的重要性 xff1a 没有网络安全就没有国家安全信息安全 xff1a 防止数据未授权的访问 xff0c 数据有意和物一的威胁 网络安全是信息安全的子集 信息安全的三要素 xff08 CIA xff09 xff1
  • 海天注塑机KEBA系统数据采集

    本文章只针对海天注塑机的KEBA系统 xff0c 因为其他注塑机厂家也用KEBA系统 xff0c 他们的采集方式可能不太一样 xff0c 所以后续有时间我将写其他文章来解释 xff08 默认你已经向海天采购了OPC组件 xff09 一 采集
  • Axure基础:事件和动态面板

    这一篇文章我们主要是将如何做系统左侧的导航 xff0c 并且告诉大家如何动态的切换各个页面 一 事件 1 事件基础 事件的核心就是什么时候做什么事 其中的什么时候可以是如下 xff1a 能做的事情如下 xff1a 2 远程监控云中的事件 监
  • 设备联网调试三板斧

    在实际的工业互联网项目中 xff0c 设备联网所占的比重越来越大 有的一期项目为了简单快速上线 xff0c 让客户直观体会到工业互联网的效果 xff0c 直接会把设备联网放在一期项目的重点 那么在做此类项目时 xff0c 设备联网调试就显得
  • 光立方完全解析

    转载请注明出处 xff1a http blog csdn net ruoyunliufeng article details 37903899 这个4 4 4的三色光立方是我在初学单片机的时候做的一个小项目 很适合给初学单片机和C语言的同学
  • 远程视频监控之应用篇(mjpg-streamer)

    转载请注明出处 xff1a http blog csdn net ruoyunliufeng article details 38515311 这篇文章将主要结合源码介绍mjpg streamer xff0c 使小伙伴们了解视频监控的实现
  • Matplotlib 入门(三):多图合并

    一 多合一显示 1 subplot方法 xff1a 设置行 列和起始点 plt subplot 2 1 1 分成两行一列 xff0c 起始点为1 2 代码 coding utf 8 34 34 34 Created on Sun Sep 2
  • python毫秒级延时

    一 毫秒延时 近期有一个ms级别延时的需求 xff0c 实际测试了一下 xff0c 环境 xff1a win7 64位 xff0c python2 7 13 结果 xff1a 毫秒级别的延时是能够支持的 xff0c 微妙是不支持的 二 de
  • 数据分析之乳腺癌预测

    零 定义问题 1 1 数据介绍 http archive ics uci edu ml machine learning databases breast cancer wisconsin breast cancer wisconsin n