I need a good, quick method for finding the 10 smallest real values from a numpy array that could have arbitrarily many nan
and/or inf
values.
我需要一个好的,快速的方法来从numpy数组中找到10个最小的实数值,这个数组可能有任意多个nan和/或inf值。
I need to identify the indices of these smallest real values, not the values themselves.
我需要确定这些最小实际值的索引,而不是值本身。
I have found the argmin
and nanargmin
functions from numpy. They aren't really getting the job done because I also want to specify more than 1 value, like I want the smallest 100 values, for example. Also they both return -inf
values as being the smallest value when it is present in the array.
我从numpy找到了argmin和nanargmin函数。他们并没有真正完成工作,因为我还想指定多于1个值,例如我想要最小的100个值。此外,它们都将-inf值返回为数组中存在的最小值。
heapq.nsmallest
kind of works, but it also returns nan
and -inf
values as smallest values. Also it doesn't give me the indices that I am looking for.
heapq.nsmallest类的工作,但它也返回nan和-inf值作为最小值。它也没有给我我正在寻找的指数。
Any help here would be greatly appreciated.
这里的任何帮助将不胜感激。
3 个解决方案
#1
10
The only values that should be throwing this out are the negative infinite ones. So try:
应该抛出的唯一值是负无限值。所以尝试:
import numpy as np
a = np.random.rand(20)
a[4] = -np.inf
k = 10
a[np.isneginf(a)] = inf
result = a[np.argsort(a)[:k]]
#2
2
It seems to me like you could just take the first n
finite values from your sorted array, instead of trying to modify the original array, which could be dangerous.
在我看来,你可以从排序数组中取出前n个有限值,而不是试图修改原始数组,这可能很危险。
n = 10
b = np.sort(a)
smalls = b[np.isfinite(b)][n:]
#3
1
you can find the index of inf
and Nan
like this:
你可以找到inf和Nan的索引,如下所示:
a=np.array([[12,12,111],[np.inf,np.inf,1,2,3],[np.nan,7,8]])
the you can loop through a
and check it with:
你可以循环并检查它:
for item in a:
idxInf=(np.isnan(a[item])).nonzero()
idxNan=(np.isnan(a[item])).nonzero()
i.e:
即:
In [17]: (np.isnan(a[2]))
Out[17]: array([ True, False, False], dtype=bool)
In [18]: (np.isnan(a[2])).nonzero()
Out[18]: (array([0]),)
#1
10
The only values that should be throwing this out are the negative infinite ones. So try:
应该抛出的唯一值是负无限值。所以尝试:
import numpy as np
a = np.random.rand(20)
a[4] = -np.inf
k = 10
a[np.isneginf(a)] = inf
result = a[np.argsort(a)[:k]]
#2
2
It seems to me like you could just take the first n
finite values from your sorted array, instead of trying to modify the original array, which could be dangerous.
在我看来,你可以从排序数组中取出前n个有限值,而不是试图修改原始数组,这可能很危险。
n = 10
b = np.sort(a)
smalls = b[np.isfinite(b)][n:]
#3
1
you can find the index of inf
and Nan
like this:
你可以找到inf和Nan的索引,如下所示:
a=np.array([[12,12,111],[np.inf,np.inf,1,2,3],[np.nan,7,8]])
the you can loop through a
and check it with:
你可以循环并检查它:
for item in a:
idxInf=(np.isnan(a[item])).nonzero()
idxNan=(np.isnan(a[item])).nonzero()
i.e:
即:
In [17]: (np.isnan(a[2]))
Out[17]: array([ True, False, False], dtype=bool)
In [18]: (np.isnan(a[2])).nonzero()
Out[18]: (array([0]),)