[英]How to find index of minimum non zero element with numpy?
I have a 4x1 array that I want to search for the minimum non zero value and find its index.我有一个 4x1 数组,我想搜索最小的非零值并找到它的索引。 For example:例如:
theta = array([0,1,2,3]).reshape(4,1)
It was suggested in a similar thread to use nonzero() or where(), but when I tried to use that in the way that was suggested, it creates a new array that doesn't have the same indices as the original:在类似的线程中建议使用 nonzero() 或 where(),但是当我尝试按照建议的方式使用它时,它创建了一个与原始索引不同的新数组:
np.argmin(theta[np.nonzero(theta)])
gives an index of zero, which clearly isn't right.给出的索引为零,这显然是不正确的。 I think this is because it creates a new array of non zero elements first.我认为这是因为它首先创建了一个新的非零元素数组。 I am only interested in the first minimum value if there are duplicates.如果有重复,我只对第一个最小值感兴趣。
np.nonzero(theta)
returns the index of the values that are non-zero. np.nonzero(theta)
返回非零值的索引。 In your case, it returns,在你的情况下,它返回,
[1,2,3]
Then, theta[np.nonzero(theta)] returns the values然后, theta[np.nonzero(theta)] 返回值
[1,2,3]
When you do np.argmin(theta[np.nonzero(theta)])
on the previous output, it returns the index of the value 1
which is 0.当您对前一个输出执行np.argmin(theta[np.nonzero(theta)])
时,它返回值为 0 的值1
的索引。
Hence, the correct approach would be:因此,正确的做法是:
i,j = np.where( theta==np.min(theta[np.nonzero(theta)]))
where i,j
are the indices of the minimum non zero element of the original numpy array i,j = np.where( theta==np.min(theta[np.nonzero(theta)]))
其中i,j
是原始 numpy 数组的最小非零元素的索引
theta[i,j]
or theta[i]
gives the respective value at that index. theta[i,j]
或theta[i]
给出该索引处的相应值。
#!/usr/bin/env python
# Solution utilizing numpy masking of zero value in array
import numpy as np
import numpy.ma as ma
a = [0,1,2,3]
a = np.array(a)
print "your array: ",a
# the non-zero minimum value
minval = np.min(ma.masked_where(a==0, a))
print "non-zero minimum: ",minval
# the position/index of non-zero minimum value in the array
minvalpos = np.argmin(ma.masked_where(a==0, a))
print "index of non-zero minimum: ", minvalpos
I think you @Emily were very close to the correct answer.我认为您@Emily 非常接近正确答案。 You said:你说:
np.argmin(theta[np.nonzero(theta)])
gives an index of zero, which clearly isn't right.np.argmin(theta[np.nonzero(theta)])
给出的索引为零,这显然是不正确的。 I think this is because it creates a new array of non zero elements first.我认为这是因为它首先创建了一个新的非零元素数组。
The last sentence is correct => the first one is wrong since it is expected to give the index in the new array.最后一句是正确的 => 第一句是错误的,因为它应该给出新数组中的索引。
Let's now extract the correct index in the old (original) array:现在让我们在旧的(原始)数组中提取正确的索引:
nztheta_ind = np.nonzero(theta)
k = np.argmin(theta[nztheta_ind])
i = nztheta_ind[0][k]
j = nztheta_ind[1][k]
or:或者:
[i[k] for i in nztheta_ind]
for arbitrary dimensionality of original array.对于原始数组的任意维数。
i = np.unravel_index(np.where(theta!=0, theta, theta.max()+1).argmin(), theta.shape)
t0
.屏蔽零会创建t0
。 There are other ways, see the perfplot.还有其他方法,请参阅 perfplot。unravel_index
fixes this problem, and hasn't been suggested yet. unravel_index
修复了这个问题,目前还没有被推荐。theta = np.triu(np.random.rand(4,4), 1) # example array
t0 = np.where(theta!=0, theta, np.nan) # 1
i0 = np.nanargmin(t0) # 2
i = np.unravel_index(i0, theta.shape) # 3
print(theta, i, theta[i]) #
mask:i = np.unravel_index(np.ma.masked_where(a==0, a).argmin(), a.shape)
掩码:i = np.unravel_index(np.ma.masked_where(a==0, a).argmin(), a.shape)
nan:i = np.unravel_index(np.nanargmin(np.where(a!=0, a, np.nan)), a.shape)
nan:i = np.unravel_index(np.nanargmin(np.where(a!=0, a, np.nan)), a.shape)
max:i = np.unravel_index(np.where(a!=0, a, a.max()+1).argmin(), a.shape)
最大值:i = np.unravel_index(np.where(a!=0, a, a.max()+1).argmin(), a.shape)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.