I have two 3d arrays,
array_1=array([[[4, 6, 6, 8],[4, 5, 8, 5]],
[[6, 0, 8, 7],[9, 9, 2, 1]],
[[5, 2, 0, 9],[4, 3, 9, 7]]])
array_2=array([[['d1', 'd8', 'd7', 'd6'],['d1', 'd9', 'd7', 'd3']],
[['d3', 'd5', 'd5', 'd2'],['d3', 'd4', 'd6', 'd4']],
[['d4', 'd3', 'd9', 'd6'],['d5', 'd8', 'd1', 'd6']]])
I want to get the maximum values of array_1 and the corresponding elements of array_2, which would be;
max_values = array([[6, 6, 6, 9, 5],[9, 9, 9, 7, 9]])
cor_elements = array([['d3', 'd8', 'd5', 'd6'],['d3','d4', 'd1', 'd6']])
I can get the max vlaues and the indices of max_values of array_1 throgh the following code,
import numpy as np
max_values=array_1.max(axis=0)
max_ind=array_1.argmax(axis=0)
could you please help me how can get the cor_elements from array_2.
CodePudding user response:
One way using numpy.indices
:
argmax = array_1.argmax(axis=0)
j, k = np.indices(argmax.shape)
array_2[argmax, j, k]
Output:
array([['d3', 'd8', 'd5', 'd6'],
['d3', 'd4', 'd1', 'd6']], dtype='<U2')