keras将标签转化为one-hot

  • 将标签转换为onehot
from keras.utils import to_categorical
y = numpy.array([0, 1, 5, 2, 3])
onehot = to_categorical(y, num_classes=6)
print(onehot)
# output
[[1. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1.]
 [0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0.]]
  • 将onehot转换回标签(通常在测试时用到)
de_onehot = numpy.argmax(onehot, axis=1)
print(y)
# output
[0 1 5 2 3]
发布了48 篇原创文章 · 获赞 3 · 访问量 2000

猜你喜欢

转载自blog.csdn.net/weixin_43486780/article/details/105120108