从Numpy 3D阵列保留切片的尺寸 [英] Preserving the dimensions of a slice from a Numpy 3d array
问题描述
我有一个形状为a.shape = (10, 10, 10)
切片时,尺寸会自动squeezed
,即
When slicing, the dimensions are squeezed
automatically i.e.
a[:,:,5].shape = (10, 10)
我想保留维度的数量,但还要确保压缩的维度是显示1的维度.
I'd like to preserve the number of dimensions but also ensure that the dimension that was squeezed is the one that shows 1 i.e.
a[:,:,5].shape = (10, 10, 1)
我曾考虑过重新铸造数组并传递ndmin
,但这只是将额外的维添加到形状元组的开头,而不管切片来自数组a
的何处.
I have thought of re-casting the array and passing ndmin
but that just adds the extra dimensions to the start of the shape tuple regardless of where the slice came from in the array a
.
推荐答案
a[:,:,[5]].shape
# (10,10,1)
a[:,:,5]
是基本切片.
a[:,:,[5]]
是整数的示例数组索引-结合基本切片.使用整数数组索引时,结果形状始终为与(广播的)索引数组形状相同" .由于[5]
(作为数组)的形状为(1,)
,
a[:,:,[5]]
最终形状为(10,10,1)
.
a[:,:,[5]]
is an example of integer array indexing -- combined with basic slicing. When using integer array indexing the resultant shape is always "identical to the (broadcast) indexing array shapes". Since [5]
(as an array) has shape (1,)
,
a[:,:,[5]]
ends up having shape (10,10,1)
.
这篇关于从Numpy 3D阵列保留切片的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!