Array reclassification with numpy

前端 未结 1 708
刺人心
刺人心 2021-01-13 22:12

I have a large (50000 x 50000) 64-bit integer NumPy array containing 10-digit numbers. There are about 250,000 unique numbers in the array.

I have a second reclassi

相关标签:
1条回答
  • 2021-01-13 22:42

    Store the lookup table as a 250,000 element array where for each index you have the mapped value. For example, if you have something like:

    lookups = [(old_value_1, new_value_1), (old_value_2, new_value_2), ...]
    

    Then you can do:

    idx, val = np.asarray(lookups).T
    lookup_array = np.zeros(idx.max() + 1)
    lookup_array[idx] = val
    

    When you get that, you can get your transformed array simply as:

    new_array = lookup_array[old_array]
    
    0 讨论(0)
提交回复
热议问题