简体   繁体   English

提取二维数组中最小点的索引

[英]Extracting index of minimum point in 2D array

I'm testing values of A and B in a function and need to find the mimum value of that function and what values of A and B produce this minimum. 我正在测试函数中A和B的值,需要找到该函数的最小值以及A和B的哪个值产生这个最小值。 I'm struggling to find a way to get these values out of the array I have made. 我正在努力找到一种方法来从我已经制作的数组中获取这些值。

I start by defining some arrays 我首先定义一些数组

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

Where real and test are arrays defined early in the code. 其中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))))

Here I am testing values of A and B over the range between -2 and 2 and for each value plugging them into the function. 在这里,我测试A和B的值在-2和2之间的范围内,并且每个值将它们插入到函数中。 The value of that function is then stored in the empty array defined above. 然后该函数的值存储在上面定义的空数组中。

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

I am then trying to identify the minimum value of that array and want to find for what values of A and B is this function at its minimum. 然后我试图确定该数组的最小值,并希望找到A和B的最小值是这个函数的最小值。 This is the part I am struggling with as the last two lines are giving incorrect values. 这是我正在努力的部分,因为最后两行给出了不正确的值。

This is giving you the minimum value of the array called array . 这将为您提供名为array的数组的最小值。 I put random values for threshold and testsunthree to make the code work and change the utilisation of the min() function 我为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)

I quickly made a dirty solution to obtain the different A,B values for which you have the min value of your array, it can be improved easily as it's only a quick fix to show you the way 我很快做了一个肮脏的解决方案,以获得不同的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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM