【问题标题】:Extracting index of minimum point in 2D array提取二维数组中最小点的索引
【发布时间】:2019-08-31 16:40:04
【问题描述】:

我在一个函数中测试 A 和 B 的值,需要找到该函数的最小值以及 A 和 B 的什么值产生这个最小值。我正在努力寻找一种方法将这些值从我制作的数组中取出。

我从定义一些数组开始

ones = np.ones((3456,5184))
array = np.zeros([5,5])
real = threshold
test = testsunthree

real 和 test 是在代码早期定义的数组。

for A in range(-2,2):
  for B in range(-2,2):
    array[A,B]=((np.mean((real-(test*(A*0.1)+((B*0.1)*ones))**2))))

在这里,我在 -2 和 2 之间的范围内测试 A 和 B 的值,并将每个值插入到函数中。然后将该函数的值存储在上面定义的空数组中。

array_min = array[array != 0].min()
print (array_min)
print zip(*np.where(array == array.min()))

然后我试图确定该数组的最小值,并想找出 A 和 B 的哪些值是该函数的最小值。这是我正在努力解决的部分,因为最后两行给出的值不正确。

【问题讨论】:

  • 你能复制粘贴整个代码吗?未定义“阈值”且未导入 numpy,我们可以更轻松地处理您的代码。
  • threshold 实际上是太阳图像的数组,所以我无法在此处导入该图像,数组的大小为 (3456, 5184)

标签: python arrays indexing minimum


【解决方案1】:

这是为您提供名为array 的数组的最小值。我为thresholdtestsunthree 设置了随机值,以使代码正常工作并更改min() 函数的利用率

import numpy as np

threshold = 4
testsunthree = 2
ones = np.ones((3456,5184))
array = np.zeros([5,5])
real = threshold
test = testsunthree

for A in range(-2,2):
  for B in range(-2,2):
     array[A,B]=((np.mean((real-(test*(A*0.1)+((B*0.1)*ones))**2))))

array_min = np.min(array)
print (array_min)

我很快做了一个肮脏的解决方案来获得不同的 A,B 值,你有你的数组的最小值,它可以很容易地改进,因为它只是一个快速的解决方案来告诉你方法

index = np.where(array == array_min)
A_B_value = []

for i in range(0, len(index[0])):
    min_index = []
    min_index.append(index[0][i])
    min_index.append(index[1][i])
    A_B_value.append(min_index)

print(A_B_value)

【讨论】:

  • 我怎样才能得到给出这个最小值的 A 和 B 的值?
猜你喜欢
  • 1970-01-01
  • 2015-09-06
  • 2015-07-22
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 2013-05-02
相关资源
最近更新 更多