Keras编码解码one-hot标签分类

43 篇文章 0 订阅
5 篇文章 1 订阅

AI:Keras编码解码one-hot标签分类,Python

import keras

if __name__ == "__main__":
    print('\none_hot_1')
    one_hot_1 = keras.utils.to_categorical(np.array([0, 1, 2]))
    print(one_hot_1)

    print('\none_hot_2')
    one_hot_2 = keras.utils.to_categorical(np.array([0, 2, 1]))
    print(one_hot_2)

    print('\none_hot_3')
    one_hot_3 = keras.utils.to_categorical(np.array([2, 1, 0]))
    print(one_hot_3)

    print('\none_hot_4')
    one_hot_4 = keras.utils.to_categorical(np.array([0, 1, 2]), num_classes=4)
    print(one_hot_4)

    print('\none_hot_5')
    one_hot_5 = keras.utils.to_categorical(np.array([0, 2, 1]), num_classes=5)
    print(one_hot_5)

    print('\none_hot_6')
    one_hot_6 = keras.utils.to_categorical(np.array([1, 6]))
    print(one_hot_6)

    print('\none_hot_7')
    one_hot_7 = keras.utils.to_categorical(np.array([6]))
    print(one_hot_7)

 

输出:

one_hot_1
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

one_hot_2
[[1. 0. 0.]
 [0. 0. 1.]
 [0. 1. 0.]]

one_hot_3
[[0. 0. 1.]
 [0. 1. 0.]
 [1. 0. 0.]]

one_hot_4
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]

one_hot_5
[[1. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 1. 0. 0. 0.]]

one_hot_6
[[0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1.]]

one_hot_7
[[0. 0. 0. 0. 0. 0. 1.]]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangphil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值