pythonpandasnumpyrandomarray-indexing

How can numpy array indexing techniques can give different outputs for same input code?


#Code

import numpy as np    
np.random.seed(124)
x_2d = np.random.randint(1,50,(3,4,5))
print("I am getting wrong output => {}".format(x_2d[0][:][1]))
print("This is what I want => {} ".format(x_2d[0,:,1]))

# Code Ended

# Output for above code

I am getting wrong output => [42  1 21 29 15]
This is what I want => [29  1 22 49] 

I am new to NumPy so I was just experimenting on numpy array selection techniques. I came to know that we can use square bracket method or comma method. But I encountered a problem. I am trying to extract column index 1 of array 0. But I am getting different outputs when I use both techniques. I have attached code snippets and outputs. Can anyone guide me where did I go wrong ?


Solution

  • When you call x_2d[0][:][1], first you get the first matrix x_2d[0]:

    >>> first = x_2d[0]
    >>> first
    array([[15, 29, 18,  8,  3],
           [42,  1, 21, 29, 15],
           [22, 22, 28, 10, 31],
           [47, 49, 41, 10, 10]])
    

    When you call first[:] you gonna receive exactly first, since you asking for all lines in first:

    >>> second = first[:]
    >>> second
    array([[15, 29, 18,  8,  3],
           [42,  1, 21, 29, 15],
           [22, 22, 28, 10, 31],
           [47, 49, 41, 10, 10]])
    

    So when you get second[1] you get the line of index 1

    >>> third = second[1]
    >>> third
    array([42,  1, 21, 29, 15])
    

    However when you ask for x_2d[0,:,1], numpy interprets it as:

    "From matrix 0 give me column 1 from the matrix composed by lines 0,1,2,3)"

    So if you ask for x_2d[1,0:2,3] the result will be [18, 2].

    Conclusion: numpy does not interpret x[0,:,1] in the same way of x[0][:][1]. You can read more in the NumPy documentation here.