两个数组之间的按行比较 [英] Row-wise comparisons between two arrays

查看:82
本文介绍了两个数组之间的按行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Numpy和Python的新手,我有疑问。希望您能帮助我。

I'm new to Numpy and Python and I have questions. I hope you could help me.

说,我有两个数组。这两个数组都有11列,第一列是索引。

Say, I have two arrays. Both arrays have 11 columns, with the first column being the index.

这是array1的示例。顺便说一下,array1将是一个实时更新的实时数组。

This is the example of array1. By the way, array1 would be a constantly updating array live.

[(0, 537, 504, 547, 560, 553,  -92, -5132, 15972, 1, 1)
 (0, 537, 504, 547, 559, 553, -100, -5128, 16108, 1, 1)
 (0, 537, 504, 547, 560, 553, -124, -5088, 16092, 1, 1)
 (0, 537, 504, 547, 559, 553, -140, -5160, 16164, 1, 1)
 (0, 537, 504, 547, 560, 552, -112, -5320, 16072, 1, 1)
 (0, 537, 504, 547, 560, 552,  -24, -5092, 16092, 1, 1)
 (0, 537, 504, 547, 560, 551, -148, -5104, 16108, 1, 1)
 (0, 537, 504, 547, 560, 551,  -92, -5136, 16092, 1, 1)
 (0, 537, 504, 547, 560, 551,    4, -5032, 16076, 1, 1)
 (0, 537, 504, 547, 560, 551,  -60, -5096, 15944, 1, 1)
 (0, 537, 504, 547, 560, 552,  -48, -5084, 16072, 1, 1)
 (0, 537, 504, 547, 560, 552,  -48, -5084, 16072, 1, 1)
 (0, 537, 504, 547, 560, 552,  -48, -5084, 16072, 1, 1)
 (0, 537, 504, 547, 560, 552,  -48, -5084, 16072, 1, 1)]

我想比较值array1的最后一行到array2的所有行之间的距离。 (它必须是array1的最后一行,因为它的内容将来自不断更新的csv文件)。我想搜索array1的最后一行的非标签列和array2的所有非标签行之间最接近的行值。 array1中的标签将为空,并且不会计入比较中。不一定要完全匹配,但我希望它能在设定的公差范围内找到最接近的匹配。 array2将充当各种字典,其标签用作参考,而这些单独标签的行中的特征用作样本。我是要实现这一权利,还是有一种更合适的方法来实现这一目标?我打算使用26种不同类型的标签,分别代表array2中26个字母,每个标签都有特定的列功能集。这26种类型的标签各有10个样本行。公差范围应由每个标签的那10个样品表示。这是array2中的csv数据示例(我已经可以将其转换为数组)。

I want to compare the value of the last row of array1 to all the rows of array2. (It has to be the last row of array1 because it's contents would come from a constantly updating csv file). I want to search for the closest row values between the non-label columns of the last row of array1 and all the non-label rows of array2. The label in array1 would be null and won't count in the comparison. It doesn't have to be an exact match, but I want it to locate the closest match within a set tolerance. The array2 will serve as a dictionary of sorts, with its labels serving as reference and the features in the row of those individual labels acting as the samples. Am I going about this right or is there a more appropriate way to achieve this? I intend to have 26 different types of labels representative of the 26 letters of the alphabet in array2, each with specific sets of column features. Those 26 types of labels will have 10 sample rows each. The tolerance range should be indicated by those 10 samples per label. Here is a sample of the csv data in array2 (which I could already convert into arrays).

LABEL,F1,F2,F3,F4,F5,X,Y,Z,C1,C2

1, 537, 504, 547, 560, 553, -92, -5132, 15972, 1, 1

1, 537, 504, 547, 559, 553, -100, -5128, 16108, 1, 1

1, 537, 504, 547, 560, 553, -124, -5088, 16092, 1, 1

1, 537, 504, 547, 559, 553, -140, -5160, 16164, 1, 1

1, 537, 504, 547, 560, 552, -112, -5320, 16072, 1, 1

1, 537, 504, 547, 560, 552, -24, -5092, 16092, 1, 1

1, 537, 504, 547, 560, 551, -148, -5104, 16108, 1, 1

1, 537, 504, 547, 560, 551, -92, -5136, 16092, 1, 1

1, 537, 504, 547, 560, 551, 4, -5032, 16076, 1, 1

1, 537, 504, 547, 560, 551, -60, -5096, 15944, 1, 1

2, 537, 504, 547, 560, 553, -92, -5132, 15972, 0, 0

2, 537, 504, 547, 559, 553, -100, -5128, 16108, 0, 0

2, 537, 504, 547, 560, 553, -124, -5088, 16092, 0, 0

2, 537, 504, 547, 559, 553, -140, -5160, 16164, 0, 0

2, 537, 504, 547, 560, 552, -112, -5320, 16072, 0, 0

2, 537, 504, 547, 560, 552, -24, -5092, 16092, 0, 0

2, 537, 504, 547, 560, 551, -148, -5104, 16108, 0, 0

2, 537, 504, 547, 560, 551, -92, -5136, 16092, 0, 0

2, 537, 504, 547, 560, 551, 4, -5032, 16076, 0, 0

2, 537, 504, 547, 560, 551, -60, -5096, 15944, 0, 0

1是A,2是B。如您所见,它们的唯一区别是1和最后两列为0。但是,其他字母在多列中会有所不同,这就是为什么我要让array1的最后一行在array2中搜索最接近的匹配项。

1 is A, and 2 is B. As you can see, their only differences are the 1s and 0s in the last two columns. However, the other letters of the alphabet will have differences in multiple columns, that's why I want the last row of array1 to search for its closest match in array2.

最后,我要打印array2的标签,其功能最接近最新数组的标签。数组1。而且由于array1中的输入将不断更新,因此如果array1的最后一行随后将更改值并对应于不同的标签,我希望它也相应地实时更新。

In the end, I want to print the label of array2 whose features are closest to that of the latest row of array1. And since the inputs in array1 are going to be constantly updating, if the last row of array1 would then change in values and correspond to a different label, I want it to update accordingly live as well.

同样,我是Python和Numpy的初学者,我不知道我是否正确地解决了这个问题。我希望你能帮助我。先感谢您。

Again, I'm a beginner in Python and Numpy I don't know if I'm approaching this correctly. I hope you can help me. Thank you in advance. I would really appreciate any help.

推荐答案

您的数组A和B:

 A = np.array([(0, 537, 504, 547, 560, 553,  -92, -5132, 15972, 1, 1),
 (0, 537, 504, 547, 559, 553, -100, -5128, 16108, 1, 1),
 (0, 537, 504, 547, 560, 553, -124, -5088, 16092, 1, 1),
 (0, 537, 504, 547, 559, 553, -140, -5160, 16164, 1, 1),
 (0, 537, 504, 547, 560, 552, -112, -5320, 16072, 1, 1),
 (0, 537, 504, 547, 560, 552,  -24, -5092, 16092, 1, 1),
 (0, 537, 504, 547, 560, 551, -148, -5104, 16108, 1, 1),
 (0, 537, 504, 547, 560, 551,  -92, -5136, 16092, 1, 1),
 (0, 537, 504, 547, 560, 551,    4, -5032, 16076, 1, 1),
 (0, 537, 504, 547, 560, 551,  -60, -5096, 15944, 1, 1),
 (0, 537, 504, 547, 560, 552,  -48, -5084, 16072, 1, 1),
 (0, 537, 504, 547, 560, 552,  -48, -5084, 16072, 1, 1),
 (0, 537, 504, 547, 560, 552,  -48, -5084, 16072, 1, 1),
 (0, 537, 504, 547, 560, 552,  -48, -5084, 16072, 1, 1)])

 B = np.array([[1, 537, 504, 547, 560, 553, -92, -5132, 15972, 1, 1],
[1, 537, 504, 547, 559, 553, -100, -5128, 16108, 1, 1],
[1, 537, 504, 547, 560, 553, -124, -5088, 16092, 1, 1],
[1, 537, 504, 547, 559, 553, -140, -5160, 16164, 1, 1],
[1, 537, 504, 547, 560, 552, -112, -5320, 16072, 1, 1],
[1, 537, 504, 547, 560, 552, -24, -5092, 16092, 1, 1],
[1, 537, 504, 547, 560, 551, -148, -5104, 16108, 1, 1],
[1, 537, 504, 547, 560, 551, -92, -5136, 16092, 1, 1],
[1, 537, 504, 547, 560, 551, 4, -5032, 16076, 1, 1],
[1, 537, 504, 547, 560, 551, -60, -5096, 15944, 1, 1],
[2, 537, 504, 547, 560, 553, -92, -5132, 15972, 0, 0],
[2, 537, 504, 547, 559, 553, -100, -5128, 16108, 0, 0],
[2, 537, 504, 547, 560, 553, -124, -5088, 16092, 0, 0],
[2, 537, 504, 547, 559, 553, -140, -5160, 16164, 0, 0],
[2, 537, 504, 547, 560, 552, -112, -5320, 16072, 0, 0],
[2, 537, 504, 547, 560, 552, -24, -5092, 16092, 0, 0],
[2, 537, 504, 547, 560, 551, -148, -5104, 16108, 0, 0],
[2, 537, 504, 547, 560, 551, -92, -5136, 16092, 0, 0],
[2, 537, 504, 547, 560, 551, 4, -5032, 16076, 0, 0],
[2, 537, 504, 547, 560, 551, -60, -5096, 15944, 0, 0]])

B与最后一行之间的差异A

Difference between B and last row of A

D = B - A[-1]

最近始终是一种讨论,但要说的是,您要让绝对值的总和最小。

"Closest" is always a discussion, but say you want the one where the sum of the absolute values are at a minimum.

np.abs(D).sum(axis=1).argmin()

这使第5行最接近。

B [np.abs(D).sum(axis = 1).argmin()] 产生:

array([    1,   537,   504,   547,   560,   552,   -24, -5092, 16092,
           1,     1])

这篇关于两个数组之间的按行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆