冒号,无,numpy数组索引器中的slice(None) [英] Colon, None, slice(None) in numpy array indexers

查看:816
本文介绍了冒号,无,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] is like a[0,np.newaxis,1], hence the output
  • whereas slice(None) is like "slice nothing", which is why a[0,:,1] is the same as a[0,slice(None),1]. See numpy's Indexing doc.

这篇关于冒号,无,numpy数组索引器中的slice(None)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆