Numpy 基于第一列与python中另一个文本文件的匹配来选择第二列数据

Numpy 基于第一列与python中另一个文本文件的匹配来选择第二列数据,numpy,matplotlib-basemap,Numpy,Matplotlib Basemap,我对numpy数组和迭代知之甚少。我有两个输入 文件夹。两个文件的第一列都表示时间(毫秒)。 输入文件1是参考值或模拟值。输入文件2从测试值中获取。我想比较输入-2的第二列(第二列和第一列) 当且仅当对应文件的第一列中存在时间匹配时,文件才具有第一个文件的第二列。 我正在反复尝试,但还没有找到合适的结果。如何 当存在匹配项时查找索引 import numpy as np my_file=np.genfromtxt('path/input1.txt') Sim_val=np.genfromtxt(

我对numpy数组和迭代知之甚少。我有两个输入 文件夹。两个文件的第一列都表示时间(毫秒)。 输入文件1是参考值或模拟值。输入文件2从测试值中获取。我想比较输入-2的第二列(第二列和第一列) 当且仅当对应文件的第一列中存在时间匹配时,文件才具有第一个文件的第二列。 我正在反复尝试,但还没有找到合适的结果。如何 当存在匹配项时查找索引

import numpy as np
my_file=np.genfromtxt('path/input1.txt')
Sim_val=np.genfromtxt('path/input2.txt')
inp1=my_file[:,0]
inp12=my_file[:,1]
inpt2=Sim_val[:,0]
inpt21=Sim_val[:,1]
xarray=np.array(inp1)
yarray=np.array(inp12)
data=np.array([xarray,yarray])
ldata=data.T

zarray=np.array(inpt2)
tarray=np.array(inpt21)
mdata=np.array([zarray,tarray])
kdata=mdata.T

i=np.searchsorted(kdata[:,0],ldata[:,0])
print i
我的inputfile-2和inputfile-1是

   0        5               0        5
  100       6               50       6
  200      10               200      15
  300      12               350      12
  400      15 # Obtained    400      15    #Simulated Value
  500      20   #Value      500      25
  600      0                650      0
  700      11               700      11
  800      12               850      8
  900      19               900      19
 1000     10                1000     3
在numpy数组和迭代方面真的很困难。 请任何人建议我如何解决上述问题。其实我 也有其他列,但所有操作都取决于第一列的匹配(时间匹配)


再次表示非常感谢。

你是说

import numpy as np

simulated = np.array([
    (0, 5),
    (100, 6),
    (200, 10),
    (300, 12),
    (400, 15),
    (500, 20),
    (600, 0),
    (700, 11),
    (800, 12),
    (900, 19),
    (1000, 10)
])

actual = np.array([
    (0, 5),
    (50, 6),
    (200, 15),
    (350, 12),
    (400, 15),
    (500, 25),
    (650, 0),
    (700, 11),
    (850, 8),
    (900, 19),
    (1000, 3)
])


def indexes_where_match(A, B):
    """ an iterator that goes over the indexes of wherever the entries in A's first-col and B's first-col match """
    return (i for i, (a, b) in enumerate(zip(A, B)) if a[0] == b[0])


def main():
    for i in indexes_where_match(simulated, actual):
        print(simulated[i][1], 'should be compared to', actual[i][1])

if __name__ == '__main__':
    main()
您还可以使用列切片,如下所示:

simulated_time, simulated_values = simulated[..., 0], simulated[..., 1:]
actual_time, actual_values = actual[..., 0], actual[..., 1:]

indexes_where_match = (i for i, (a, b) in enumerate(zip(simulated_time, actual_time)) if a == b)

for i in indexes_where_match:
    print(simulated_values[i], 'should be compared to', actual_values[i])


# outputs:
# [5] should be compared to [5]
# [10] should be compared to [15]
# [15] should be compared to [15]
# [20] should be compared to [25]
# [11] should be compared to [11]
# [19] should be compared to [19]
# [10] should be compared to [3]

是的,我就是要这个。无论何时匹配,我都会比较两个文件的第二列。我会检查你的一个。只是修改相同的问题。如果模拟值为100,那么实际值为70。其中50个是匹配的。意味着两者都以不同的速率生成数据。那么现在我如何从模拟值中减去匹配的实际值呢。简言之,如果5000毫秒的模拟值出现在第20个索引上,而5000毫秒的实际值出现在第15个索引上。现在我如何比较这两个索引的其他两列。@Nitsan BenHanoch在我的第一个例子中,这两列的长度相同。因此,在相同的位置,相同的索引数据对两列都是匹配的。现在,不同长度的索引i对于两列数据来说是不一样的。@Nitsan BenHanoch。我的第一个病例数据长度相同,发生在同一地点。所以索引是匹配的。但在这种情况下,两者的比率不同。因此,索引的方式对于这两种情况并不常见。在这种情况下,如何求解两个指数