1

I want to compare multiple arrays which are defined in the values of a dictionary. I want to compare every element within all arrays to find the lowest value for this element. The key of the array corresponding to the lowest element should be mapped into a new array (or list) as such:

img_dictionary = {0: np.array([[935, 925, 271, 770, 869, 293],
                               [125, 385, 291, 677, 770, 770]], dtype=uint64),
                  1: np.array([[ 12,  92,  28, 942, 124, 882],
                               [241, 853, 292, 532, 834, 231]], dtype=uint64),
                  2: np.array([[934, 633, 902, 912, 922, 812],
                               [152, 293, 284, 634, 823, 326]], dtype=uint64),
                  3: np.array([[362,  11, 292,  48,  92, 481],
                               [196, 294, 446, 591,  92, 591]], dtype=uint64)}

Expected outcome

(array([[1, 3, 1, 3, 3, 0],
        [0, 2, 2, 1, 3, 1]], dtype=uint64),)

I have tried to use np.minimum() and functions alike, but these do not allow for the comparison of multiple arrays.

2
  • Ivan, you edited my np.array and np.dtype to array and dtype respectively, but if I try this without defining np. it states NameError: name 'array' is not defined, same for dtype NameError: name 'uint64' is not defined.. Commented Aug 4, 2021 at 17:20
  • Sorry about that, it was meant to be np.array. It will work as array if you import NumPy with from numpy import *... Commented Aug 4, 2021 at 18:44

1 Answer 1

4

Assuming each entry in your dictionnary has the same number of elements, then you can work on your data as one array. This can be done by stacking the arrays with np.stack:

>>> arr = np.stack(list(img_dictionary.values()))
array([[[935, 925, 271, 770, 869, 293],
        [125, 385, 291, 677, 770, 770]],

       [[ 12,  92,  28, 942, 124, 882],
        [241, 853, 292, 532, 834, 231]],

       [[934, 633, 902, 912, 922, 812],
        [152, 293, 284, 634, 823, 326]],

       [[362,  11, 292,  48,  92, 481],
        [196, 294, 446, 591,  92, 591]]], dtype=uint64)

Then, you can apply np.argmin:

>>> arr.argmin(axis=0)
array([[1, 3, 1, 3, 3, 0],
       [0, 2, 2, 1, 3, 1]])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, please add arr to np,argmin(axis=0) --> np.argmin(arr, axis=0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.