我有一个名为test
的非唯一原始数据。使用此输入,我想创建一个输出向量以及一组获得非零输出的 rows
和包含其输出的 data
。
import numpy as np
rows = np.array([3, 4])
test = np.array([1, 3, 3, 4, 5])
data = np.array([-1, 2])
我的预期输出是一个形状为 test.shape
的向量。
输出
中的每个元素:
- 如果
element
在索引为i
的rows
中,output[i] = data[i]
- 否则,
output[i] = 0
换句话说,以下生成我的输出。
output = np.zeros(test.shape)
for i, val in enumerate(rows):
output[test == val] = data[i]
有什么方法可以对其进行矢量化吗?
最佳答案
这是一个基于 searchsorted
的向量化方法-
# Get sorted index positions
idx = np.searchsorted(rows, test)
# Set out-of-bounds(invalid ones) to some dummy index, say 0
idx[idx==len(rows)] = 0
# Get invalid mask array found out by indexing data array
# with those indices and looking for matches
invalid_mask = rows[idx] != test
# Get data indexed array as output and set invalid places with 0s
out = data[idx]
out[invalid_mask] = 0
最后几行可能有两种选择,如果你挖掘一行 -
out = data[idx] * (rows[idx] == test) # skips using `invalid_mask`
out = np.where(invalid_mask, 0, data[idx])
关于python - 向量化此非唯一键操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49903830/