python - 使用 [ :] syntax 的 numpy 维度问题

标签 python arrays numpy syntax indexing

我有一个问题...

我使用 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/

相关文章:

python - 如何装饰 Python 3 中的部分应用函数?

带有列表的 Python Deque appendleft

python - 在 Airflow 1.9.0 中填充 `dag_id` 的 `ExternalTaskSensor` 的最佳实践是什么?

python - 检查大矩阵是否是python中的对角矩阵

python - 快速迭代多维 numpy 数组中的向量

python - 获取有序 CSV 文件中高于特定 unix 时间戳的行号的有效方法

java - 在具有空值的情况下按列对二维 Java 字符串数组进行排序

javascript - 访问和比较 JSON 值

java - 从字符串创建 JSONArray Android - Java

python - 遍历python中的字典列表