1. 程式人生 > >index 2 is out of bounds for axis 1 with size 2

index 2 is out of bounds for axis 1 with size 2

在使用類似  keras.np_utils.to_categorical 的函式,對標籤轉換成one-hot編碼的時候,要使標籤從0開始。即如果是兩類,設標籤裡面的內容為0和1,不能設定為1和2或其他數字。

比如:

from keras.utils import np_utils
x=[]
x.append(2)
x.append(1)
print(x)
x = np_utils.to_categorical(x, 2)
print(x)

這就是錯誤的

 

from keras.utils import np_utils
x=[]
x.append(0)
x.append(1)
print(x)
x = np_utils.to_categorical(x, 2)
print(x)

這樣才正確