if slice 2d array set of coordinates
>>> test = np.reshape(np.arange(40),(5,8)) >>> coords = np.array((1,3,4)) >>> slice = test[:, coords] then slice has shape expect
>>> slice.shape (5, 3) but if repeat 3d array
>>> test = np.reshape(np.arange(80),(2,5,8)) >>> slice = test[0, :, coords] then shape now
>>> slice.shape (3, 5) is there reason these different? separating indices returns shape expect
>>> slice = test[0][:][coords] >>> slice.shape (5, 3) why these views have different shapes?
slice = test[0, :, coords] is simple indexing, in effect saying "take 0th element of first coordinate, of second coordinate, , [1,3,4] of third coordinate". or more precisely, take coordinates (0,whatever,1) , make our first row, (0,whatever,2) , make our second row, , (0,whatever,3) , make our third row. there 5 whatevers, end (3,5).
the second example gave this:
slice = test[0][:][coords] in case you're looking @ (5,8) array, , taking 1st, 3rd , 4th elements, 1st, 3rd , 4th rows, end (5,3) array.
edit discuss 2d case:
in 2d case, where:
>>> test = np.reshape(np.arange(40),(5,8)) >>> test array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30, 31], [32, 33, 34, 35, 36, 37, 38, 39]]) the behaviour similar.
case 1:
>>> test[:,[1,3,4]] array([[ 1, 3, 4], [ 9, 11, 12], [17, 19, 20], [25, 27, 28], [33, 35, 36]]) is selecting columns 1,3, , 4.
case 2:
>>> test[:][[1,3,4]] array([[ 8, 9, 10, 11, 12, 13, 14, 15], [24, 25, 26, 27, 28, 29, 30, 31], [32, 33, 34, 35, 36, 37, 38, 39]]) is taking 1st, 3rd , 4th element of array, rows.
Comments
Post a Comment