Numpy Array Documentation Cutting Rule

In the base slicing of the numpy array http://docs.scipy.org/doc/numpy-1.5.x/reference/arrays.indexing.html#basic-slicing ,
I found the following rule, which does not work for the example shown below.

Rule: Suppose n is the number of elements in the dimension of the circle. Then, if I am not specified, it defaults to 0 for k> 0 and n for k <0. If j is not specified, then by default n for k> 0 and -1 for k <0. If k is not specified, the value defaults to 1. Note that :: is the same as: and means selecting all indexes along this axis.

What I understood: This may be top-down priority:

a) If k is not set, it defaults to 1.
b) If j is not set, it defaults to n for k> 0 and -1 for k <0.
c) if I do not set default 0 for k> 0 and n for k <0.

Now let's look at an example. Basically what I do is take a 3D array and print it from bottom to top. First, the layer with the highest index begins, and then the smaller. Please see the code for a better understanding.

import numpy as np

b= np.arange(24).reshape(2,3,4)
print "Here is the input :" 
print b
print

print "Here is what is desired output :"
print b[::-1 , :: ,::]
print 

print "Here is what I want to get desired output by a different way using above rule :"
print b[2:-1:-1 , :: , ::]

Exit:

Here is the 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 is what is 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 is what I want to get desired output by a different way using above rule :
[]

Is it b[::-1 , :: ,::]not the same as b[2:-1:-1 , :: , ::]the rule above?

+4
source share
2 answers

, , slice. . , range():

:

import numpy as np
s = slice(None, None, -1)
t = np.array([1, 2, 3, 4])
s.indices(len(t))

:

(3, -1, -1)

(, , ) [::-1] (3, -1, -1), t[3:-1:-1] . (Start, stop, stride) range(). , t[range(3,-1,-1)], [4, 3, 2, 1].

+2

.

:

In [437]: np.arange(10)[::-1]
Out[437]: array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

-

In [438]: np.arange(10)[10::-1]
Out[438]: array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

-1 ,

In [439]: np.arange(10)[10:-1:-1]
Out[439]: array([], dtype=int32)

-2, ; , , -2 .

In [440]: np.arange(10)[10:-2:-1]
Out[440]: array([9])

4 :

In [441]: np.arange(10)[10:-5:-1]
Out[441]: array([9, 8, 7, 6])

, :

In [442]: np.arange(10)[10:-10:-1]
Out[442]: array([9, 8, 7, 6, 5, 4, 3, 2, 1])

0:

In [445]: np.arange(10)[10:0:-1]
Out[445]: array([9, 8, 7, 6, 5, 4, 3, 2, 1])

None, :

In [446]: np.arange(10)[10:None:-1]
Out[446]: array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
+1

Source: https://habr.com/ru/post/1626483/


All Articles