numpyArr[:,:,:,c] 和 numpyArr[...,c] 有什么区别?

What is the difference between numpyArr[:,:,:,c] and numpyArr[...,c]?

我一直在 coursera 上学习深度学习课程。在我做作业时,我在 github 上看到了一段代码。

1. numpyArr[...,c]
2. numpyArr[:,:,:,c]

这些切片方法有什么区别?

如果两个数组都是 4 维,则结果没有差异。但是,如果您并不真正关心维数,则使用省略号 (...) 仅表示任意维数。所以第一个版本的意思是:

"get all dimensions but from the last one (whatever the last is) only entry c "

第二个表示

“获取完整的维度 0、1、2,并且仅来自维度 3 的条目 c

4 维数组相同,但 5 维数组不同。

对于具有多个维度的数组,可能会更有趣:

arr = np.random.uniform(size=(3, 3, 3, 3, 3))
print(arr[1, ..., 2, 3].shape)

这意味着:获取第一个维度上的第二个条目以及最后两个维度的那个条目 2 和 3,以及介于两者之间的所有内容。

Some years ago, this has already been asked,但需要知道 ... 是省略号。