如何比较字典值中的多个数组,并将每个数组元素映射到新的 array/list

How to compare multiple arrays from dictionary values, and map per array element the dictionary key into new array/list

我想比较字典值中定义的多个数组。我想比较所有数组中的每个元素以找到该元素的最低值。最低位元素对应的数组的键应该映射到一个新的数组(或列表)中:

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)}

预期结果

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

我曾尝试使用 np.minimum() 和类似的功能,但是这些不允许比较多个数组。

假设您词典中的每个条目都具有相同数量的元素,那么您可以将数据作为一个数组来处理。这可以通过使用 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)

那么,您可以申请np.argmin:

>>> arr.argmin(axis=0)
array([[1, 3, 1, 3, 3, 0],
       [0, 2, 2, 1, 3, 1]])