【问题标题】:How to find the most close array in the 3-dimensions array如何在 3 维数组中找到最接近的数组
【发布时间】:2018-03-16 09:49:22
【问题描述】:

根据我对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 解决方案后,发现那里的所有东西都很难使用。

提前致谢。

【问题讨论】:

  • 已添加到解决方案中

标签: python numpy tensor


【解决方案1】:

一种可能的方式:

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))]

【讨论】:

  • 非常感谢,您能否进行一些更改,先找到相同的 +/-,然后找到最接近的 cal,我将不胜感激
  • @Taufik_TF 我不太明白你的意思? B == A 将为您提供一个真/假数组,其中匹配为真。像这样使用np.where(B==A),它将为您提供所有完全匹配的索引。
【解决方案2】:

试试这个,

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

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多