Numpy Selecting Specific Column Index Per Row by Using a List of Indexes

NumPy selecting specific column index per row by using a list of indexes

If you've got a boolean array you can do direct selection based on that like so:

>>> a = np.array([True, True, True, False, False])
>>> b = np.array([1,2,3,4,5])
>>> b[a]
array([1, 2, 3])

To go along with your initial example you could do the following:

>>> a = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> b = np.array([[False,True,False],[True,False,False],[False,False,True]])
>>> a[b]
array([2, 4, 9])

You can also add in an arange and do direct selection on that, though depending on how you're generating your boolean array and what your code looks like YMMV.

>>> a = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> a[np.arange(len(a)), [1,0,2]]
array([2, 4, 9])

Numpy replacing specific column index per row by using a list of indexes with nan

Use masking -

m = ~np.isnan(idx) # Mask of non-NaNs
row = np.arange(a.shape[0])[m]
col = idx[m].astype(int)
a[row, col] = 20

where, idx is the indexing array.

Sample run -

In [161]: a = np.array([[1,2,3], [4,5,6], [7,8,9]])

In [162]: idx = np.array([1,np.nan,2])

In [163]: m = ~np.isnan(idx) # Mask of non-NaNs
...: row = np.arange(a.shape[0])[m]
...: col = idx[m].astype(int)
...: a[row, col] = 20
...:

In [164]: a
Out[164]:
array([[ 1, 20, 3],
[ 4, 5, 6],
[ 7, 8, 20]])

Selecting specific rows and columns from NumPy array

Fancy indexing requires you to provide all indices for each dimension. You are providing 3 indices for the first one, and only 2 for the second one, hence the error. You want to do something like this:

>>> a[[[0, 0], [1, 1], [3, 3]], [[0,2], [0,2], [0, 2]]]
array([[ 0, 2],
[ 4, 6],
[12, 14]])

That is of course a pain to write, so you can let broadcasting help you:

>>> a[[[0], [1], [3]], [0, 2]]
array([[ 0, 2],
[ 4, 6],
[12, 14]])

This is much simpler to do if you index with arrays, not lists:

>>> row_idx = np.array([0, 1, 3])
>>> col_idx = np.array([0, 2])
>>> a[row_idx[:, None], col_idx]
array([[ 0, 2],
[ 4, 6],
[12, 14]])

Select One Element in Each Row of a Numpy Array by Column Indices

You can choose from given array using numpy.choose which constructs an array from an index array (in your case select_id) and a set of arrays (in your case input_array) to choose from. However you may first need to transpose input_array to match dimensions. The following shows a small example:

In [101]: input_array
Out[101]:
array([[ 3, 14],
[12, 5],
[75, 50]])

In [102]: input_array.shape
Out[102]: (3, 2)

In [103]: select_id
Out[103]: [0, 1, 1]

In [104]: output_array = np.choose(select_id, input_array.T)

In [105]: output_array
Out[105]: array([ 3, 5, 50])

2D to 1D numpy array with indices of column for each row

You are close, try:

C = A[range(len(B)), B]

This should yield C[i] = A[i,B[i]]

Numpy: How to select row entries in a 2d array by column vector

You can achieve this using np.take_along_axis:

>>> np.take_along_axis(X, S[..., None], axis=1)
array([[1],
[9],
[3],
[1]])

You need to make sure both array arguments are of the same shape (or broadcasting can be applied), hence the S[..., None] broadcasting.

Of course your can reshape the returned value with a [:, 0] slice.

>>> np.take_along_axis(X, S[..., None], axis=1)[:, 0]
array([1, 9, 3, 1])

Alternatively you can just use indexing with an arangement:

>>> X[np.arange(len(S)), S[np.arange(len(S))]]
array([1, 9, 3, 1])

I believe this is also equivalent to np.diag(X[:, S]) but with unnecessary copying...

Select elements in each row with column indices from another array

You can use advance indexing in numpy:

a[np.indices(idx.shape)[0], idx]

np.indices(idx.shape)[0] creates the corresponding row indices for your column indices in idx and together, they form advance indexing.

output:

[[2 4]
[5 7]
[2 6]
[3 6]]


Related Topics



Leave a reply



Submit