gpt4 book ai didi

python - 对于二维 numpy 数组的每一行,获取第二个二维数组中相等行的索引

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:54 24 4
gpt4 key购买 nike

我有两个巨大的 2d numpy 整数数组 X 和 U,其中假设 U 只有唯一行。对于 X 中的每一行,我想获得 U 中匹配行的相应行索引(如果有的话,否则为 -1)。例如,如果将以下数组作为输入传递:

U = array([[1, 4],
[2, 5],
[3, 6]])

X = array([[1, 4],
[3, 6],
[7, 8],
[1, 4]])

输出应该是:

array([0,2,-1,0])

是否有使用 Numpy 执行此操作(或类似操作)的有效方法?

@迪瓦卡尔:你的方法对我来说失败了

print(type(rows), rows.dtype, rows.shape)
print(rows[:10])
print(search2D_indices(rows[:10], rows[:10]))

<class 'numpy.ndarray'> int32 (47398019, 5)
[[65536 1 1 1 17]
[65536 1 1 1 153]
[65536 1 1 2 137]
[65536 1 1 3 153]
[65536 1 1 9 124]
[65536 1 1 13 377]
[65536 1 1 13 134]
[65536 1 1 13 137]
[65536 1 1 13 153]
[65536 1 1 13 439]]
[ 0 1 2 3 4 -1 -1 -1 -1 9]

最佳答案

方法 #1

灵感来自 this solutionFind the row indexes of several values in a numpy array
,这是一个使用 searchsorted 的矢量化解决方案-

def search2D_indices(X, searched_values, fillval=-1):
dims = np.maximum(X.max(0), searched_values.max(0))+1
X1D = np.ravel_multi_index(X.T,dims)
searched_valuesID = np.ravel_multi_index(searched_values.T,dims)
sidx = X1D.argsort()
idx = np.searchsorted(X1D,searched_valuesID,sorter=sidx)
idx[idx==len(sidx)] = 0
idx_out = sidx[idx]
return np.where(X1D[idx_out] == searched_valuesID, idx_out, fillval)

sample 运行-

In [121]: U
Out[121]:
array([[1, 4],
[2, 5],
[3, 6]])

In [122]: X
Out[122]:
array([[1, 4],
[3, 6],
[7, 8],
[1, 4]])

In [123]: search2D_indices(U, X, fillval=-1)
Out[123]: array([ 0, 2, -1, 0])

方法 #2

扩展到具有负整数的情况,我们需要相应地偏移 dims 并转换为 1D,就像这样 -

def search2D_indices_v2(X, searched_values, fillval=-1):
X_lim = X.max()-X.min(0)
searched_values_lim = searched_values.max()-searched_values.min(0)

dims = np.maximum(X_lim, searched_values_lim)+1
s = dims.cumprod()

X1D = X.dot(s)
searched_valuesID = searched_values.dot(s)
sidx = X1D.argsort()
idx = np.searchsorted(X1D,searched_valuesID,sorter=sidx)
idx[idx==len(sidx)] = 0
idx_out = sidx[idx]

return np.where(X1D[idx_out] == searched_valuesID, idx_out, fillval)

sample 运行-

In [142]: U
Out[142]:
array([[-1, -4],
[ 2, 5],
[ 3, 6]])

In [143]: X
Out[143]:
array([[-1, -4],
[ 3, 6],
[ 7, 8],
[-1, -4]])

In [144]: search2D_indices_v2(U, X, fillval=-1)
Out[144]: array([ 0, 2, -1, 0])

方法 #3

另一个基于views -

# https://stackoverflow.com/a/45313353/ @Divakar
def view1D(a, b): # a, b are arrays
a = np.ascontiguousarray(a)
b = np.ascontiguousarray(b)
void_dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
return a.view(void_dt).ravel(), b.view(void_dt).ravel()

def search2D_indices_views(X, searched_values, fillval=-1):
X1D,searched_valuesID = view1D(X, searched_values)
sidx = X1D.argsort()
idx = np.searchsorted(X1D,searched_valuesID,sorter=sidx)
idx[idx==len(sidx)] = 0
idx_out = sidx[idx]
return np.where(X1D[idx_out] == searched_valuesID, idx_out, fillval)

关于python - 对于二维 numpy 数组的每一行,获取第二个二维数组中相等行的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49964765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com