IndexError:索引5超出了大小为5的数组的范围

时间:2016-11-27 07:54:38

标签: python arrays

我正在使用我创建的数据集。它应该有5个类,它包含500个用于训练的样本和100个用于测试的样本。 我无法执行此操作:

Y_train = np_utils.to_categorical(y_train, num_classes)
Y_test = np_utils.to_categorical(y_test, num_classes)

因为它一直给我:

 Y[i, y[i]] = 1.
IndexError: index 5 is out of bounds for axis 1 with size 5

如果我将类的值更改为6它可以工作,但这不正确我只有5个类。

我需要帮助。

1 个答案:

答案 0 :(得分:1)

在许多编程语言中,数组之类的数据结构索引从0(零)开始。也就是说,如果您的结构包含五个元素,则索引的范围为0到4,而不是1到5.

因此,索引5超出了只有五个元素的结构。