Stupid420
Stupid420

Reputation: 1419

selecting images as per specific index values in numpy array

I have an array containing images of data as follows.

print(np.shape(input_data_transformed))

(120, 120, 1, 589)

Here input_data_transformed is NumPy array having 589 images stored in it. Each image is 120x120 in size with a single channel.

I have another NumPy array called index array as follows.

index_array=np.array([  8,   9,  10,  11, ..............., 584, 585, 586])

I want to select images from input_data_transformed as per the index values in index_array

So the final_filtered_data should contain only those images data, the index of which is given in the index_array

final_filtered_data=?

Upvotes: 0

Views: 479

Answers (1)

yatu
yatu

Reputation: 88305

Seems like your dimensions are in the wrong order. You could transpose and then just index on the first axis:

input_data_transformed.transpose(3,2,0,1)[index_array]

Checking with an example:

a = np.random.rand(120, 120, 1, 589)
index_array=np.array([  8,   9,  10,  11, 584, 585, 586])

a.transpose(3,2,0,1)[index_array].shape
# (7, 1, 120, 120)

Upvotes: 1

Related Questions