Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器错误:索引5超出大小为5的数组的界限_Python_Arrays - Fatal编程技术网

Python 索引器错误:索引5超出大小为5的数组的界限

Python 索引器错误:索引5超出大小为5的数组的界限,python,arrays,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 如

我正在使用我创建的数据集。它应该有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个类


请告诉我需要帮助。

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


因此,对于只有五个元素的结构,索引5是超出范围的。

如果你有5个类,它们将在数组元素0-4中,因此5将超出范围。Mybe你的愤怒是从0到4。给出的错误不在我的代码中,我的意思是我的代码中没有Y[I,Y[I]=1?我只是想告诉大家,我有一个示例100 testign,它们应该被分为5个类,所以我将类的数量设置为5,即使我将其设置为4,它也会给我相同的错误,只有它与6一起工作!!如果错误不在您显示的代码中,我们将找不到它。Y[i,Y[i]]=1。索引器:索引5超出轴1的范围,大小为5。这部分表示屏幕上显示的错误,调试后,我发现在代码Y_train=np_utils.to_category(Y_train,num_classes)Y_test=np_utils.to_category(Y_test,num_classes)中的这些行出现错误