python - numpy 数组项之间的最小二乘差

标签 python numpy

因为 High performance calculation of least squares difference from all possible combinations (n lists),我才开始学习一些 numpy :

现在我被计算困住了,需要一些帮助。

我有一个像这样的 numpy 数组对象:

>>> items
array([[ 246, 1143, 1491, ..., 1167,  325, 1158],
       [ 246, 1143, 1491, ..., 1167,  519, 1158],
       [ 246, 1143, 1491, ..., 1167,  507, 1158],
       ..., 
       [1491, 1143,  246, ..., 1167,  325, 1158],
       [1491, 1143,  246, ..., 1167,  519, 1158],
       [1491, 1143,  246, ..., 1167,  507, 1158]])

我想得到他的所有成员中平方差最小的数组的编号,一个 numpythonic 版本:

for num,item in enumerate(items): #Calculate for each list of items
      for n in range(len(item)):
        for i in range(n, len(item)):
          dist += (item[n]-item[i])**2 #Key formula
          if dist>min_dist: #This is a shortcut
              break
          else:
              continue
          break               
      if min_dist is None or dist < min_dist:
        min_dist = dist
        best = num #We get the number of the combination we want

如有任何提示,我将不胜感激。

最佳答案

初始化您的 NxM 数组:

>>> import numpy as np
>>> items = np.random.random_sample((10,3))

计算每个 N M 维向量的所有元素之间的平方和,并将结果存储在列表中:

>>> sq = [(np.subtract.outer(item,item) ** 2).sum() for item in items]

找到所有元素之间的平方和最小的向量的索引:

>>> best_index = np.argmin(sq)

或者,为了避免中间列表:

best = np.inf
best_index = None
for i,item in enumerate(items):
    ls = (np.subtract.outer(item,item) ** 2).sum()
    if ls < best:
        best = ls
        best_index = i

关于python - numpy 数组项之间的最小二乘差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13749943/

相关文章:

python - OpengL 我的方格在哪里?

python - 如何在tensorflow中处理大量数据?

python - 在进程对象之间共享 SciPy 稀疏数组

Python 和 re.compile 返回不一致的结果

python - 根据索引值对数据框列执行计算

python - Numpy 广播数组

python - 如何将 scipy 行矩阵转换为 numpy 数组

python - 如何为我的数据集标记或勾选 Pandas?

python - 如何在Python中实现快速排序

python - 使用 Python 3.8 的 Jupyter Notebook - NotImplementedError