to_categorical将类别向量转化为one-hot形式
from keras.utils.np_utils import *
#类别向量
num = [0,1,2,3,4,5,6,7,8]
#调用to_categorical将num按照9个类别来进行转换
num_onehot = to_categorical(num, 9)
print(num_onehot)
结果
[[1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 1.]]