Python Numpy索引切片不丢失维度信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3551242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Numpy index slice without losing dimension information
提问by mindmatters
I'm using numpy and want to index a row without losing the dimension information.
我正在使用 numpy 并希望在不丢失维度信息的情况下索引一行。
import numpy as np
X = np.zeros((100,10))
X.shape # >> (100, 10)
xslice = X[10,:]
xslice.shape # >> (10,)
In this example xslice is now 1 dimension, but I want it to be (1,10). In R, I would use X[10,:,drop=F]. Is there something similar in numpy. I couldn't find it in the documentation and didn't see a similar question asked.
在这个例子中,xslice 现在是一维,但我希望它是 (1,10)。在 R 中,我会使用 X[10,:,drop=F]。numpy.js 中有类似的东西吗?我在文档中找不到它,也没有看到类似的问题。
Thanks!
谢谢!
采纳答案by Joe Kington
It's probably easiest to do x[None, 10, :]or equivalently (but more readable) x[np.newaxis, 10, :].
这可能是最容易做的x[None, 10, :]或等效的(但更具可读性)x[np.newaxis, 10, :]。
As far as why it's not the default, personally, I find that constantly having arrays with singleton dimensions gets annoying very quickly. I'd guess the numpy devs felt the same way.
至于为什么它不是默认值,就我个人而言,我发现不断拥有具有单例维度的数组很快就会变得烦人。我猜麻木的开发者也有同样的感觉。
Also, numpy handle broadcasting arrays very well, so there's usually little reason to retain the dimension of the array the slice came from. If you did, then things like:
此外,numpy 可以很好地处理广播数组,因此通常没有理由保留切片来自的数组的维度。如果你这样做了,那么事情就像:
a = np.zeros((100,100,10))
b = np.zeros(100,10)
a[0,:,:] = b
either wouldn't work or would be much more difficult to implement.
要么不起作用,要么实施起来要困难得多。
(Or at least that's my guess at the numpy dev's reasoning behind dropping dimension info when slicing)
(或者至少这是我对 numpy dev 在切片时删除维度信息的推理的猜测)
回答by mindmatters
I found a few reasonable solutions.
我找到了一些合理的解决方案。
1) use numpy.take(X,[10],0)
1) 使用 numpy.take(X,[10],0)
2) use this strange indexing X[10:11:, :]
2)使用这个奇怪的索引 X[10:11:, :]
Ideally, this should be the default. I never understood why dimensions are ever dropped. But that's a discussion for numpy...
理想情况下,这应该是默认值。我一直不明白为什么尺寸会下降。但这是对 numpy 的讨论...
回答by gnebehay
Another solution is to do
另一个解决方案是做
X[[10],:]
or
或者
I = array([10])
X[I,:]
The dimensionality of an array is preserved when indexing is performed by a list (or an array) of indexes. This is nice because it leaves you with the choice between keeping the dimension and squeezing.
当索引由索引列表(或数组)执行时,数组的维数被保留。这很好,因为它让您可以在保持尺寸和挤压之间做出选择。
回答by Andrew Schwartz
Here's an alternative I like better. Instead of indexing with a single number, index with a range. That is, use X[10:11,:]. (Note that 10:11does not include 11).
这是我更喜欢的替代方案。不是用单个数字索引,而是用一个范围索引。也就是说,使用X[10:11,:]. (注意10:11不包括11)。
import numpy as np
X = np.zeros((100,10))
X.shape # >> (100, 10)
xslice = X[10:11,:]
xslice.shape # >> (1,10)
This makes it easy to understand with more dimensions too, no Nonejuggling and figuring out which axis to use which index. Also no need to do extra bookkeeping regarding array size, just i:i+1for any ithat you would have used in regular indexing.
这也使得使用更多维度更容易理解,无需None杂耍和找出使用哪个索引的轴。也无需对数组大小进行额外的簿记,只需i:i+1为i您在常规索引中使用的任何内容进行记录即可。
b = np.ones((2, 3, 4))
b.shape # >> (2, 3, 4)
b[1:2,:,:].shape # >> (1, 3, 4)
b[:, 2:3, :].shape . # >> (2, 1, 4)
回答by leilu
To add to the solution involving indexing by lists or arraysby gnebehay, it is also possible to use tuples:
要添加到涉及gnebehay按列表或数组索引的解决方案中,还可以使用元组:
X[(10,),:]
回答by Jthorpe
This is especially annoying if you're indexing by an array that might be length 1 at runtime. For that case, there's np.ix_:
如果您在运行时按长度可能为 1 的数组进行索引,这尤其令人讨厌。对于这种情况,有np.ix_:
some_array[np.ix_(row_index,column_index)]

