Keras編碼解碼one-hot標籤分類

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

 

發佈了1033 篇原創文章 · 獲贊 999 · 訪問量 340萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章