0

This little python code:

   >>> a=np.random.rand(3,4,5)
   >>> a[0][:][3]
   >>> a[0,:,3]

Produces the results:

array([[[ 0.19080354,  0.45701919,  0.17411363,  0.45117827,  0.10413359],
            [ 0.86430848,  0.81831987,  0.27604238,  0.25587538,  0.72733844],
            [ 0.42065355,  0.63994284,  0.64540483,  0.55639512,  0.4455423 ],
            [ 0.04778727,  0.53506934,  0.79615599,  0.24200543,  0.82332594]],
           [[ 0.36535239,  0.5973006 ,  0.71075267,  0.16814739,  0.26409851],
            [ 0.85557313,  0.54190805,  0.65531428,  0.80448208,  0.54959253],
            [ 0.62112884,  0.9159606 ,  0.10186144,  0.14956198,  0.38026561],
            [ 0.70577261,  0.02682898,  0.04136858,  0.15603152,  0.47125989]],
           [[ 0.72864857,  0.09365008,  0.84137507,  0.43887926,  0.26616441],
            [ 0.31022073,  0.54251517,  0.30635049,  0.36270005,  0.85149399],
            [ 0.39371669,  0.38230285,  0.77115029,  0.22647156,  0.57128166],
            [ 0.54906932,  0.87058929,  0.72157733,  0.79480009,  0.033705  ]]])
array([ 0.04778727,  0.53506934,  0.79615599,  0.24200543,  0.82332594])
array([ 0.45117827,  0.25587538,  0.55639512,  0.24200543])

Why do the last two lines, a[0][:][3] and a[0,:,3], not return the same slice of the array? I expected them both to return what came from a[0,:,3].

1
  • 1
    a[0].T[:][3] or easier a[0].T[3] this will give you the same output as a[0,:,3] Commented Jun 8, 2018 at 13:43

3 Answers 3

2

Each [] is evaluated separately by the Python interpreter, e.g.

In [117]: a=np.random.rand(3,4,5)
In [118]: a[0]
Out[118]: 
array([[0.98688694, 0.77224477, 0.19871568, 0.00552212, 0.81546143],
       [0.70685734, 0.72900717, 0.77127035, 0.07404465, 0.35846573],
       [0.11586906, 0.86310343, 0.62329813, 0.33089802, 0.06355835],
       [0.31098232, 0.32518332, 0.72960618, 0.63755747, 0.88721274]])
In [119]: _[:]
Out[119]: 
array([[0.98688694, 0.77224477, 0.19871568, 0.00552212, 0.81546143],
       [0.70685734, 0.72900717, 0.77127035, 0.07404465, 0.35846573],
       [0.11586906, 0.86310343, 0.62329813, 0.33089802, 0.06355835],
       [0.31098232, 0.32518332, 0.72960618, 0.63755747, 0.88721274]])
In [120]: _[3]
Out[120]: array([0.31098232, 0.32518332, 0.72960618, 0.63755747, 0.88721274])

Making the trailing slices explicit (for clarity to us humans):

In [121]: a[0,:,:][:,:][3,:]
Out[121]: array([0.31098232, 0.32518332, 0.72960618, 0.63755747, 0.88721274])

The middle [:] does not select a dimension from the original a. It operates on the result of a[0,:,:], and does nothing (except make a new array of the same shape and data). The last [3] does not select from the third dimension of a - it selects from the first dimension of the array it got from the [:] step. Note it returns a shape (5,) array, the size of the last dimension of a. a[0,3,:] produces the same thing.

This on the other hand is handled entirely by the numpy indexing, and deals with all 3 dimensions at once:

In [122]: a[0,:,3]
Out[122]: array([0.00552212, 0.07404465, 0.33089802, 0.63755747])

This returns a shape (4,), the middle dimension of a. a[0,:,:][:,3] produces the same thing.

The key point is that numpy operates within the Python interpreter; it does not change Python syntax.

Sign up to request clarification or add additional context in comments.

Comments

2

a[0][:][3] says: take 0th (sub)array, third row, and is equivalent to a[0][3]

a[0,:,3] says: take 0th (sub)array, all rows, third element (column)

Comments

1

That's because [:] doesn't make any change to the array, so a[0][:][3] is same as a[0][3] which equals to a[0, 3, :]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.