数据描述
Number of Instances: 699(as of 15 July 1922)
Number of Attributes: 10 plus the class attribute
Attribute Information: (class attribute has been moved to last column)
Attribute | Dcmain |
---|---|
1.样本编码 Simple code nuber | 1-10 |
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 |
10.有丝分裂 MItoses | 1-10 |
11.类 Class | 2表示良性,4表示恶性 |
Missing attribute values: 16
- There are 16 instances in Groups 1 to 6 that contain a single missing(i.e., unavailable) attribute value, now denoted by “?”.
Class distribution:
- Benign: 458(65.5%)
- Malignant: 241(34.5%)
该原始数据共有699条样本,其中良/恶性分布分别为65.5%和34.5%,每条样本有11列不同的数值:1列用于检索的id,9列与肿瘤相关的医学特征,以及一列表征肿瘤类型的数值。所有9列用于表示肿瘤医学特质地数值均被量化为1~10之间的数字,而肿瘤的类型也借由数字2和数字4分别指代良性与恶行。不过,这份数据也声明其中包含16个缺失值,并且用“?”标出
数据处理
import pandas as pd
import numpy as np
# 创建特征列表
column_names = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape', 'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli', 'Mitoses', 'Class']
# 使用pandas.read_csv函数从互联网读取指定数据
data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data',names=column_names)
# 将?替换为标准缺失值表示
data = data.replace(to_replace='?', value=np.nan)
# 丢弃带有缺失值的数据(只要有一个维度有缺失)
data = data.dropna(how='any')
print(data.shape) # (683,11)
由于数据没有提供对应的测试样本用于评估模型性能,因此需要对带有标记的数据进行分割。通常情况下,25%的数据会作为测试集,其余75%的数据用于训练
# 使用sklearn.model_selection里的train_test_split模块用于分割数据
from sklearn.model_selection import train_test_split
# 随机采样25%的数据用于测试,剩下的75%用于构建训练集合
X_train, X_test, y_train, y_test = train_test_split(data[column_names[1:10]], data[column_names[10]], test_size=0.25, random_state=33)
# 查验训练样本的数量和类别分布
print(y_train.value_counts())
# 查验测试样本的数量和类别分布
print(y_test.value_counts())
综上,我们用于训练样本共有512条(344条良性,168条恶性),测试样本有171条(100条良性,71条恶性)
尝试模型
# 从sklearn.preprocessing里导入StandardScaler
from sklearn.preprocessing import StandardScaler
# 从sklearn.linear_model里导入LogisticRegression与SGDClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
# 标准化数据,保证每个维度的特征数据方差为1,均值为0.使得预测结果不会被某些维度过大的特征值而主导,即归一化
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)
# 初始化LogisticRegression与SGDClassifier
lr = LogisticRegression()
sgdc = SGDClassifier()
# 调用LogisticRegression中的fit函数/模块用来训练模型参数
lr.fit(X_train, y_train)
# 使用训练好的模型lr对X_test进行预测,结果存储在变量lr_y_predict中
lr_y_predict = lr.predict(X_test)
# 调用SGDClassifier中的fit函数/模块用来训练模型参数
sgdc.fit(X_train, y_train)
# 使用训练好的模型sgdc对X_test进行预测,结果存储在变量sgdc_y_predicet中
sgdc_y_predict = sgdc.predict(X_test)
这里我们选择查全率(recall),查准率(precision),F1指标作为模型评判指标。性能度量相关具体内容可以看这篇文章
# 从sklearn.metrics里导入classification_report模块
from sklearn.metrics import classification_report
# 使用逻辑斯蒂回归模型自带的评分函数score获得模型在测试集上的准确性结果
print('Accuracy of LR Classifier:', lr.score(X_test, y_test))
# 利用classification_report模块获得LogisticRegression其他三个指标的结果
print(classification_report(y_test, lr_y_predict, target_names=['Benign', 'Malignant']))
# 使用随机梯度下降模型自带的评分函数score获得模型在测试集上的准确性结果
print('Accuracy of SGD Classifier:', sgdc.score(X_test, y_test))
# 利用classification_report模块获得SGDClassifier其他三个指标的结果
print(classification_report(y_test, sgdc_y_predict, target_names=['Benign', 'Malignant']))