First, of all, my apologies if this has been answered elsewhere. All I could find were questions about replacing elements of a given value, not elements of multiple values.<
I think the Divakar #3 method assumes that the mapping dict covers all values (or at least the maximum value) in the target array. Otherwise, to avoid index out of range errors, you have to replace the line
mapping_ar = np.zeros(k.max()+1,dtype=v.dtype) with
mapping_ar = np.zeros(array.max()+1,dtype=v.dtype)
That adds considerable overhead.