利用svm算法实现癌症的诊断分类

利用svm算法实现癌症的诊断分类 一, 研究背景 癌症——由DNA变异引起的细胞恶性增生,是全球第二大死因,在2018年带走了估计约960万人的生命,占全球全年死亡人数的六分之一。基于组学数据的癌...

利用svm算法实现癌症的诊断分类

, 研究背景

癌症——由DNA变异引起的细胞恶性增生,是全球第二大死因,在2018年带走了估计约960万人的生命,占全球全年死亡人数的六分之一。基于组学数据的癌症预防和诊疗研究,对于提升人类的健康和福祉具有重要意义。

大数据挖掘是研究癌症的重要途径,有助于在分子水平上洞察癌变机制,为治疗、用药、预后监测提供帮助。

二,算法原理

 

设给定的数据集D (X1, y1), …, (X|D|, y|D|), 其中Xi是训练 元组,具有相关联的类标号yi,可以画出无限多条分离直线(或超平面)将两种不同类型 的元组分开,需要找出最好的那一条,对先前未见到的元组具有最小分类误差的那一条,SVM要搜索具有最大边缘的超平面,即最大边缘超平面

 

三,算法的实现

1加载数据源

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn import metrics
from sklearn.preprocessing import StandardScaler

 

读取数据

 

data = pd.read_csv("./data.csv")

 

2、数据探索

 

查看数据的基本情况:可以看到各字段数据没有缺失

 

data.info()

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 569 entries, 0 to 568

Data columns (total 32 columns):

id                         569 non-null int64

diagnosis                  569 non-null object

radius_mean                569 non-null float64

texture_mean               569 non-null float64

perimeter_mean             569 non-null float64

area_mean                  569 non-null float64

smoothness_mean            569 non-null float64

compactness_mean           569 non-null float64

concavity_mean             569 non-null float64

concave points_mean        569 non-null float64

symmetry_mean              569 non-null float64

fractal_dimension_mean     569 non-null float64

radius_se                  569 non-null float64

texture_se                 569 non-null float64

perimeter_se               569 non-null float64

area_se                    569 non-null float64

smoothness_se              569 non-null float64

compactness_se             569 non-null float64

concavity_se               569 non-null float64

concave points_se          569 non-null float64

symmetry_se                569 non-null float64

fractal_dimension_se       569 non-null float64

radius_worst               569 non-null float64

texture_worst              569 non-null float64

perimeter_worst            569 non-null float64

area_worst                 569 non-null float64

smoothness_worst           569 non-null float64

compactness_worst          569 non-null float64

concavity_worst            569 non-null float64

concave points_worst       569 non-null float64

symmetry_worst             569 non-null float64

fractal_dimension_worst    569 non-null float64

dtypes: float64(30), int64(1), object(1)

memory usage: 142.3+ KB

 

data.columns

Index(['id', 'diagnosis', 'radius_mean', 'texture_mean', 'perimeter_mean',

       'area_mean', 'smoothness_mean', 'compactness_mean', 'concavity_mean',

       'concave points_mean', 'symmetry_mean', 'fractal_dimension_mean',

       'radius_se', 'texture_se', 'perimeter_se', 'area_se', 'smoothness_se',

       'compactness_se', 'concavity_se', 'concave points_se', 'symmetry_se',

       'fractal_dimension_se', 'radius_worst', 'texture_worst',

       'perimeter_worst', 'area_worst', 'smoothness_worst',

       'compactness_worst', 'concavity_worst', 'concave points_worst',

       'symmetry_worst', 'fractal_dimension_worst'],

      dtype='object')

   

mean 代表平均值,se 代表标准差,worst 代表最大值,后30个特征值实际是10个特征值(radiustextureperimeterareasmoothnesscompactnessconcavityconcave pointssymmetryfractal_dimension_mean)的平均值、标准差和最大值。

 

 

3、数据整理

1)去掉没有实际意义的“id”列

 

data.drop("id",axis=1,inplace=True)

 

2“diagnosis”字段意思是诊断结果,将B(良性)设为0M(恶性)设为1

 

data['diagnosis']=data['diagnosis'].map({'M':1,'B':0})

 

3)后面30个字段可以分成3

 

features_mean= list(data.columns[1:11])
features_se=
list(data.columns[12:21])
features_worst=
list(data.columns[22:31])

 

4、特征字段的筛选

1)诊断结果可视化

 

sns.countplot(data['diagnosis'],label="Count")
plt.show()

 

2)用热力图呈现features_mean字段之间的相关性

 

corr = data[features_mean].corr()
plt.figure(
figsize=(14,14))
sns.heatmap(corr, annot=True)
plt.show()

 

 

热力图数值越接近1,相关性越大

 

因此radius_meanperimeter_mean area_mean 相关性非常大

compactness_meanconcavity_meanconcave_points_mean3个字段也是相关的,因此我们可以取其中的一个作为代表。

 

3)进行特征选择

 

特征选择的目的是降维,用少量的特征代表数据的特性,这样也可以增强分类器的泛化能力,避免数据过拟合。

 

features_remain = ['radius_mean','texture_mean', 'smoothness_mean','compactness_mean','symmetry_mean', 'fractal_dimension_mean']

 

 

可以从相关性大的的每类属性中任意选一个作为代表,

 

所以从meanseworst中选择mean,从radius_meanperimeter_mean area_mean中选择radius_mean,以及从compactness_meanconcavity_meanconcave_points_mean中选择compactness_mean,这样就可以把原来的10个属性缩减为6个属性

 

 

5、准备训练集和测试集 、

 

# 抽取30%的数据作为测试集,其余作为训练集
train, test = train_test_split(data, test_size = 0.3)# in this our main data is splitted into train and test
#
抽取特征选择的数值作为训练和测试数据
train_X = train[features_remain]
train_y=train[
'diagnosis']
test_X= test[features_remain]
test_y =test[
'diagnosis']

# 采用Z-Score规范化数据,保证每个特征维度的数据均值为0,方差为1
ss = StandardScaler()
train_X = ss.fit_transform(train_X)
test_X = ss.transform(test_X)
 

6、让SVM做训练和预测

# 创建SVM分类器
model = svm.SVC()
# 用训练集做训练
model.fit(train_X,train_y)
# 用测试集做预测
prediction=model.predict(test_X)
print('准确率: ', metrics.accuracy_score(prediction,test_y))

 

 

准确率:  0.9181286549707602

得出结果准确率在90以上,说明训练结果还不错。

四,算法评估

支持向量机(Support Vector Machines, SVM) 是一种快速可靠的线性分类器,在有限的数据量下性能非常好。给定训练数据(监督学习),SVM算法得到一个最优超平面,从而对训练数据进行分类。虽然具有训练时间长的缺点,但对复杂的非线性决策边界的建模能力具有高度准确性(使用最大边缘)。

 

小组成员:2020302641 王硕

2020302794 郭涛

  • 发表于 2023-05-09 19:40
  • 阅读 ( 277 )

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
王硕
王硕

西北工业大学

1 篇文章

作家榜 »

  1. 解弘艺 17 文章
  2. 高曾谊 16 文章
  3. 胡中天 14 文章
  4. 旺仔牛奶opo 14 文章
  5. LH 14 文章
  6. 罗柏荣 13 文章
  7. Panda-admin 13 文章
  8. 林晨 12 文章