Python - How to Extract Elements from an Array Based on an Array of Indices

Python - How to extract elements from an array based on an array of indices?

X = [X[index] for index in Y]

This is a list comprehension; you can look up that topic to learn more.

How to extract elements in an array with regard to an index?

I think you want boolean indexing:

A[index.astype(bool)]
# array([2, 4])

Use index of one array to extract value from list to be appended into new array (Python)

You can directly index using numpy arrays:

>>> import numpy as np
>>> data = np.array([2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36])
>>> first_index = np.array([7, 17])
>>> second_index = data[first_index]
>>> second_index
array([16, 36])

How do I extract elements from a 2D array using a 2D array of indices?

I've encountered the same issue not long ago, and the answer is actually quite simple :

result = a[b[:,0], b[:,1]]

Extract the index value from array

If you can use numpy, check out argwhere

a1 = np.array([[0,1,2],[3,4,5]])
a2 = [0,1,2,3,4,5]
a3 = np.argwhere(a1 == a2[3]).squeeze() # -> (1, 0)

Extract values from a numpy array based on another array of 0/1 indices

If your mask array idx has the same shape as your array A, then you should be able to extract elements specified by the mask if you convert idx to a boolean array, using astype.

Demo -

>>> A
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]])
>>> idx
array([[1, 0, 0, 1, 1],
[0, 0, 0, 1, 0],
[1, 0, 0, 1, 1],
[1, 0, 0, 1, 1],
[0, 1, 1, 1, 1]])
>>> A[idx.astype(bool)]
array([ 0, 3, 4, 8, 10, 13, 14, 15, 18, 19, 21, 22, 23, 24])

Extracting value of an array based on the value of another array PYTHON

Since you are already using numpy as a dependency, you can use the np.argmin function, which returns the indices of the minimum values of an array. Then you can use the index returned to index flux_err.

In your case, that would be something along those lines:

min_idx = np.argmin(flux)
min_err = flux_err[min_idx]

Notice that argmin() may return multiple indices in case the minimum value is repeated in the array.

P.S.: Unless I am missing what you mean there, astype(bool) is not related to any of this.

Efficiently extract values from array using a list of index

You can use advanced indexing which basically means extract the row and column indices from the index array and then use it to extract values from a, i.e. a[index[:,0], index[:,1]] -

%timeit a[index[:,0], index[:,1]]
# 12.1 µs ± 368 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit [a[i, j] for [i, j] in index]
# 2.22 ms ± 105 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Extract elements of a 2d array with indices from another 2d array

This can be easily done if we index into the raveled data array:

out = data.ravel()[ind.ravel() + np.repeat(range(0, 8*ind.shape[0], 8), ind.shape[1])].reshape(ind.shape)

Explanation

It might be easier to understand if it is broken down into three steps:

indices = ind.ravel() + np.repeat(range(0, 8*ind.shape[0], 8), ind.shape[1])
out = data.ravel()[indices]
out = out.reshape(ind.shape)

ind has the information on the elements from data that we want. Unfortunately, it is expressed in 2-D indices. The first line above converts these into indices of the 1-D raveled data. The second line above selects those elements out of the raveled array data. The third line restores the 2-D shape to out.
The 2-D indices represented by ind is converted to indindices has the indices



Related Topics



Leave a reply



Submit