跨两个2D(或3D)numpy数组获取交叉(公共)索引 [英] Get intersecting (common) indexes across two 2D (or 3D) numpy arrays
问题描述
我有两个不同目录的数据,我想使用坐标来匹配这两个目录。数据I是来自目录1的 x1,y1,z1,a1,b1,c1等
(约50万个元素), x2,我想要做的是首先构造一个二维数组,其中包含(x,y)坐标(x,y),其中x,y,z2,a2,e2,m2,n2,etc
,如果必要,我将扩展到(x,y,z),并比较二维数组找到相同的元素。
I have data from two different catalogs, I want to use the coordinates to match those two catalogs. The data I have is x1,y1,z1,a1,b1,c1,etc
(about half million elements) from catalog 1, and x2,y2,z2,a2,e2,m2,n2,etc
(about million elements) from catalog 2. What I am trying to do is first to construct a 2D array which contains (x,y) coordinates, if necessary I will extend to (x,y,z), and compare the 2D arrays to find the same elements.
co1 = np.vstack((x1,y1)).T
co2 = np.vstack((x2,y2)).T
idx1 = np.in1d(co1,co2) # not working for 2D arrays
idx2 = np.in1d(co2,co1)
np.savetxt('combined_data.txt',np.c_[x1[idx1],y1[idx1],a1[idx1],e2[idx2],n2[idx2]],fmt='%1.4f %1.4f %1.4f %1.4f %1.4f')
例如,我有以下数据集:
For example, I have the following dataset:
x1 = np.array([1,2,3,4,5])
y1 = np.array([5,4,3,2,1])
x2 = np.array([1,4,6,2,6,4,8,9,3])
y2 = np.array([5,1,5,3,6,2,8,3,3])
(1,5), (3,3), (4,2) are the common coordinates between the two catalogs. Therefore,
idx1 = [Ture, False, True, True, False], idx2 = [True, False, False, False, False, True, False, False, True].
但问题是 np.in1d
是一个1D程序,它不能应用于2D或3D阵列。任何人都知道一些numpy例程来完成这个任务?
But the problem is that np.in1d
is a 1D routine, it can not be applied to 2D or 3D arrays. Anyone knows some numpy routines to accomplish this task?
推荐答案
将这两个数组转换为pandas dataframes:
Convert both arrays to pandas dataframes:
df1 = pd.DataFrame({"x" : x1, "y" : y1})).reset_index()
合并它们:
result = pd.merge(df1, df2, left_on=["x","y"], right_on=["x","y"])
# index_x x y index_y
#0 0 1 5 0
#1 2 3 3 8
#2 3 4 2 5
并获得索引:
result[["index_x","index_y"]]
# index_x index_y
#0 0 0
#1 2 8
#2 3 5
这篇关于跨两个2D(或3D)numpy数组获取交叉(公共)索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!