0

I have a numpy array, a:

a = np.array([[-21.78878256,  97.37484004, -11.54228119],
              [ -5.72592375,  99.04189958,   3.22814204],
              [-19.80795922,  95.99377136, -10.64537733]])

I have another array, b:

b = np.array([[ 54.64642121,  64.5172014,   44.39991983],
              [  9.62420892,  95.14361441,   0.67014312],
              [ 49.55036427,  66.25136632,  40.38778238]])

I want to extract minimum value indices from the array, b.

ixs = [[2],
       [2],
       [2]]

Then, want to extract elements from the array, a using the indices, ixs:

The expected answer is:

result = [[-11.54228119]
          [3.22814204]
          [-10.64537733]]

I tried as:

ixs = np.argmin(b, axis=1)

print ixs
[2,2,2]

result = np.take(a, ixs)
print result

Nope!

Any ideas are welcomed

2 Answers 2

1

You can use

result = a[np.arange(a.shape[0]), ixs]

np.arange will generate indices for each row and ixs will have indices for each column. So effectively result will have required result.

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

6 Comments

more straight, perhaps without ixs = np.argmin(b, axis=1) is expected
You need to do that to find min in b
i do not want to use result = a[np.arange(a.shape[0]), ixs], instead want to do something before it..., any better alternative?
What do you mean by "before it"... what is the problem doing this?
i want to replace ixs = np.argmin(b, axis=1)
|
0

You can try using below code

np.take(a, ixs, axis = 1)[:,0]

The initial section will create a 3 by 3 array and slice the first column

>>> np.take(a, ixs, axis = 1)

array([[-11.54228119, -11.54228119, -11.54228119],
       [  3.22814204,   3.22814204,   3.22814204],
       [-10.64537733, -10.64537733, -10.64537733]])

Comments