家居企业网站建设如何,个人主页搭建,网站建设便宜的公司,山东省工程建设信息官方网站目录
一、数据分析及对象
二、目的及分析任务
三、方法及工具
四、数据读入
五、数据理解
六、数据准备
七、模型训练
八、模型应用及评价 一、数据分析及对象
CSV文件——“bc_data.csv”
数据集链接#xff1a;https://download.csdn.net/download/m0_70452407/88…目录
一、数据分析及对象
二、目的及分析任务
三、方法及工具
四、数据读入
五、数据理解
六、数据准备
七、模型训练
八、模型应用及评价 一、数据分析及对象
CSV文件——“bc_data.csv”
数据集链接https://download.csdn.net/download/m0_70452407/88524905
该数据集主要记录了569个病例的32个属性主要属性/字段如下
1ID病例的ID。
2Diagnosis诊断结果M为恶性B为良性。该数据集共包含357个良性病例和212个恶性病例。
3细胞核的10个特征值包括radius半径、texture纹理、perimeter周长、面积area、平滑度smoothness、紧凑度compactness、凹面concavity、凹点concave points、对称性symmetry和分形维数fractal dimension等。同时为上述10个特征值分别提供了3种统计量分别为均值mean、标准差standard error和最大值worst or largest。
二、目的及分析任务
1使用训练集对SVM模型进行训练。
2使用SVM模型对威斯康星乳腺癌数据集的诊断结果进行预测。
3对SVM模型进行评价。
三、方法及工具
Python语言及pandas、NumPy、matplotlib、scikit-learn包。
svm.SVC的超参数及其解读
svm.SVC的超参数及其解读 参数名称参数类型说明C浮点型必须为正默认值为1.0在sklearn.svm.SVC中使用的惩罚是L2范数的平方C对应的是此惩罚的正则化参数即惩罚的系数。C值越大则表明对分类错误的惩罚越大因此分类结果更倾向于全正确的情况C值越小则表明对分类错误的惩罚越小因此分类结果将允许更多的错误。kernel可以是以下中的任意字符’linear,poly,rbf,sigmoid,precomputed默认为rbf。核函数类型rbf为径向基函数linear为线性核poly为多项式核函数degree类型int默认值为3当指定kernel为poly时表示选择的多项式的最高次数默认为三次多项式polygammascale、’auto或者float默认值为scale在0.22版本之前默认为autogamma为rbf、’poly、sigmoid的核系数。decision_function_shape默认为ovr只有两个值可供选择ovr和ovo在处理多分类问题时确定采用某种策略。ovr表示一对一的分类器假如有k个类别则需要构建k*(k-1)/2个分类器ovo为一对多的分类器假如有k个类别则需要构建k个分类器。
四、数据读入
导入需要的第三方包
import pandas as pd
import numpy as np
import matplotlib.pyplot#导入sklearn的svm
from sklearn import svm#导入metrics评估方法
from sklearn import metrics#train_test_split用于拆分训练集和测试集
from sklearn.model_selection import train_test_split#StandardScalery作用是去均值和方差归一化
from sklearn.preprocessing import StandardScaler
读入数据
df_bc_datapd.read_csv(D:\\Download\\JDK\\数据分析理论与实践by朝乐门_机械工业出版社\\第4章 分类分析\\bc_data.csv)
对数据集进行显示
df_bc_data 五、数据理解
对数据框df_bc_data进行探索性分析这里采用的实现方法为调用pandas包中数据框DataFrame的describe()方法。
df_bc_data.describe() 查看数据集中是否存在缺失值
df_bc_data.info() class pandas.core.frame.DataFrame
RangeIndex: 569 entries, 0 to 568
Data columns (total 32 columns):# Column Non-Null Count Dtype
--- ------ -------------- ----- 0 id 569 non-null int64 1 diagnosis 569 non-null object 2 radius_mean 569 non-null float643 texture_mean 569 non-null float644 perimeter_mean 569 non-null float645 area_mean 569 non-null float646 smoothness_mean 569 non-null float647 compactness_mean 569 non-null float648 concavity_mean 569 non-null float649 concave points_mean 569 non-null float6410 symmetry_mean 569 non-null float6411 fractal_dimension_mean 569 non-null float6412 radius_se 569 non-null float6413 texture_se 569 non-null float6414 perimeter_se 569 non-null float6415 area_se 569 non-null float6416 smoothness_se 569 non-null float6417 compactness_se 569 non-null float6418 concavity_se 569 non-null float6419 concave points_se 569 non-null float6420 symmetry_se 569 non-null float6421 fractal_dimension_se 569 non-null float6422 radius_worst 569 non-null float6423 texture_worst 569 non-null float6424 perimeter_worst 569 non-null float6425 area_worst 569 non-null float6426 smoothness_worst 569 non-null float6427 compactness_worst 569 non-null float6428 concavity_worst 569 non-null float6429 concave_points_worst 569 non-null float6430 symmetry_worst 569 non-null float6431 fractal_dimension_worst 569 non-null float64
dtypes: float64(30), int64(1), object(1)
memory usage: 142.4 KB 查看数据是否存在不均衡的问题
df_bc_data[diagnosis].value_counts() B 357
M 212
Name: diagnosis, dtype: int64 六、数据准备
由于id一列并非为自变量或因变量删除该列。
new_bcdf_bc_data.drop([id],axis1)
将diagnosis属性字段的取值M使用1代替B使用0代替。
new_bc[diagnosis]new_bc[diagnosis].map({M:1,B:0})
将数据集拆分为训练集和测试集这里使用20%的数据作为测试集。
bc_train,bc_testtrain_test_split(new_bc,test_size0.2)
将训练集和测试集的数据属性和标签进行拆分。
#对训练集的数据和标签进行拆分
bc_train_databc_train.iloc[:,1:]
bc_train_labelbc_train[diagnosis]
#对测试集的数据和标签进行拆分
bc_test_databc_test.iloc[:,1:]
bc_test_labelbc_test[diagnosis]
为了排除数值的量纲对结果的影响需要对训练数据和预测数据进行标准化处理。
bc_train_dataStandardScaler().fit_transform(bc_train_data)
bc_test_dataStandardScaler().fit_transform(bc_test_data)
七、模型训练
使用训练集训练SVM模型。除了直接指定参数的数值之外还可以使用自动调参计数如GridSearchCV进行参数选择。
bc_modelsvm.SVC(C0.2,kernellinear) #创建SVM分类器
bc_model.fit(bc_train_data,bc_train_label) #训练模型 SVC(C0.2, kernellinear) 八、模型应用及评价
使用已经训练好的SVM模型在测试集上进行测试并输出评价指标的取值。
#在测试集上应用模型并进行评价
predictionbc_model.predict(bc_test_data)
#评价指标
print(混淆矩阵\n,metrics.confusion_matrix(bc_test_label,prediction))
print(准确率,metrics.accuracy_score(bc_test_label,prediction))
print(查准率,metrics.precision_score(bc_test_label,prediction))
print(召回率,metrics.recall_score(bc_test_label,prediction))
print(F1值,metrics.f1_score(bc_test_label,prediction)) 混淆矩阵[[74 0][ 1 39]]
准确率 0.9912280701754386
查准率 1.0
召回率 0.975
F1值 0.9873417721518987