其他分享
首页 > 其他分享> > keras.utils.to_categorical方法

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

 

标签:num,keras,train,categorical,classes,utils
来源: https://www.cnblogs.com/yadira/p/16530031.html