Python multi-dimensional notation transpose automatically

后端 未结 1 822
梦毁少年i
梦毁少年i 2020-12-22 06:44

I have the following minimal example:

a = np.zeros((5,5,5))
a[1,1,:] = [1,1,1,1,1]
print(a[1,:,range(4)])

I would expect as output an array

相关标签:
1条回答
  • 2020-12-22 07:13

    This is an example of mixed basic and advanced indexing, as discussed in https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing

    The slice dimension has been appended to the end.

    With one scalar index this is a marginal case for the ambiguity described there. It's been discussed in previous SO questions and one or more bug/issues.

    Numpy sub-array assignment with advanced, mixed indexing

    In this case you can replace the range with a slice, and get the expected order:

    In [215]: a[1,:,range(4)].shape
    Out[215]: (4, 5)               # slice dimension last
    In [216]: a[1,:,:4].shape
    Out[216]: (5, 4)
    In [219]: a[1][:,[0,1,3]].shape
    Out[219]: (5, 3)
    
    0 讨论(0)
提交回复
热议问题