IndexError: index 6 is out of bounds for axis 1 with size 6

错误:

Using TensorFlow backend.
Traceback (most recent call last):
  File "E:/Python3Doc/APIdocs/kerasMy/to-cat.py", line 7, in
    y_train_use = np_utils.to_categorical(y_train, num_classes=nb_classes)
  File "C:\Program Files\Anaconda3\lib\site-packages\keras\utils\np_utils.py", line 31, in to_categorical
    categorical[np.arange(n), y] = 1

IndexError: index 6 is out of bounds for axis 1 with size 6


# coding: utf-8

from keras.utils import np_utils

nb_classes = 6
y_train = [1, 3, 5, 2, 6, 4, 1]
y_train_use = np_utils.to_categorical(y_train, num_classes=nb_classes)

print(y_train_use)

修改:nb_classes=7,即类标号要从0开始!!!!!!!!!!

如下:

# coding: utf-8

from keras.utils import np_utils

nb_classes = 7
y_train = [1, 3, 5, 2, 6, 4, 1]
y_train_use = np_utils.to_categorical(y_train, num_classes=nb_classes)

print(y_train_use)

结果:

[[ 0.  1.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  1.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.]
 [ 0.  0.  0.  0.  1.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  0.]]

你可能感兴趣的:(自然语言处理,文本挖掘)