Python机器学习(七十五)Keras 预处理分类标签

让我们看看分类标签数据:

print (y_train.shape)
# (60000,)
print (y_train[:10])
# [5 0 4 1 9 2 1 3 1 4]

可以看到,这是一个一位数组,包含了训练数据集对应的标签。

在Keras中,模型训练时,需要把分类标签数据转换为类似位图的矩阵,例如y_train前面10个值是:

[5 0 4 1 9 2 1 3 1 4]

表示为矩阵(对应位置标为1)

[[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] # 5 对应位置标为1
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] # 0
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] # 4
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] # 1
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.] # 9 以此类推
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]

可以使用to_categorical来转换标签数据。

# 将一维类数组转换为10维分类矩阵
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)

打印Y_train矩阵的形状

print (Y_train.shape)
# (60000, 10)

打印Y_train矩阵的前面10条内容:

>>> print (Y_train[:10])
[[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]

可以看到对应 [5 0 4 1 9 2 1 3 1 4]

上一篇:15 手写数字识别


下一篇:手写数字识别-小数据集