keras.utils.to_categorical方法

2022/8/15 23:32:45

本文主要是介绍keras.utils.to_categorical方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

用法:to_categorical(y, num_classes=None, dtype='float32')

将整型的类别标签转为onehot编码。y为int数组,num_classes为标签类别总数,大于max(y)(标签从0开始的)。

返回:如果num_classes=None,返回len(y) * [max(y)+1](维度,m*n表示m行n列矩阵,下同),否则为len(y) * num_classes。

import keras
 
train=keras.utils.to_categorical([1,3])
print(train)
"""
[[0. 1. 0. 0.]
 [0. 0. 0. 1.]]
"""
train=keras.utils.to_categorical([1,3],num_classes=5)
print(train)
"""
[[0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0.]]
"""

将类别标签转化为one-hot编码,MNIST示例(十个类别: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9):

y = np.array([1,4,5,9,3,5,7,0,3,6])

y = keras.utils.to_categorical(y, num_classes=10)

y 0 1 2 3 4 5 6 7 8 9
1 0 1 0 0 0 0 0 0 0

0

4 0 0 0 0 1 0 0 0 0 0
5 0 0 0 0 0 1 0 0 0 0
9 0 0 0 0 0 0 0 0 0 1
3 0 0 0 1 0 0 0 0 0 0
5 0 0 0 0 0 1 0 0 0 0
7 0 0 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 0 0 0
3 0 0 0 1 0 0 0 0 0 0
6 0 0 0 0 0 0 1 0 0 0

即:

y = [[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]]

 



这篇关于keras.utils.to_categorical方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程