Store multidimensional numpy array slice with newaxis to object

亡梦爱人 提交于 2019-12-02 07:46:32

You can construct the index tuple manually, but NumPy includes a helper for it:

slice_tuple = np.s_[np.newaxis, ..., :, np.newaxis]

Then b[np.newaxis, ..., :, np.newaxis] is equivalent to b[slicetuple].


For reference, constructing the tuple manually would be (np.newaxis, Ellipsis, slice(None), np.newaxis). Also, np.newaxis is None, so (None, Ellipsis, slice(None), None) would be equivalent.


np.s_ can be reimplemented yourself as follows:

class IndexHelper(object):
    def __getitem__(self, arg):
        return arg

s_ = IndexHelper()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!