in basic slicing of numpy array http://docs.scipy.org/doc/numpy-1.5.x/reference/arrays.indexing.html#basic-slicing,
found following rule not work example shown below.
rule: assume n number of elements in dimension being sliced. then, if not given defaults 0 k > 0 , n k < 0 . if j not given defaults n k > 0 , -1 k < 0 . if k not given defaults 1. note :: same : , means select indices along axis.
what understood : can prioritized top down :
a)if k not given defaults 1.
b)if j not given defaults n k > 0 , -1 k < 0 .
c)if not given defaults 0 k > 0 , n k < 0.
now let's see example. doing take 3d array , print bottom . layer largest index comes first , smaller one. please @ code better understanding.
import numpy np b= np.arange(24).reshape(2,3,4) print "here input :" print b print print "here desired output :" print b[::-1 , :: ,::] print print "here want desired output different way using above rule :" print b[2:-1:-1 , :: , ::] output :
here input : [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] here desired output : [[[12 13 14 15] [16 17 18 19] [20 21 22 23]] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]] here want desired output different way using above rule : [] is b[::-1 , :: ,::] not same b[2:-1:-1 , :: , ::] above rule?
the document right, doesn't means can use calculated start, end index in slice object again. tells logic calculate start & end index. use calculated index, need generate index range():
here example:
import numpy np s = slice(none, none, -1) t = np.array([1, 2, 3, 4]) s.indices(len(t)) outputs:
(3, -1, -1) so (start, stop, stride) of [::-1] 4 element array (3, -1, -1), t[3:-1:-1] empty. (start, stop, stride) range(), so, can use t[range(3,-1,-1)] [4, 3, 2, 1].
Comments
Post a Comment