您可以将布尔索引与高级 numpy 数组索引一起使用:
array3 = array1.astype(float) # this copies the array by default.
array3[array1 != 0] = array2[array1[array1 != 0]-1, 2]
结果是:
array([[ 0, 62., 62., 88.],
[ 0, 73., 64., 95.],
[ 0, 59., 67., 65.]])
说明
您首先创建一个布尔数组,指示非零条目的位置:
>>> non_zero_mask = array1 != 0
array([[False, True, True, True],
[False, True, True, True],
[False, True, True, True]], dtype=bool)
这将用于查找应替换的元素。
那么你需要找到这些元素的值:
>>> non_zero_values = array1[non_zero_mask]
array([7, 4, 1, 8, 5, 2, 9, 6, 3])
由于您的 array2 已排序并以值 1 开头,因此我们需要减一以找到替换值的适当行。如果您的 array2 未排序,您可能需要对其进行排序或在两者之间进行另一个索引:
>>> replacement_rows = array2[non_zero_values-1]
array([[ 7., 7., 62.],
[ 4., 4., 62.],
[ 1., 1., 88.],
[ 8., 8., 73.],
[ 5., 5., 64.],
[ 2., 2., 95.],
[ 9., 9., 59.],
[ 6., 6., 67.],
[ 3., 3., 65.]])
>>> replacement_values = array2[non_zero_values-1, 2] # third element of that row!
array([ 62., 62., 88., 73., 64., 95., 59., 67., 65.])
然后将这些值分配给原始数组或新数组:
array3[non_zero_mask] = replacement_values
这种方法依赖于array2 的顺序,所以如果有更复杂的条件,它就会中断。但这要么需要找到 value 和 index 之间的关系并将其插入,而不是我所做的简单 -1,要么进行另一个中间 np.where/boolean 索引。
扩展
如果您没有已排序的 array2 并且无法对其进行排序,您可以这样做:
>>> array3 = array1.astype(float)
>>> array3[array1 != 0] = array2[np.where(array2[:, 0][None, :] == array1[array1 != 0][:, None])[1], 2]
>>> array3
array([[ 0., 62., 62., 88.],
[ 0., 73., 64., 95.],
[ 0., 59., 67., 65.]])
由于这适用于相互广播数组,您将创建一个大小为array1.size * array1.size 的数组。所以这可能不是很高效,但仍然完全矢量化。
Numba(如果你想要速度)
numba 非常棒,如果你想加快速度,因为没有原生 numpy 或 scipy 版本。如果您有 anaconda 或 conda,它已经安装,所以它可能是一个可行的选择:
import numba as nb
import numpy as np
@nb.njit
def nb_replace_values(array, old_new_array):
res = np.zeros(array.shape, dtype=np.float64)
rows = array.shape[0]
columns = array.shape[1]
rows_replace_array = old_new_array.shape[0]
for row in range(rows):
for column in range(columns):
val = array[row, column]
# only replace values that are not zero
if val != 0:
# Find the value to replace the element with
for ind_replace in range(rows_replace_array):
if old_new_array[ind_replace, 0] == val:
# Match found. Replace and break the innermost loop
res[row, column] = old_new_array[ind_replace, 2]
break
return res
nb_replace_values(array1, array2)
array([[ 0., 62., 62., 88.],
[ 0., 73., 64., 95.],
[ 0., 59., 67., 65.]])
特别是对于大型数组,这显然是最快且内存效率最高的解决方案,因为不会创建临时数组。第一次调用会慢很多,因为函数需要动态编译。
时间安排:
%timeit nb_replace_values(array1, array2)
100000 次循环,3 次中的最佳:每个循环 6.23 µs
%%timeit
array3 = array1.astype(float)
array3[array1 != 0] = array2[np.where(array2[:, 0][None, :] == array1[array1 != 0][:, None])[1], 2]
10000 次循环,3 次中的最佳:每个循环 74.8 µs
# Solution provided by @PDRX
%%timeit
array3 = array1.astype(float)
for i in array2[:,0]:
i_arr1,j_arr1 = np.where(array1 == i)
i_arr2 = np.where(array2[:,0] == i)
array3[i_arr1,j_arr1] = array2[i_arr2,2]
1000 次循环,3 次中的最佳:每个循环 689 µs