如何在Python中比较两个数组并找到最佳匹配?

如何在Python中比较两个数组并找到最佳匹配?,python,arrays,numpy,scipy,Python,Arrays,Numpy,Scipy,我有两个数组X和Y,X是基本数组,Y在一个循环中运行。循环运行时,我想比较数组,以找到最接近的Y到X的值,或者换句话说,其中Y最接近X。作为示例,我附上了可复制代码: from __future__ import division import numpy as np import matplotlib.pyplot as plt from scipy import interpolate x = np.array([[0.12, 0.11, 0.1, 0.09, 0.08],

我有两个数组X和Y,X是基本数组,Y在一个循环中运行。循环运行时,我想比较数组,以找到最接近的Y到X的值,或者换句话说,其中Y最接近X。作为示例,我附上了可复制代码:

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate


x = np.array([[0.12, 0.11, 0.1, 0.09, 0.08],
       [0.13, 0.12, 0.11, 0.1, 0.09],
       [0.15, 0.14, 0.12, 0.11, 0.1],
       [0.17, 0.15, 0.14, 0.12, 0.11],
       [0.19, 0.17, 0.16, 0.14, 0.12],
       [0.22, 0.19, 0.17, 0.15, 0.13],
       [0.24, 0.22, 0.19, 0.16, 0.14],
       [0.27, 0.24, 0.21, 0.18, 0.15],
       [0.29, 0.26, 0.22, 0.19, 0.16]])

y = np.array([[0.07, 0.06, 0.05, 0.04, 0.03],
              [0.08, 0.07, 0.06, 0.05, 0.04],
              [0.10, 0.09, 0.07, 0.06, 0.05],
              [0.14, 0.12, 0.11, 0.09, 0.08],
              [0.16, 0.14, 0.13, 0.11, 0.09],
              [0.19, 0.16, 0.14, 0.12, 0.10],
              [0.22, 0.20, 0.17, 0.14, 0.12],
              [0.25, 0.22, 0.19, 0.16, 0.13],
              [0.27, 0.24, 0.20, 0.17, 0.14]])


for i in range(100):
    y = y + (i / 10000)

我想在找到最接近的值时打破循环。我的意思是,这些值应该在原始值的±10%或其他百分比范围内。在Python中如何实现这一点?

您可以计算两个矩阵之间的欧几里德距离:

import numpy as np
import scipy.spatial.distance
import matplotlib.pyplot as plt
x = np.array([[0.12, 0.11, 0.1, 0.09, 0.08],
   [0.13, 0.12, 0.11, 0.1, 0.09],
   [0.15, 0.14, 0.12, 0.11, 0.1],
   [0.17, 0.15, 0.14, 0.12, 0.11],
   [0.19, 0.17, 0.16, 0.14, 0.12],
   [0.22, 0.19, 0.17, 0.15, 0.13],
   [0.24, 0.22, 0.19, 0.16, 0.14],
   [0.27, 0.24, 0.21, 0.18, 0.15],
   [0.29, 0.26, 0.22, 0.19, 0.16]])
y = np.array([[0.07, 0.06, 0.05, 0.04, 0.03],
          [0.08, 0.07, 0.06, 0.05, 0.04],
          [0.10, 0.09, 0.07, 0.06, 0.05],
          [0.14, 0.12, 0.11, 0.09, 0.08],
          [0.16, 0.14, 0.13, 0.11, 0.09],
          [0.19, 0.16, 0.14, 0.12, 0.10],
          [0.22, 0.20, 0.17, 0.14, 0.12],
          [0.25, 0.22, 0.19, 0.16, 0.13],
          [0.27, 0.24, 0.20, 0.17, 0.14]])

dists = []
for i in range(100):
     y = y + (i / 10000.)
     dists.append(scipy.spatial.distance.euclidean(x.flatten(), y.flatten()))
plt.plot(dists)
将返回此图,它是两个矩阵之间欧几里德距离的演化:

要至少中断循环,可以使用:

dist = np.inf
for i in range(100):
    y = y + (i / 10000.)
    d = scipy.spatial.distance.euclidean(x.flatten(), y.flatten())
    if d < dist:
        dist = d
    else:
        break
print dist
# 0.0838525491562 #(the minimal distance)
print y
#[[ 0.1051  0.0951  0.0851  0.0751  0.0651] 
#[ 0.1151  0.1051  0.0951  0.0851  0.0751] 
#[ 0.1351  0.1251  0.1051  0.0951  0.0851] 
#[ 0.1751  0.1551  0.1451  0.1251  0.1151] 
#[ 0.1951  0.1751  0.1651  0.1451  0.1251] 
#[ 0.2251  0.1951  0.1751  0.1551  0.1351] 
#[ 0.2551  0.2351  0.2051  0.1751  0.1551] 
#[ 0.2851  0.2551  0.2251  0.1951  0.1651] 
#[ 0.3051  0.2751  0.2351  0.2051  0.1751]]
dist=np.inf
对于范围(100)内的i:
y=y+(i/10000)
d=scipy.spatial.distance.euclidean(x.flatten(),y.flatten())
如果d
定义
最佳匹配
最接近的
术语。人们使用不同的方法来测量矩阵(数组)的接近度。最大化确定系数,可能是请用此澄清更新您的问题不要忘记在
循环中将
i
除以
中的浮点数,以增加
y
y=y+(i/10000.)
)请更正y增量以确保一致性(请参见上面的注释),实际上您可以使用许多不同的度量:请参见