如何在 3 维数组中找到最接近的数组

How to find the most close array in the 3-dimensions array

以我对python & numpy的有限经验,我在网上搜索了很长时间。但是没有用。请帮助或尝试提供一些想法如何实现这一目标。

A=[3,-1, 4]

B = array([1,1,1],[1,-1,1],[1,1,-1])

The most close one in B is [1, -1, 1]
  1. 正负权重>(A,B)收盘
  2. 在B中找到最接近的(所有相同的Pos或Neg)

B1 = array([1,1,1], [1,-1,1], [1,1, -1], [3,1,4])

The result is [1,-1,1]

在四处寻找合适的 XX 解决方案后,发现那里的一切都很难使用。

提前致谢。

一种可能的方式:

A = np.array([3,-1, 4])

B = np.array([[1,1,1],[1,-1,1],[1,1,-1]])

# distances array-wise
np.abs(B - A)

# sum of absolute values of distances (smallest is closest)
np.sum(np.abs(B - A), axis=1)

# index of smallest (in this case index 1)
np.argmin(np.sum(np.abs(B - A), axis=1))

# all in one line (take array 1 from B)
result = B[np.argmin(np.sum(np.abs(B - A), axis=1))]

试试这个,

import numpy as np
A=np.array([3,-1, 4])
B =np.array([[1,1,1],[1,-1,1],[1,1,-1]])
x=np.inf
for val  in B:
    if (x>(np.absolute(A-val)).sum())and((np.sign(A)==np.sign(val)).all()==True):   
        x=(np.absolute(A-val)).sum()
        y=val
print x
print y