1

I'm working with a problem using numpy arrays and I hit a roadblock, basically I have two arrays, one with a 2D numpy array and the other is a 1D numpy which represents some index of the 2D array, what I need is to use pairs of this indexes to extract a 2D numpy array from the original 2D array, I did something, but I'm sure it can be better, so I'm asking for advice. Here is my code:

import numpy as np
import itertools

x = np.arange(25).reshape(5, 5) #Original Array

#x = [[ 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]]

y = np.array([0, 2, 4]) #Indexes

idx = list(itertools.product(y, repeat = 2)) #This create a combination of the indexes to act as my coordinates from the array

#idx = [(0, 0), (0, 2), (0, 4), (2, 0), (2, 2), (2, 4), (4, 0), (4, 2), (4, 4)]

newarray = np.array([x[i] for i in idx]).reshape(3, 3) #This uses the tuples from before to extract the values of the original array

#newarray = [[ 0  2  4]
#            [10 12 14]             #The extracted array
#            [20 22 24]]

So it works, but I think there is a lot to improve, for example, in the final step I use a list comprehesion and then a numpy array, and then a reshape, also I'm not sure if it's okay to create all the combinations of the index array maybe there is a easier way, so any advice will be appreciated, thank you!

3 Answers 3

1
 x[::2, ::2]

will select every other row and column

For a less regular pattern try

x[y[:,None], y]

which uses advanced indexing

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

2 Comments

I gotta check advaced indexing, not sure what the None thing is doing here, anyway, thank you so much!
Here None is np.newaxis, making a (n,1) array. In Advanced Indexing, the indexing arrays are broadcast against each other. So a (n,1) with (m,) indexes a (n,m) space. A (m,) with (m,) indexes a (m,).
1

Numpy has some sophisticated indexing options. Also remember that reshape is free; never be afraid to reshape.

import numpy as np
import itertools

x = np.arange(25).reshape(5, 5) #Original Array
y = [0, 2, 4]
idx = list(itertools.product(y, repeat = 2)) #This create a combination of the indexes to act as my coordinates from the array
idx0 = [k[0] for k in idx]
idx1 = [k[1] for k in idx]
print(idx)
newarray = x[idx0,idx1].reshape((3,3))
print(newarray)

Output:

[(0, 0), (0, 2), (0, 4), (2, 0), (2, 2), (2, 4), (4, 0), (4, 2), (4, 4)]
[[ 0  2  4]
 [10 12 14]
 [20 22 24]]

Comments

1

I think what you hate is the for keyword (like me). And in fact you don't need itertools.
So my answer would be:

import numpy as np

x = np.arange(25).reshape(5, 5)
y = np.array([0, 2, 4])

ny = y.size
i = y.reshape(ny, 1)
j = y.repeat(ny).reshape(ny, ny).T

print(x[i, j])

Output:

[[ 0  2  4]
 [10 12 14]
 [20 22 24]]

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.