Manasi
Manasi

Reputation: 189

Filter n x dimensional array with index matrix

I have a n X dimensional array 'A' of the shape - (50000, 32, 32, 3)

I want to filter items belonging to the following indexes - array([ 5, 10, 15, ..., 49938, 49952, 49988])

How do I create a new array 'B' containing only items belonging to these indices.

Upvotes: 0

Views: 54

Answers (2)

shaik moeed
shaik moeed

Reputation: 5785

You can get values of N-D(4-D in your case) array by using,

Sample data:

>>> array = np.random.randn(10, 3, 3, 2) # 4-D array
>>> filter_index_array = [2,4,8,9] # your filter index values

Output:

>>> filtered_array = np.array([x[i] for i in filter_index_array]) # This is how you can get your required index values.
>>> filtered_array.shape
(4, 3, 3, 2)

Validation:

>>> len(filtered_array) == len(filter_index_array)
True

Upvotes: 0

fountainhead
fountainhead

Reputation: 3722

If idx is your array array([ 5, 10, 15, ..., 49938, 49952, 49988]), then:

result = A[idx]

will give the required filtered array.

Upvotes: 1

Related Questions