How to pass multiple index arrays in Ipython

ipython

#1

I have created a array using the function np.arrange and reshape after creating the array. Now, I want to create a subset of array but the result I am getting is 1-D array instead of a square array.

In [105]: arr = np.arange(32).reshape((8, 4))
    In [106]: arr
    Out[106]:
    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]])
    In [107]: arr[[1, 5, 7, 2], [0, 3, 1, 2]]
    Out[107]: array([ 4, 23, 29, 10])

#2

@sid100158- I think you want to generate square matrix instead of 1-D array
Basically you are selecting the coordinates as (1,0),(5,3),(7,1),(2,2) that’s why you are getting 1-D array

you can use two methods for getting a square matrix

the first one is the use of semicolon for selecting the column

  arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]
    array([[ 4, 7, 5, 6],
    [20, 23, 21, 22],
    [28, 31, 29, 30],
    [ 8, 11, 9, 10]]) 

Second one is use of np.ix_ function

np.ix_ - which converts 1-D array to square matrix

arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])]
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]]).

Hope this helps!

Regards,
Hinduja