看代码

复制代码
from keras.utils.np_utils import *

# 类别向量定义
b = [0, 1, 2, 3]
# 调用to_categorical将b按照4个类别来进行转换
b = to_categorical(b, 4)
print(b)
# [[1. 0. 0. 0.]
#  [0. 1. 0. 0.]
#  [0. 0. 1. 0.]
#  [0. 0. 0. 1.]]

# 注意,生成双精度浮点,一般我们需要单精度
print(b.dtype)      # float64
b = b.astype('float32')
print(b.dtype)      # float32
复制代码

将 数值label 变成 二进制编码