【问题标题】:Finding the correspondence of the elements of two columns in a two dimensional array in another 2D array在另一个二维数组中查找二维数组中两列元素的对应关系
【发布时间】:2014-10-09 02:00:21
【问题描述】:

我有一个数据文件,其中包含XY 位置以及额外列中的一些其他属性。该文件已与代码一起使用,并且已对额外的列进行了一些计算。另一方面,基于XY 的原始文件的顺序已更改。每个文件包含近 25000 行。我想根据第一个文件中的XY 位置找到对第二个文件下达命令的最快方法,并将额外的属性附加到原始文件中。 第一个文件的示例:

149.021 204.492 0.187235 0.233713 0.457313 1
478.467 421.622 -0.10692 -0.141686 0.80883 1
160.736 220.413 -0.0236415 -0.0962102 0.591765 1
485.327 60.0308 -0.121497 -0.195995 0.205639 1
497.014 59.1949 -0.00143256 0.0205032 0.179389 1
468.399 421.935 0.0709232 0.144875 1.15085 1
492.597 53.4478 -0.0407651 0.0535784 0.826217 1
153.751 1.22557 -0.194406 0.107156 1.15702 1
66.8625 38.0019 -0.178805 0.1475 0.732446 1
410.695 366.188 -0.179227 -0.216467 0.414247 1
502.513 361.575 -0.114651 -0.0094424 0.966985 1
398.835 61.0347 -0.160149 -0.216436 0.761429 1
459.336 177.548 0.174666 -0.0481024 1.43403 1
111.007 236.622 0.23444 0.201739 1.15933 1
46.9406 255.214 -0.00335369 -0.0306168 0.494205 1
236.685 335.616 -0.00456215 0.0871247 1.26644 1
329.606 231.826 0.131988 0.00122767 1.1712 1
335.467 128.234 -0.0490298 -0.0520559 0.61536 1
53.6696 191.586 0.194366 0.18284 1.47372 1
359.599 148.55 0.127806 -0.0621203 0.559922 1
494.895 431.539 0.201591 0.206185 0.818496 1
342.083 439.732 -0.00373251 -0.0841907 0.383131 1
426.053 201.598 0.0908615 -0.130614 1.23092 1
499.756 443.071 0.195117 0.0999967 0.312757 1
363.483 369.165 0.0791957 -0.0225179 0.319282 1

第二个文件的一部分:

494.895 431.539 -0.0175584 -0.109455 12 1
53.6696 191.586 -0.062199 0.504269 15 1
46.9406 255.214 0.405452 0.237562 6 1
329.606 231.826 -0.371944 -0.0784321 19 1
342.083 439.732 0.100881 -0.0167807 11 1
111.007 236.622 0.0578686 0.223055 12 1
363.483 369.165 0.518668 -0.0620763 18 1
485.327 60.0308 0.271933 -0.0514785 6 1
66.8625 38.0019 -0.191781 -0.104952 9 1
468.399 421.935 -0.1768 0.0328495 8 1
499.756 443.071 0.0240113 -0.146665 14 1
502.513 361.575 0.353556 -0.136077 16 1
149.021 204.492 0.198594 0.256404 1 1
160.736 220.413 0.0992627 0.206257 18 1
410.695 366.188 0.320096 0.0840666 20 1
398.835 61.0347 0.162925 0.081776 20 1
497.014 59.1949 -0.249416 0.239242 15 1
153.751 1.22557 -0.00283959 0.152171 20 1
459.336 177.548 0.297309 0.0990636 11 1
236.685 335.616 0.185583 -0.00510526 19 1
359.599 148.55 0.369466 0.279213 9 1
478.467 421.622 -0.28347 -0.0403472 18 1
426.053 201.598  0.273321 0.150204 5 1
492.597 53.4478 -0.0993111 -0.114191 19 1
335.467 128.234 -0.699557 0.0164072 18 1

【问题讨论】:

  • 来自Neil的评论: 第一个文件的顺序有什么特别的吗?阅读这个问题,我想知道最简单的解决方案是否可能是按 (X, Y) 对两个文件进行排序,以便它们匹配。
  • @ArtjomB。这也是我的第一个想法……但是文件的点数不同,可以通过基于距离的方法来克服
  • @SaulloCastro 他们没有不同的点数。好吧,我有点懒得匹配它们,因为它有点乏味。
  • @ArtjomB。顺序很重要,因为我需要根据他们的顺序将其与另一个文件匹配。

标签: python arrays numpy scikit-learn


【解决方案1】:

如果两个数组的行数相同,您可以使用np.lexsort(),这可能会给您带来更好的性能,您可以简单地这样做:

a = np.loadtxt('file1.txt')
b = np.loadtxt('file2.txt')

aind = np.lexsort((a[:,1], a[:,0]))    
bind = np.lexsort((b[:,1], b[:,0]))

a = a[aind]
b = b[bind]

这里可以将新的a 与新的b 进行比较...


编辑: 在对大型文件进行一些测试后,我已经删除了基于距离的方法,它需要大量内存......另一种方法是使用 np.in1d() 来识别数组 b 的哪些值包含在数组a... 效率更高。对于要比较的xy 列不一定是前两个列的一般情况:

import numpy as np

xcol_a = 6
ycol_a = 7
xcol_b = 0
ycol_b = 1
a = np.loadtxt('file1.txt')
b = np.loadtxt('file2.txt')

check_x = np.in1d(a[:, xcol_a], b[:, xcol_b])
check_y = np.in1d(a[:, ycol_a], b[:, ycol_b])
check = (check_x & check_y)
non_existing_indices = np.where(np.logical_not(check))[0]
non_existing_values = a[non_existing_indices]

a = a[check] # taking only the values of a that where also found in b
aind = np.lexsort((a[:, ycol_a], a[:, xcol_a]))
orig_order = np.argsort(aind)
bind = np.lexsort((b[:, ycol_b], b[:, xcol_b]))

a = a[aind][orig_order]
b = b[bind][orig_order]

现在b数组按照数组a排序,按照数组a的原顺序排列。

【讨论】:

  • 我想保留第一个文件的顺序,因为我正在读取另一个文件并进行一些计算。
  • 如果您确定两个文件的行将一对一匹配,您可以按照第一个文件的顺序对第二个文件进行排序b = b[bind[np.argsort(aind)]]
猜你喜欢
  • 2021-03-03
  • 2021-02-03
  • 2017-04-11
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
相关资源
最近更新 更多