• 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]

  • 相关阅读:
    对于Dubbo一些面试题自己的答案
    序列化和反序列化的简单理解
    学习Spring-Session+Redis实现session共享
    Java中的String,StringBuilder,StringBuffer三者的区别
    个人对数据结构的理解和总结
    LeetCode 101. Symmetric Tree
    LeetCode 100. Same Tree
    LeetCode 88. Merge Sorted Array
    LeetCode 83. Remove Duplicates from Sorted List
    LeetCode 70. Climbing Stairs
  • 原文地址:https://www.cnblogs.com/huanghanyu/p/13173924.html
Copyright © 2020-2023  润新知