获取numpy数组的正确索引



我必须成对比较np.array,我需要返回索引。

我的代码是:

import numpy as np
Vals = np.array([[1.0, 1.0], [2., 2.], [1., 2.], [2., 1.], [3., 3.], [3., 3.]])
for Val in itertools.combinations(Vals,2):
X1 = Val[0][0]
X2 = Val[1][0]
Y1 = Val[0][1]
Y2 = Val[1][1]
Index1 = np.where( (Vals == Val[0]).all(axis=1))[0][0]
Index2 = np.where( (Vals == Val[1]).all(axis=1))[0][0]

print(X1,Y1,Index1)
print(X2,Y2,Index2)

直到两个或多个具有相同值的元组以Vals为单位(如示例中所示(,此操作才会正常运行。np.where返回这个元组在数组中的第一个出现,所以我得到了错误的索引。如何获得正确的索引?

for i, j in itertools.combinations(range(len(Vals)), 2):
print(Vals[i], i, "|", Vals[j], j)
[1. 1.] 0 | [2. 2.] 1
[1. 1.] 0 | [1. 2.] 2
[1. 1.] 0 | [2. 1.] 3
[1. 1.] 0 | [3. 3.] 4
[1. 1.] 0 | [3. 3.] 5
[2. 2.] 1 | [1. 2.] 2
[2. 2.] 1 | [2. 1.] 3
[2. 2.] 1 | [3. 3.] 4
[2. 2.] 1 | [3. 3.] 5
[1. 2.] 2 | [2. 1.] 3
[1. 2.] 2 | [3. 3.] 4
[1. 2.] 2 | [3. 3.] 5
[2. 1.] 3 | [3. 3.] 4
[2. 1.] 3 | [3. 3.] 5
[3. 3.] 4 | [3. 3.] 5

您是否尝试过使用zip迭代串联中的每个数组

最新更新