Решение 1:
Так как вам нужно, чтобы все остальные индексы работали нормально, просто используйте np.where EDIT: @Paul Panzer указывает, однако, что это не работает, если -9999недопустимый индекс для поиска.
import numpy as np
a = np.asarray([[1, 11], [2, 22], [3, 33], [10000, 555]], dtype=np.int32)
b = np.asarray([[0, 1, 2, 3, -9999], [0, 1, 2, 10000, -9999], [0, 1, 2, 3, -9999], [0, 1, 2, 3, -9999]])
lut = np.arange(b.max()+1) #look up table
#creating equation to result in output
k,v = a.T
lut[k] = v
out_matrix = np.where(b!=-9999,lut[b],-9999)
Вывод:
array([[ 0, 11, 22, 33, -9999],
[ 0, 11, 22, 555, -9999],
[ 0, 11, 22, 33, -9999],
[ 0, 11, 22, 33, -9999]])
Решение 2:
Если хитрость не сработает, некоторые хитрости наверняка могут сработать,Используйте таблицу поиска, чтобы создать дополнительный поиск.Оберните функцию, если вам нужно выполнить эту операцию несколько раз.
import numpy as np
def masked_lookup(lut, lookup_array, mask_value = -9999):
'''Performs a lookup for all values except a mask_value,
set to -9999 by default. Uses Array indexing and points
the masked value to itself in the lookup. '''
lut_with_mask = np.zeros(len(lut) + 1, dtype=np.int32)
lut_with_mask[:-1] = lut
lut_with_mask[-1] = mask_value
lookup_array[lookup_array == mask_value] = len(lut)
return lut_with_mask[lookup_array]
a = np.asarray([[1, 11], [2, 22], [3, 33], [1000, 555]], dtype=np.int32)
b = np.asarray([[0, 1, 2, 3, -9999], [0, 1, 2, 1000, -9999], [0, 1, 2, 3, -9999], [0, 1, 2, 3, -9999]])
max_value = b.max()
lut = np.arange(max_value + 1) #look up table
#creating equation to result in output
k,v = a.T
lut[k] = v
out_matrix = masked_lookup(lut, b)
Вывод:
array([[ 0, 11, 22, 33, -9999],
[ 0, 11, 22, 555, -9999],
[ 0, 11, 22, 33, -9999],
[ 0, 11, 22, 33, -9999]])