AI:Keras编码解码one-hot标签分类,Python
import keras
if __name__ == "__main__":
print('\none_hot_1')
one_hot_1 = keras.utils.to_categorical(np.array([0, 1, 2]))
print(one_hot_1)
print('\none_hot_2')
one_hot_2 = keras.utils.to_categorical(np.array([0, 2, 1]))
print(one_hot_2)
print('\none_hot_3')
one_hot_3 = keras.utils.to_categorical(np.array([2, 1, 0]))
print(one_hot_3)
print('\none_hot_4')
one_hot_4 = keras.utils.to_categorical(np.array([0, 1, 2]), num_classes=4)
print(one_hot_4)
print('\none_hot_5')
one_hot_5 = keras.utils.to_categorical(np.array([0, 2, 1]), num_classes=5)
print(one_hot_5)
print('\none_hot_6')
one_hot_6 = keras.utils.to_categorical(np.array([1, 6]))
print(one_hot_6)
print('\none_hot_7')
one_hot_7 = keras.utils.to_categorical(np.array([6]))
print(one_hot_7)
输出:
one_hot_1
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
one_hot_2
[[1. 0. 0.]
[0. 0. 1.]
[0. 1. 0.]]
one_hot_3
[[0. 0. 1.]
[0. 1. 0.]
[1. 0. 0.]]
one_hot_4
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]
one_hot_5
[[1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 0. 0. 0.]]
one_hot_6
[[0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1.]]
one_hot_7
[[0. 0. 0. 0. 0. 0. 1.]]