【问题标题】:Compare all elements in two numpy arrays without using a loop在不使用循环的情况下比较两个 numpy 数组中的所有元素
【发布时间】:2018-05-09 21:47:33
【问题描述】:

我有一个矩阵和矩阵中每一行的质心向量。我想将矩阵中的每个元素与向量中的每个元素进行比较,并找出质心向量中的哪个元素最接近矩阵中的数据点。有没有办法在不使用循环的情况下做到这一点?我将使用大量数据并希望它尽可能快

这是一个非常简单的python示例,说明我现在的做法:

import scipy as sp
test_array = sp.array([(1,1,1),(3,4,5),(6,12,18)])
sumx = test_array.sum(axis=1)
centroid_vector = sumx / len(test[0])
for i in centroid_vector:
    x = abs(test_array - i)
    minimum = sp.argmin(x)

所需的结果是具有最小距离、原始值(来自 test_array)和距离最小的质心向量中元素的索引的矩阵。在这种情况下,它看起来像这样:

[(0, 1, 1), 
 (0, 1, 1), 
 (0, 1, 1),
 (1, 3, 2), 
 (0, 4, 2),
 ...
 (6, 18, 3)]

【问题讨论】:

  • test 是一个 Numpy ndarray 吗?
  • 您希望一个最小值(在所有矩阵'值和所有向量'值之间)还是三个最小值(centroid_vector 的每个 i 一个最小值)?
  • 请为您的示例发布所需的结果。
  • np.unravel_index(np.argmin(np.abs(np.subtract.outer(test.mean(axis=1), test.ravel())), axis=1), test.shape) 为质心的每个分量提供test 中最接近值的坐标。输出:(array([0, 1, 2]), array([0, 1, 1]))。这些是左上角,中心和底部中心,
  • @wwii,是的,它是一个 numpy 数组,导入了 scipy

标签: python numpy scipy


【解决方案1】:

这是您问题的一种解决方案:

import scipy as sp
test_array = sp.array([(1,1,1),(3,4,5),(6,12,18)])
# Create the centroid another way but yours is fine
centroid_vector = test_array.sum(axis=1)/test_array.shape[1]
# Generate an array with all the difference between 
# each element of test_array (row) and centroid_vector (column)
delta_array = abs(test_array.reshape((9,1)) - centroid_vector)
# Finally, the first column of your output is delta_array.min(axis=1), 
# the second is test_array.reshape((9,1))
# and the third is delta_array.argmin(axis=1)
# so you can do:
array_output = sp.array([delta_array.min(axis=1),test_array.reshape((9)),
                         delta_array.argmin(axis=1)]).transpose()

注意:centroid_vector 中的元素索引从 0(python 约定)开始,而不是您问题中的 1,但如果您希望在第三列中使用 1、2 或 3,只需执行 delta_array.argmin(axis=1) +1

注意2:避免使用sum作为变量名,它是一个内置函数,在你的代码中可能会带来一些麻烦。

【讨论】:

    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多