linear_classifier

良/恶性乳腺癌肿瘤预测

数据描述

数据下载地址:良/恶性乳腺癌肿瘤预测任务的原始数据

  • 原始数据共有699条样本,每条样本有11列不同的数值:
  • 1列用于检索的id,9列与肿瘤相关的医学特征,以及一列表征肿瘤特征的数值;
  • 所有9列数值均被量化为1~10之间的数字;
  • 肿瘤的类型也由数字2和4分别代表良性和恶性;
  • 这份数据其中包含16个缺失值,用“?”表示。

数据预处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- coding: UTF-8 -*-
# 导入 pandas 与 numpy 工具包
import pandas as pd
import numpy as np
# 创建特征列表
column_names = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Sizes', 'Uniformity 0f Cell Shape', 'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromation', '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)
# 将?替换为标准缺失值表示
dta = data.replace(to_replace='?', value=np.nan)
# 丢弃带有缺失值的数据(只要有一个维度有缺失)
data = data.dropna(how='any')
# 输出 data 的数据量和维度
data.shape

准备训练、测试数据

由于原始数据没有提供对应的测试样本用于评估模型性能,因此需要对带有标记的数据进行分割。通常情况下,25%的数据会作为测试集,剩下75%作为训练集。

1
2
3
4
5
6
7
8
9
10
# 使用 sklearn.cross_validation 里的 train_test_split 模块用于分割数据
from sklearn.cross_validation 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)
# 查验训练样本的数量和类别分布
y_train.value_counts()
# 查验测试样本的数量和类别分布
y_test.value_counts()

使用线性分类模型进行预测任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 从 sklearn.preprocessing 里导入 StandardScaler
from sklearn.preprocessing import StandardScaler
# 从 sklearn.linear_model 里导入 LogisticRegression 与 SGDClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassfier
# 标准化数据,保证每个维度的特征数据方差为1,均值为0。使得预测结果不会被某些维度过大的特征值而主导
ss = StandarScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)
# 初始化 LogisticRegression 与 SGDClassfier
Ir = LogisticRegression()
sgdc = SGDClassfier()
# 调用 LogisticRegression 中的 fit 函数/模块用来训练模型参数
Ir.fit(X_train, y_train)
# 使用训练好的模型 Ir 对 X_test 进行预测,结果存储在变量 Ir_y_predict中
Ir_y_predict = Ir.predict(X_test)
# 调用 SGDClassfier 中的 fit函数/模块用来训练模型参数
sgdc.fit(X_train, y_train)
# 使用训练好的模型 sgdc 对 X_test 进行预测,结果储存在变量 sgdc_y_predict中
sgdc_y_predict = sgdc.predict(X_test)

使用线性分类模型的性能分析

评价指标:

  • 准确性 Accuracy:比对预测结果和原本的正确标记,计算全部测试样本中预测正确的百分比。

  • 召回率 Recall

  • 精确率 Precision

  • 为了综合考虑召回率与精确率,计算这两个指标的调和平均数 F1 measure

1
2
3
4
5
6
7
8
9
10
11
12
# 从 sklearn.metrics 里导入 classification_report模块
from sklearn.metrics import classification_report
# 使用 Logistic 回归模型自带的评分函数 score 获得模型在测试集上的准确性结果
print 'Accuracy of LR Classfier:', Ir.score(X_test, y_test)
# 利用 classification_report 模块获得 LogisticRegression 其他三个指标的结果
print classification_report(y_test, Ir_y_predict, target_names=['Benign', 'Malignant'])
# 使用随机梯度下降模型自带的评分函数 score 获得模型在测试集上的准确性结果
print 'Accuracy of SGD Classfier:', sgdc-score(X_test, y_test)
# 利用 classification_report 模块获得 SGDClassfier 其他三个指标的结果
print classification_report(y_test, sgdc_y_predict, target_names=['Benign','Malignant'])

分析

线性分类器是最基本和常用的机器学习模型。这里所使用的的模型包括 LogisticRegression 和 SGDClassifier。输出如图:

LR

SGD

(为什么一样呢?……..

  • LogisticRegression 对参数的计算采用精确解析的方式,计算时间长但是模型性能略高;
  • SGDClassifier采用随机梯度上升算法估计模型参数,计算时间短但是产出的模型性能略低。
    一般对于训练数据规模在10万量级以上的数据,采用随机梯度算法对模型参数进行估计。