Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
Arrays 重新格式化数组错误:**Indexer错误:元组索引超出范围_Arrays_Python 2.7_Numpy_Reshape_Flatten - Fatal编程技术网

Arrays 重新格式化数组错误:**Indexer错误:元组索引超出范围

Arrays 重新格式化数组错误:**Indexer错误:元组索引超出范围,arrays,python-2.7,numpy,reshape,flatten,Arrays,Python 2.7,Numpy,Reshape,Flatten,我有一个或多个名为my_data的数组,以便: 维度(my_数据)=[96,18,36,36,3]类型 但是,my_data.shape返回(96,),但我希望得到(96,18,36,36,3)。之后我尝试了my_data[0]。shape返回(18,)而不是(18,36,36,3),最后my_data[0][0]。shape返回(36,36,3) 我想知道为什么我没有得到(96,18,36,36,3)的形状 我为什么需要它? my_data=my_data.reshape(my_data.sh

我有一个或多个名为my_data的数组,以便: 维度(my_数据)=[96,18,36,36,3]类型

但是,
my_data.shape
返回
(96,)
,但我希望得到(96,18,36,36,3)。之后我尝试了
my_data[0]。shape
返回
(18,)
而不是(18,36,36,3),最后
my_data[0][0]。shape
返回(36,36,3)

我想知道为什么我没有得到(96,18,36,36,3)的形状

我为什么需要它?

my_data=my_data.reshape(my_data.shape[0],my_data.shape[1],my_data.shape[2]*my_data.shape[3]*my_data.shape[4])
我想将我的_数据重塑为
(96,18,36*36*3)

我试过什么?

my_data=my_data.reshape(my_data.shape[0],my_data.shape[1],my_data.shape[2]*my_data.shape[3]*my_data.shape[4])
我得到以下错误:

 *** IndexError: tuple index out of range

我甚至不知道你是如何得到一个
np.array
中包含
np.array
的数组的。但是,如果数据对齐,则simple casting应为您解决此问题,例如:

import numpy as np

x = [np.random.randint(1, 100, (2, 3, 4)) for _ in range(2 * 3)]
x = np.array(x)
x.shape
# (6, 2, 3, 4)
如果数据未对齐,应首先对齐:

import numpy as np
x = [np.random.randint(1, 100, (2, 3, np.random.randint(1, 5))) for _ in range(2 * 3)]
print([y.shape for y in x])
# [(2, 3, 2), (2, 3, 1), (2, 3, 1), (2, 3, 4), (2, 3, 1), (2, 3, 1)]
# x = np.array(x) will cause ValueError...


def zero_padding(arr, shape):
    result = np.zeros(shape, dtype=arr.dtype)
    mask = tuple(slice(None, d) for d in arr.shape)
    result[mask] = arr
    return result


x = [zero_padding(y, (2, 3, 4)) for y in x]
x = np.array(x)
x.shape
# (6, 2, 3, 4)
注意:NumPy的
pad()
或's
num.reframe()
提供了此
zero\u padding()
的更复杂版本


免责声明:我是FlyingCircus的主要作者。

类型
没有告诉我们多少。如果它具有
shape
属性,则很可能是
ndarray
(或子类)<代码>数据类型更具诊断性。如果
shape
是(96,)并且您希望得到5d,那么数据类型可能是
object
。使用该
形状
,只能使用
[0]
对其进行索引;这是一个1元素元组。看起来您有一个96元素的对象数据类型数组。其中一个元素也是包含18个元素的对象数据类型。其中之一是(36,36,3)数字数组。数组是如何生成的?它可能有不同长度的子阵列。当然,我刚刚检查过,有些子阵列有不同的长度。当压平的时候,应该是3888=36*36*3,但是我发现其中一些是35642808。有没有什么有效的方法可以0-pad所有与3888不同的子阵列?非常感谢。