gpt4 book ai didi

python - 这个 Slicing 和 Ellipse 索引操作会产生什么结果?

转载 作者:行者123 更新时间:2023-11-28 22:52:21 24 4
gpt4 key购买 nike

我尝试使用 ndarrays 了解这段代码会发生什么:

max_evecs = evecs[..., :, 0]

其中 evecs 是 ndarray 类型。到目前为止,我知道 ':' 是一个切片运算符,而 '...' 是一个所谓的椭圆。到目前为止,':' 表示所有元素,'...' 表示需要的数量。我调查了具有以下形状的数组:

>>> max_evecs.shape
(128, 128, 72)
>>> evecs.shape
(128, 128, 72, 3)

我可以想象,它试图将 3D 数组转换为三元组 (x,y,z) 的数组。但我不确定。

对于关心的人:它来自 dipy 软件包:函数 quantize_evecs inside https://github.com/nipy/dipy/blob/ff75b192f694cdb62cc11310159cdb652ce62073/dipy/reconst/dti.py 1663 年左右的线

最佳答案

来自 http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

Ellipsis expand to the number of : objects needed to make a selection tuple of the same length as x.ndim. Only the first ellipsis is expanded, any others are interpreted as :.

因此在您的示例中,evecsndim 等于 4,以下是等效的:

evecs[..., :, 0]
evecs[:, :, :, 0]

以交互方式尝试事物以感受它也很有帮助。对于一些简单的数据,例如 a = np.random.rand(3, 3, 2),尝试打印出 a 和它的各种切片,如 a [..., 0]a[1, ...] 以及类似的,看看它们是如何相关的。

关于python - 这个 Slicing 和 Ellipse 索引操作会产生什么结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20540892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com