
MNIST数据集是一组由美国高中生和人口调查局员工手写的70,000个数字的图片,每张图片上面有代表的数字标记。
这个数据集被广泛使用,被称之为机器学习领域的“Hello World”,主要是被用于分类问题。本文是对MNIST数据集执行一个二分类的建模
关键词:随机梯度下降、二元分类、混淆矩阵、召回率、精度、性能评估
在这里是将一份存放在本地的mat文件的数据导进来:
In [1]:
import pandas as pd
import numpy as np
import scipy.io as si
# from sklearn.datasets import fetch_openmlIn [2]:
mnist = si.loadmat('mnist-original.mat')In [3]:
type(mnist)   # 查看数据类型Out[3]:
dictIn [4]:
mnist.keys()Out[4]:
dict_keys(['__header__', '__version__', '__globals__', 'mldata_descr_ordering', 'data', 'label'])我们发现导进来的数据是一个字典。其中data和label两个键的值就是我们想要的特征和标签数据
In [5]:
# 修改1:一定要转置
X, y = mnist["data"].T, mnist["label"].T
X.shapeOut[5]:
(70000, 784)总共是70000张图片,每个图片中有784个特征。图片是28*28的像素,所以每个特征代表一个像素点,取值从0-255。
In [6]:
y.shapeOut[6]:
(70000, 1)In [7]:
y   # 每个图片有个专属的数字Out[7]:
array([[0.],
       [0.],
       [0.],
       ...,
       [9.],
       [9.],
       [9.]])In [8]:
import matplotlib as mpl
import matplotlib.pyplot as plt
one_digit = X[0]
one_digit_image = one_digit.reshape(28, 28)
plt.imshow(one_digit_image, cmap="binary")
plt.axis("off")
plt.show()In [9]:
y[0]    # 真实的标签的确是0Out[9]:
array([0.])  # 结果是0元数据中标签是字符串,我们需要转成整数类型
In [10]:
y.dtypeOut[10]:
dtype('<f8')In [11]:
y = y.astype(np.uint8)前面的6万条是训练集,后面的1万条是测试集
In [12]:
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]比如现在有1张图片,显示是0,我们识别是:“0和非0”,两种情形即可,这就是简单的二元分类问题
In [13]:
y_train_0 = (y_train == 0)  # 挑选出5的部分
y_test_0 = (y_test == 0)使用scikit-learn自带的SGDClassifier分类器:能够处理非常大型的数据集,同时SGD适合在线学习
In [14]:
from sklearn.linear_model import SGDClassifier
sgd_c = SGDClassifier(random_state=42)  # 设置随机种子,保证运行结果相同
sgd_c.fit(X_train, y_train_0)
/Applications/downloads/anaconda/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py:993: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)Out[14]:
SGDClassifier(random_state=42)在这里我们检查下数字0的图片:结果为True
In [15]:
sgd_c.predict([one_digit])  # one_digit是0,非5 表示为FalseOut[15]:
array([ True])一般而言,分类问题的评估比回归问题要困难的多。
In [16]:
# K折交叉验证
from sklearn.model_selection import StratifiedKFold
# 用于生成分类器的副本
from sklearn.base import clone
# 实例化对象
k_folds = StratifiedKFold(
    n_splits = 3,  # 3折
    shuffle=True,  # add   一定要设置shuffle才能保证random_state生效
    random_state=42
)
# 每个折叠由StratifiedKFold执行分层抽样
for train_index, test_index in k_folds.split(X_train, y_train_0):
    # 分类器的副本
    clone_c = clone(sgd_c)
    X_train_folds = X_train[train_index]  # 训练集的索引号
    y_train_folds = y_train_0[train_index]
    X_test_fold = X_train[test_index]  # 测试集的索引号
    y_test_fold = y_train_0[test_index]
    clone_c.fit(X_train_folds, y_train_folds)  # 模型训练
    y_pred = clone_c.predict(X_test_fold)  # 预测
    n_correct = sum(y_pred == y_test_fold)  # 预测准确的数量
    print(n_correct / len(y_pred))  # 预测准确的比例运行的结果如下:
[0.09875 0.09875 0.09875 ... 0.90125 0.90125 0.90125]
[0.0987 0.0987 0.0987 ... 0.9013 0.9013 0.9013]
[0.0987 0.0987 0.0987 ... 0.9013 0.9013 0.9013]使用cross_val_score来评估分类器:
In [17]:
# 评估分类器的效果
from sklearn.model_selection import cross_val_score
cross_val_score(sgd_c,  # 模型
                X_train,  # 数据集
                y_train_0,
                cv=3,  # 3折
                scoring="accuracy"  # 准确率
               )
# 结果
array([0.98015, 0.95615, 0.9706 ])可以看到准确率已经达到了95%以上,效果是相当的可观
自定义一个“非0”的简易分类器,看看效果:
In [18]:
from sklearn.base import BaseEstimator  # 基分类器
class Never0Classifier(BaseEstimator):
    def fit(self, X, y=None):
        return self
    def predict(self, X):
        return np.zeros((len(X), 1), dtype=bool)In [19]:
never_0_clf = Never0Classifier()
cross_val_score(
    never_0_clf,  # 模型
    X_train,   # 训练集样本
    y_train_0,  # 训练集标签
    cv=3,  # 折数
    scoring="accuracy"
    )Out[19]:
array([0.70385, 1.     , 1.     ])In [20]:
统计数据中每个字出现的次数:
pd.DataFrame(y).value_counts()Out[20]:
1    7877
7    7293
3    7141
2    6990
9    6958
0    6903
6    6876
8    6825
4    6824
5    6313
dtype: int64In [21]:
6903 / 70000Out[21]:
下面显示大约有10%的概率是0这个数字
0.09861428571428571In [22]:
(0.70385 + 1 + 1) / 3Out[22]:
0.9012833333333333可以看到判断“非0”准确率基本在90%左右,因为只有大约10%的样本是属于数字0。
所以如果猜测一张图片是非0,大约90%的概率是正确的。
评估分类器性能更好的方法是混淆矩阵,总体思路是统计A类别实例被划分成B类别的次数
混淆矩阵是通过预测值和真实目标值来进行比较的。
cross_val_predict函数返回的是每个折叠的预测结果,而不是评估分数
In [23]:
from sklearn.model_selection import cross_val_predict
y_train_pred = cross_val_predict(
    sgd_c,  # 模型
    X_train,  # 特征训练集
    y_train_0,  # 标签训练集
    cv=3  # 3折
)
y_train_predOut[23]:
array([ True,  True,  True, ..., False, False, False])In [24]:
# 导入混淆矩阵
from sklearn.metrics import confusion_matrix
confusion_matrix(y_train_0, y_train_pred)Out[24]:
array([[52482,  1595],
       [  267,  5656]])混淆矩阵中:行表示实际类别,列表示预测类别
In [25]:
# 假设一个完美的分类器:只存在真正类和真负类,它的值存在于对角线上
y_train_perfect_predictions = y_train_0
confusion_matrix(y_train_0, y_train_perfect_predictions)Out[25]:
array([[54077,     0],
       [    0,  5923]])召回率的公式为:
混淆矩阵显示的内容:
精度:正类预测的准确率
召回率(灵敏度或真正类率):分类器正确检测到正类实例的比例
In [26]:
from sklearn.metrics import precision_score, recall_score
precision_score(y_train_0, y_train_pred)  # 精度Out[26]:
0.78003034064267In [27]:
recall_score(y_train_0, y_train_pred)  # 召回率Out[27]:
0.9549214924869154F_1系数是精度和召回率的谐波平均值。只有当召回率和精度都很高的时候,分类器才会得到较高的F_1分数
𝐹1=21精度+1召回率(3)(3)F1=21精度+1召回率
In [28]:
from sklearn.metrics import f1_score
f1_score(y_train_0, y_train_pred)Out[28]:
0.8586609989373006精度和召回率通常是一对”抗体“,我们一般不可能同时增加精度又减少召回率,反之亦然,这就现象叫做精度/召回率权衡
In [29]:
# 使用decision_function
y_scores = sgd_c.decision_function([one_digit])
y_scoresOut[29]:
array([24816.66593936])In [30]:
threshold = 0  # 设置阈值
y_digit_pred = y_scores > threshold
y_digit_predOut[30]:
array([ True])In [31]:
# 提升阈值
threshold = 100000
y_digit_pred = y_scores > threshold
y_digit_predOut[31]:
array([False])In [32]:
y_scores = cross_val_predict(
    sgd_c,
    X_train,
    y_train_0.ravel(),  # 原文 y_train_0
    cv=3,
    method="decision_function")
y_scoresOut[32]:
array([ 51616.39393745,  27082.28092103,  20211.29278048, ...,
       -23195.59964776, -21022.63597851, -18702.17990507])有了这些分数就可以计算精度和召回率:
In [33]:
from sklearn.metrics import precision_recall_curve
precisions, recalls, thresholds = precision_recall_curve(y_train_0, y_scores)In [34]:
precisions  # 精度Out[34]:
array([0.10266944, 0.10265389, 0.10265566, ..., 1.        , 1.        ,
       1.        ])In [35]:
recalls   # 召回率Out[35]:
array([1.00000000e+00, 9.99831167e-01, 9.99831167e-01, ...,
       3.37666723e-04, 1.68833361e-04, 0.00000000e+00])In [36]:
thresholds  # 阈值Out[36]:
array([-86393.49001095, -86375.60229796, -86374.22313529, ...,
        92555.12952489,  93570.30614671,  96529.58216984])In [37]:
def figure_precision_recall(precisions, recalls, thresholds):
    plt.plot(thresholds, precisions[:-1],"b--",label="Precision")  # 精度-蓝色
    plt.plot(thresholds, recalls[:-1],"g-",label="Recall")  # 召回率-绿色
    plt.legend(loc="center right", fontsize=12)
    plt.xlabel("Threshold", fontsize=16)
    plt.grid(True)
figure_precision_recall(precisions, recalls, thresholds)
plt.show()直接绘制精度和召回率的曲线图:
# 精度-召回率
plt.plot(recalls[:-1], precisions[:-1],"b--")
plt.legend(loc="center right", fontsize=12)
plt.xlabel("Threshold", fontsize=16)
plt.grid(True)现在我们将精度设置成90%,通过np.argmax()函数来获取最大值的第一个索引,即表示第一个True的值:
In [39]:
threshold_90_precision = thresholds[np.argmax(precisions >= 0.9)]
threshold_90_precisionOut[39]:
9075.648564157285In [40]:
y_train_pred_90 = (y_scores >= threshold_90_precision)
y_train_pred_90Out[40]:
array([ True,  True,  True, ..., False, False, False])In [41]:
# 再次查看精度和召回率
precision_score(y_train_0, y_train_pred_90)Out[41]:
0.9001007387508395In [42]:
recall_score(y_train_0, y_train_pred_90)Out[42]:
0.9051156508526085还有一种经常和二元分类器一起使用的工具,叫做受试者工作特征曲线ROC。
绘制的是真正类率(召回率的别称)和假正类率(FPR)。FPR是被错误分为正类的负类实例比率,等于1减去真负类率(TNR)
TNR是被正确地分为负类的负类实例比率,也称之为特异度。
ROC绘制的是灵敏度和(1-特异度)的关系图
In [43]:
# 1、计算TPR、FPR
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y_train_0, y_scores)In [44]:
def plot_roc_curve(fpr,tpr,label=None):
    plt.plot(fpr, tpr, linewidth=2,label=label)
    plt.plot([0,1], [0,1], "k--")
    plt.legend(loc="center right", fontsize=12)
    plt.xlabel("FPR", fontsize=16)
    plt.ylabel("TPR", fontsize=16)
    plt.grid(True)
plot_roc_curve(fpr,tpr)
plt.show()auc就是上面ROC曲线的线下面积。完美的分类器ROC_AUC等于1;纯随机分类器的ROC_AUC等于0.5
In [45]:
from sklearn.metrics import roc_auc_score
roc_auc_score(y_train_0, y_scores)Out[45]:
0.9910680354987216ROC曲线和精度/召回率(PR)曲线非常类似,选择经验:当正类非常少见或者我们更加关注假正类而不是假负类,应该选择PR曲线,否则选择ROC曲线
报错:index 1 is out of bounds for axis 1 with size 1
In [46]:
X_train.shapeOut[46]:
(60000, 784)In [47]:
# 解决方案
y_train_0 = y_train_0.reshape(X_train.shape[0], )
y_train_0Out[47]:
array([ True,  True,  True, ..., False, False, False])In [48]:
from sklearn.ensemble import RandomForestClassifier
forest_clf = RandomForestClassifier(random_state=42)
y_probas_forest = cross_val_predict(forest_clf,
                                    X_train,
                                    y_train_0,
                                    cv=3,
                                    method="predict_proba")
y_probas_forestOut[48]:
array([[0.  , 1.  ],
       [0.04, 0.96],
       [0.15, 0.85],
       ...,
       [0.93, 0.07],
       [0.97, 0.03],
       [0.96, 0.04]])使用roc_curve函数来提供分类的概率:
In [49]:
y_scores_forest = y_probas_forest[:,1]
fpr_rf, tpr_rf, thresholds_rf = roc_curve(y_train_0, y_scores_forest)In [50]:
plt.plot(fpr, tpr, "b:", label="SGD")
plot_roc_curve(fpr_rf,tpr_rf,"Random Forest")
plt.legend(loc="lower right")
plt.show()现在我们重新查看ROC-AUC值、精度和召回率,发现都得到了提升:
In [51]:
roc_auc_score(y_train_0,y_scores_forest)  # ROC-AUC值Out[51]:
0.9975104189747056In [52]:
precision_score(y_train_0,y_train_pred)  # 精度Out[52]:
0.78003034064267In [53]:
recall_score(y_train_0,y_train_pred)  # 召回率Out[53]:
0.9549214924869154本文从公开的MNIST数据出发,通过SGD建立一个二元分类器,同时利用交叉验证来评估我们的分类器,以及使用不同的指标(精度、召回率、精度/召回率平衡)、ROC曲线等来比较SGD和RandomForestClassifier不同的模型。