WBPC乳腺癌预后诊断数据集(Ⅲ)—基于SVM与PCA的预后诊断模型建立

文章目录

    • 关于数据
    • 模型求解思路
      • 建立关于data的诊断模型建立
      • 利用原有的数据建立预后复发判断模型
      • 利用原有修正的数据建立预后复发判断模型
    • 数据导入与处理
    • 建立关于data的诊断模型
      • 降维模型
    • 主成分分析
      • 模型改进分析
    • KNN
    • 小结

关于数据

1、原有数据经过一定的缺失值处理德等操作后得到数据集data_2其包括乳针吸细胞的的32特征(30项细胞特征、一项肿瘤特征、一项淋巴状态特征)194项数据,其是关于预后患者是否复发的数据;
2、补充数据集:在WPBC官网上找到的诊断数据,其共有30项特征(30项细胞特征)569例数据,为数据集data,其主要是判断患者是否肿瘤为恶性的数据。

模型求解思路

这里主要使用的算法:
1、线性的SVM支持向量机linearSVM
2、非线性(核函数方法)的SVM分类向量机。SVC

建立关于data的诊断模型建立

因为关于data的数据量比较大,笔者首先利用data数据(70%训练30%测试),得到的训练模型可以判断患者的肿瘤是否为恶性,在用于测试其是否可以用于判断预后的患者是否复发。

利用原有的数据建立预后复发判断模型

利用原有修正的数据建立预后复发判断模型

将data2数据70%训练30%测试。

数据导入与处理

#引用约定
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
import numpy as np
import seaborn as sns
import pandas as pd
#导入数据
data1=pd.read_csv('data_1.csv')#原数据
data=pd.read_csv('data.csv')#补充数据
data2=pd.read_csv('data_2.csv')#修正数据
#将补充数据数据进行分块处理
datax=data.drop(['id','diagnosis'],axis = 1)
data['diagnosis']=data['diagnosis'].map({'M':1,'B':0})
datay=data['diagnosis']
#将原数据数据数据进行分块处理
data1x=data1.drop(['id','outcome','time'],axis = 1)
data1y=data1['outcome']
#将原数据数据数据进行分块处理
data2x=data2.drop(['id','outcome','time'],axis = 1)
#用于测试补充的效果,故将后两项指标删去
data2x0=data2.drop(['id','outcome','time','diameter of the excised tumor in centimeters','Lymph node status'],axis = 1)
data2['outcome']=data2['outcome'].map({'R':1,'N':0})
data2y=data2['outcome']
#采用z_Scorce规范化数据,使得每个特征维度的数据均值为0,方差为1
# ss=StandardScaler()
# datax=ss.fit_transform(datax)
# data2x=ss.fit_transform(data2x)
# data2x0=ss.fit_transform(data2x0)
#将数据分成70%的训练集与30%的测试集
datax_train, datax_test, datay_train, datay_test = train_test_split(datax, datay, test_size=0.3)
data2x_train, data2x_test, data2y_train, data2y_test = train_test_split(data2x, data2y, test_size=0.3)
data2x0_train, data2x0_test, data2y_train, data2y_test=train_test_split(data2x0, data2y, test_size=0.3)
data1x_train, data1x_test, data1y_train, data1y_test = train_test_split(data1x, data1y, test_size=0.3)

建立关于data的诊断模型

LinearSVC

基于liblinear库实现
有多种惩罚参数和损失函数可供选择
训练集实例数量大(大于1万)时也可以很好地进行归一化
既支持稠密输入矩阵也支持稀疏输入矩阵
多分类问题采用one-vs-rest方法实现
SVC

基于libsvm库实现
训练时间复杂度为0( n 2 n^2 n2)
训练集实例数量大(大于1万)时很难进行归一化
多分类问题采用one-vs-rest方法实现

#模型建立
model_data=svm.LinearSVC()
model_data1=svm.SVC()
model_data_1=svm.LinearSVC()
model_data_11=svm.SVC()
model_data_2=svm.LinearSVC()
model_data_21=svm.SVC()
#data linearSVC模型训练
model_data.fit(datax_train,datay_train)
d:\program_for_code\python\lib\site-packages\sklearn\svm\_base.py:947: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  "the number of iterations.", ConvergenceWarning)





LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
          intercept_scaling=1, loss='squared_hinge', max_iter=1000,
          multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
          verbose=0)
##data SVC模型训练
model_data1.fit(datax_train,datay_train)
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)
#data_1 linearSVC模型训练
model_data_1.fit(data1x_train,data1y_train)
d:\program_for_code\python\lib\site-packages\sklearn\svm\_base.py:947: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  "the number of iterations.", ConvergenceWarning)





LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
          intercept_scaling=1, loss='squared_hinge', max_iter=1000,
          multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
          verbose=0)
#data_1 SVC模型训练
model_data_11.fit(data1x_train,data1y_train)
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)
#data_2 linearSVC模型训练
model_data_2.fit(data2x_train,data2y_train)
d:\program_for_code\python\lib\site-packages\sklearn\svm\_base.py:947: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  "the number of iterations.", ConvergenceWarning)





LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
          intercept_scaling=1, loss='squared_hinge', max_iter=1000,
          multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
          verbose=0)
#data_2 SVC模型训练
model_data_21.fit(data2x_train,data2y_train)
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)

相关参数解释
参数:

C:C-SVC的惩罚参数C?默认值是1.0
C越大,相当于惩罚松弛变量,希望松弛变量接近0,即对误分类的惩罚增大,趋向于对训练集全分对的情况,这样对训练集测试时准确率很高,但泛化能力弱。C值小,对误分类的惩罚减小,允许容错,将他们当成噪声点,泛化能力较强。

kernel :核函数,默认是rbf,可以是‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’

– 线性:u’v

– 多项式:(gammau’v + coef0)^degree

– RBF函数:exp(-gamma|u-v|^2)

– sigmoid:tanh(gammau’v + coef0)

degree :多项式poly函数的维度,默认是3,选择其他核函数时会被忽略。
gamma : ‘rbf’,‘poly’ 和‘sigmoid’的核函数参数。默认是’auto’,则会选择1/n_features
coef0 :核函数的常数项。对于‘poly’和 ‘sigmoid’有用。
probability :是否采用概率估计?.默认为False
shrinking :是否采用shrinking heuristic方法,默认为true
tol :停止训练的误差值大小,默认为1e-3
cache_size :核函数cache缓存大小,默认为200
class_weight :类别的权重,字典形式传递。设置第几类的参数C为weight*C(C-SVC中的C)
verbose :允许冗余输出?
max_iter :最大迭代次数。-1为无限制。
decision_function_shape :‘ovo’, ‘ovr’ or None, default=None3
random_state :数据洗牌时的种子值,int值
主要调节的参数有:C、kernel、degree、gamma、coef0。

#查看拟合效果
print('补充数据的LinearSVC模型诊断肿瘤性质准确率:')
model_data.score(datax_test,datay_test)
补充数据的LinearSVC模型诊断肿瘤性质准确率:





0.783625730994152
print('补充数据的SVC模型诊断肿瘤性质准确率:')
model_data1.score(datax_test,datay_test)
补充数据的SVC模型诊断肿瘤性质准确率:





0.8947368421052632
print('补充数据的LinearSVC模型预后准确率:')
model_data.score(data2x0_test,data2y_test)
补充数据的LinearSVC模型预后准确率:





0.559322033898305
print('补充数据的SVC模型预后准确率:')
model_data1.score(data2x0_test,data2y_test)
补充数据的SVC模型预后准确率:





0.3389830508474576
print('原数据的LinearSVC模型预后准确率:')
model_data_1.score(data1x_test,data1y_test)
原数据的LinearSVC模型预后准确率:





0.2711864406779661
print('原数据的SVC模型预后准确率:')
model_data_11.score(data1x_test,data1y_test)
原数据的SVC模型预后准确率:





0.7288135593220338
print('原修正数据的LinearSVC模型预后准确率:')
model_data_2.score(data2x_test,data2y_test)
原修正数据的LinearSVC模型预后准确率:





0.7966101694915254
print('原修正数据的SVC模型预后准确率:')
model_data_21.score(data2x_test,data2y_test)
原修正数据的SVC模型预后准确率:





0.7966101694915254

降维模型

data2.head()
id outcome time radius_mean texture_mean perimeter_mean area_mean smoothness_mean compactness_mean concavity_mean ... perimeter_worst area_worst smoothness_worst compactness_worst concavity_worst concave points_worst symmetry_worst fractal_dimension_worst diameter of the excised tumor in centimeters Lymph node status
0 119513 0 31 18.02 27.600000 117.50 1013.0 0.094890 0.103600 0.1086 ... 139.70 1436.0 0.119500 0.192600 0.3140 0.1170 0.267700 0.08113 5.0 5
1 8423 0 61 17.99 22.300979 122.80 1001.0 0.118400 0.142642 0.3001 ... 184.60 2019.0 0.162200 0.665600 0.7119 0.2654 0.460100 0.11890 3.0 2
2 842517 0 116 21.37 17.440000 137.50 1373.0 0.088360 0.118900 0.1255 ... 159.10 1949.0 0.118800 0.344900 0.3414 0.2032 0.433400 0.09067 2.5 0
3 843483 0 123 11.42 20.380000 77.58 386.1 0.102774 0.142642 0.2414 ... 98.87 567.7 0.143921 0.364567 0.6869 0.2575 0.322251 0.17300 2.0 0
4 843584 1 27 20.29 14.340000 135.10 1297.0 0.100300 0.132800 0.1980 ... 152.20 1575.0 0.137400 0.205000 0.4000 0.1625 0.236400 0.07678 3.5 0

5 rows × 35 columns

feature_mean=list(data2.columns[3:13])
feature_se=list(data2.columns[11:21])
feature_worst=list(data2.columns[21:31])
feature_other=list(data2.columns[31:33])
print(feature_mean)
['radius_mean', 'texture_mean', 'perimeter_mean', 'area_mean', 'smoothness_mean', 'compactness_mean', 'concavity_mean', 'concave points_mean', 'symmetry_mean', 'fractal_dimension_mean']
feature_remain=['texture_mean','area_mean','concavity_mean','compactness_mean'
                ,'radius_worst','texture_worst','perimeter_worst','compactness_worst','area_worst','concavity_worst']
data3=data2[feature_remain]
data3.head()
texture_mean area_mean concavity_mean compactness_mean radius_worst texture_worst perimeter_worst compactness_worst area_worst concavity_worst
0 27.600000 1013.0 0.1086 0.103600 21.63 37.08 139.70 0.192600 1436.0 0.3140
1 22.300979 1001.0 0.3001 0.142642 25.38 17.33 184.60 0.665600 2019.0 0.7119
2 17.440000 1373.0 0.1255 0.118900 24.90 20.98 159.10 0.344900 1949.0 0.3414
3 20.380000 386.1 0.2414 0.142642 14.91 26.50 98.87 0.364567 567.7 0.6869
4 14.340000 1297.0 0.1980 0.132800 22.54 16.67 152.20 0.205000 1575.0 0.4000
#采用z_Scorce规范化数据,使得每个特征维度的数据均值为0,方差为1
# ss=StandardScaler()
# data3x=ss.fit_transform(data3)
data3x_train, data3x_test, data3y_train, data3y_test = train_test_split(data3, data2y, test_size=0.3)
model_data_3=svm.LinearSVC()
model_data_31=svm.SVC()
#data_3 linearSVC模型训练
model_data_3.fit(data3x_train,data3y_train)
d:\program_for_code\python\lib\site-packages\sklearn\svm\_base.py:947: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  "the number of iterations.", ConvergenceWarning)





LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
          intercept_scaling=1, loss='squared_hinge', max_iter=1000,
          multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
          verbose=0)
#data_3 linearSVC模型训练
model_data_31.fit(data3x_train,data3y_train)
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)
#查看拟合效果
print('降维数据的LinearSVC模型诊断肿瘤性质准确率:')
model_data_3.score(data3x_test,data3y_test)
降维数据的LinearSVC模型诊断肿瘤性质准确率:





0.7627118644067796
#查看拟合效果
print('降维数据的SVC模型诊断肿瘤性质准确率:')
model_data_31.score(data3x_test,data3y_test)
降维数据的SVC模型诊断肿瘤性质准确率:





0.7627118644067796

主成分分析

利用SPSS提取出7个主成分,分别为:FAC1_1~FAC2_2

data_PCA=pd.read_csv('主成分.csv')
data_PCA.head()

outcome FAC1_1 FAC2_1 FAC3_1 FAC4_1 FAC5_1 FAC6_1 FAC7_1
0 N 0.056555 -0.854017 -0.641684 1.600536 0.233733 0.678122 0.851336
1 N 1.190768 1.652260 0.217565 -1.164537 2.576727 1.693878 -0.012999
2 N 0.938121 -1.049531 0.619337 -1.586026 2.958155 -1.203444 -0.379524
3 N -1.377811 1.105750 0.871857 -0.407618 0.502238 -0.001458 -0.512918
4 R 0.573373 -0.626717 1.645737 -2.419531 -1.445667 0.121050 -0.215974

模型改进分析

1、与假阳性案例相比,自定义损失函数以惩罚假阴性(真实R但预测为N)案例,因为假阴性在真实环境中可能威胁生命,而假阳性只是相对烦恼。
2、深入研究我们预测错误的案例。这将帮助我了解模型出了什么问题以及如何(如果可能)解决此问题。
3、在系统中添加“模糊性”,以将困难的案例定向给人类专家,这样我们就避免了危及生命的情况。
4、使用t-sne图可视化这两个类别。 深入研究降维,以仅选择最佳维数以节省计算能力。
5、在此数据上比较神经网络,梯度提升和knn的性能。 尝试概率图形模型。
6、通过删除相关特征并一次添加一个特征(最重要)来比较模型性能。 数据大小对模型性能的影响。

data_PCAx=data_PCA.drop(['outcome'],axis=1)
data_PCA['outcome']=data_PCA['outcome'].map({'R':1,'N':0})
data_PCAy=data_PCA['outcome']
#采用z_Scorce规范化数据,使得每个特征维度的数据均值为0,方差为1
#ss=StandardScaler()
#data_PCAx=ss.fit_transform(data_PCAx)
#将数据分成70%的训练集与30%的测试集
data_PCAx_train, data_PCAx_test, data_PCAy_train, data_PCAy_test = train_test_split(data_PCAx, data_PCAy, test_size=0.3)
#模型构建
model_data_PCA=svm.LinearSVC()
model_data_PCA1=svm.SVC()
#linearSVC模型构建
model_data_PCA.fit(data_PCAx_train,data_PCAy_train)
LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
          intercept_scaling=1, loss='squared_hinge', max_iter=1000,
          multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
          verbose=0)
print('原数据经过PCA降维后的linearSVC模型预后准确率:')
model_data_PCA.score(data_PCAx_test,data_PCAy_test)
原数据经过PCA降维后的linearSVC模型预后准确率:





0.8305084745762712
#linearSVC模型构建
model_data_PCA1.fit(data_PCAx_train,data_PCAy_train)
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)
print('原数据经过PCA降维后的SVC模型预后准确率:')
model_data_PCA1.score(data_PCAx_test,data_PCAy_test)
原数据经过PCA降维后的SVC模型预后准确率:





0.847457627118644

KNN

尝试使用KNN算法进行分类

from sklearn.neighbors import KNeighborsClassifier
knn=KNeighborsClassifier(n_neighbors=5,p=2
                         ,metric='minkowski')
knn.fit(data_PCAx_train,data_PCAy_train)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
                     metric_params=None, n_jobs=None, n_neighbors=5, p=2,
                     weights='uniform')
knn.score(data_PCAx_test,data_PCAy_test)
0.7457627118644068
knn.fit(data2x_train,data2y_train)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
                     metric_params=None, n_jobs=None, n_neighbors=5, p=2,
                     weights='uniform')
knn.score(data2x_test,data2y_test)
0.7627118644067796
data.head()
id diagnosis radius_mean texture_mean perimeter_mean area_mean smoothness_mean compactness_mean concavity_mean concave points_mean ... radius_worst texture_worst perimeter_worst area_worst smoothness_worst compactness_worst concavity_worst concave points_worst symmetry_worst fractal_dimension_worst
0 842302 M 17.99 10.38 122.80 1001.0 0.11840 0.27760 0.3001 0.14710 ... 25.38 17.33 184.60 2019.0 0.1622 0.6656 0.7119 0.2654 0.4601 0.11890
1 842517 M 20.57 17.77 132.90 1326.0 0.08474 0.07864 0.0869 0.07017 ... 24.99 23.41 158.80 1956.0 0.1238 0.1866 0.2416 0.1860 0.2750 0.08902
2 84300903 M 19.69 21.25 130.00 1203.0 0.10960 0.15990 0.1974 0.12790 ... 23.57 25.53 152.50 1709.0 0.1444 0.4245 0.4504 0.2430 0.3613 0.08758
3 84348301 M 11.42 20.38 77.58 386.1 0.14250 0.28390 0.2414 0.10520 ... 14.91 26.50 98.87 567.7 0.2098 0.8663 0.6869 0.2575 0.6638 0.17300
4 84358402 M 20.29 14.34 135.10 1297.0 0.10030 0.13280 0.1980 0.10430 ... 22.54 16.67 152.20 1575.0 0.1374 0.2050 0.4000 0.1625 0.2364 0.07678

5 rows × 32 columns

小结

 以上内容主要为笔者jupyter notebook的5月初建模的内容,赛后还未进行详细的注释阐述,主要研究内容是如何选用算法得到较高的正确率,数据集的特点是不到200份,所以做出的效果不如WPBC官网500数据量的数据集的效果,迫于时间精力的有限,暂定以此作为结果,不足之处望读者多多指正。

你可能感兴趣的:(机器学习,python,数学建模,机器学习,算法,python,WPBC数据集,乳腺癌预后诊断)