我有一个问题...
我使用 shape=(4,128,256,256)
创建了一个 numpy.array
。
如果我打印出以下内容:
print shape(x[:][3][1][:])
输出是 shape=(256,256)
,而不是我预期的 (4,256)
...
还有声明
print x[:][4][1][1]
产生错误:索引超出范围
经过一番尝试和错误后,在我看来,如果后面跟着另一个具有离散值的参数,则 [:] 不起作用...
我通过使用循环解决了当前的问题,但对于 future 我想了解我做错了什么......
感谢您的帮助...
最佳答案
要得到你想要的东西,你必须采取正确的措施:
x[:, 3, 1, :].shape => (4, 256)
numpy
数组不是标准列表
如果您执行x[:][3][1][:]
,您实际上会执行以下操作:
x1 = x[:] # get the whole array
x2 = x1[3] # get the fourth element along the first dimension
x2.shape => (128, 256, 256)
x3 = x2[1] # get the second element along the first dimension of `x2`
x3.shape => (256, 256)
x3[:] # get all `x3`
有关索引的更多说明,请参阅 the numpy documentation
<小时/>关于执行时出现的错误
x[:][4][1][1]
您会得到索引越界
,因为x[:]
是整个数组,并且第一个维度是4,所以x[:][4 ]
不存在
关于python - 使用 [ :] syntax 的 numpy 维度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20069835/