本文介绍了冒号,无,numpy数组索引器中的slice(None)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关于a = np.arange(24).reshape(2,3,4)
a[0,:,1]
或a[0,slice(None),1]
输出array([1, 5, 9])
,而a[0,None,1]
给出array([[4, 5, 6, 7]])
某人可以解释后者吗?
推荐答案
使用原始None
(不在slice
中)与使用,但它只是一个别名.
Using a raw None
(not in slice
) is the same thing as using np.newaxis
, of which it is but an alias.
在您的情况下:
-
a[0,None,1]
就像a[0,np.newaxis,1]
,因此输出 - 而
slice(None)
就像什么都没切",这就是为什么a[0,:,1]
与a[0,slice(None),1]
相同的原因.请参阅numpy的索引文档.
a[0,None,1]
is like a[0,np.newaxis,1]
, hence the output- whereas
slice(None)
is like "slice nothing", which is whya[0,:,1]
is the same asa[0,slice(None),1]
. See numpy's Indexing doc.
这篇关于冒号,无,numpy数组索引器中的slice(None)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!