在python中找到numpy数组(或元组)中的最小/最大排除零

时间:2022-05-26 21:27:36

I have an array. The valid values are not zero (either positive or negetive). I want to find the minimum and maximum within the array which should not take zeros into account. For example if the numbers are only negative. Zeros will be problematic.

我有一个阵列。有效值不为零(正或负)。我想在数组中找到不应该考虑零的最小值和最大值。例如,如果数字只是负数。零会有问题。

5 个解决方案

#1


52  

How about:

import numpy as np
minval = np.min(a[np.nonzero(a)])
maxval = np.max(a[np.nonzero(a)])

where a is your array.

其中a是你的数组。

#2


15  

If you can choose the "invalid" value in your array, it is better to use nan instead of 0:

如果您可以在数组中选择“无效”值,则最好使用nan而不是0:

>>> a = numpy.array([1.0, numpy.nan, 2.0])
>>> numpy.nanmax(a)
2.0
>>> numpy.nanmin(a)
1.0

If this is not possible, you can use an array mask:

如果无法做到这一点,可以使用数组掩码:

>>> a = numpy.array([1.0, 0.0, 2.0])
>>> ma = numpy.ma.masked_equal(a, 0.0, copy=False)
>>> ma.max()
2.0
>>> ma.min()
1.0

Compared to Josh's answer using advanced indexing, this has the advantage of avoiding to create a copy of the array.

与使用高级索引的Josh的答案相比,这具有避免创建阵列副本的优点。

#3


2  

Here's another way of masking which I think is easier to remember (although it does copy the array). For the case in point, it goes like this:

这是另一种屏蔽方式,我认为更容易记住(尽管它会复制数组)。就这一点而言,它是这样的:

>>> import numpy
>>> a = numpy.array([1.0, 0.0, 2.0])
>>> ma = a[a != 0]
>>> ma.max()
2.0
>>> ma.min()
1.0
>>> 

It generalizes to other expressions such as a > 0, numpy.isnan(a), ... And you can combine masks with standard operators (+ means OR, * means AND, - means NOT) e.g:

它推广到其他表达式,如a> 0,numpy.isnan(a),......你可以将掩码与标准运算符组合(+表示OR,*表示AND, - 表示NOT),例如:

# Identify elements that are outside interpolation domain or NaN
outside = (xi < x[0]) + (eta < y[0]) + (xi > x[-1]) + (eta > y[-1])
outside += numpy.isnan(xi) + numpy.isnan(eta)
inside = -outside
xi = xi[inside]
eta = eta[inside]

#4


0  

A simple way would be to use a list comprehension to exclude zeros.

一种简单的方法是使用列表推导来排除零。

>>> tup = (0, 1, 2, 5, 2)
>>> min([x for x in tup if x !=0])
1

#5


0  

You could use a generator expression to filter out the zeros:

您可以使用生成器表达式过滤掉零:

array = [-2, 0, -4, 0, -3, -2]
max(x for x in array if x != 0)

#1


52  

How about:

import numpy as np
minval = np.min(a[np.nonzero(a)])
maxval = np.max(a[np.nonzero(a)])

where a is your array.

其中a是你的数组。

#2


15  

If you can choose the "invalid" value in your array, it is better to use nan instead of 0:

如果您可以在数组中选择“无效”值,则最好使用nan而不是0:

>>> a = numpy.array([1.0, numpy.nan, 2.0])
>>> numpy.nanmax(a)
2.0
>>> numpy.nanmin(a)
1.0

If this is not possible, you can use an array mask:

如果无法做到这一点,可以使用数组掩码:

>>> a = numpy.array([1.0, 0.0, 2.0])
>>> ma = numpy.ma.masked_equal(a, 0.0, copy=False)
>>> ma.max()
2.0
>>> ma.min()
1.0

Compared to Josh's answer using advanced indexing, this has the advantage of avoiding to create a copy of the array.

与使用高级索引的Josh的答案相比,这具有避免创建阵列副本的优点。

#3


2  

Here's another way of masking which I think is easier to remember (although it does copy the array). For the case in point, it goes like this:

这是另一种屏蔽方式,我认为更容易记住(尽管它会复制数组)。就这一点而言,它是这样的:

>>> import numpy
>>> a = numpy.array([1.0, 0.0, 2.0])
>>> ma = a[a != 0]
>>> ma.max()
2.0
>>> ma.min()
1.0
>>> 

It generalizes to other expressions such as a > 0, numpy.isnan(a), ... And you can combine masks with standard operators (+ means OR, * means AND, - means NOT) e.g:

它推广到其他表达式,如a> 0,numpy.isnan(a),......你可以将掩码与标准运算符组合(+表示OR,*表示AND, - 表示NOT),例如:

# Identify elements that are outside interpolation domain or NaN
outside = (xi < x[0]) + (eta < y[0]) + (xi > x[-1]) + (eta > y[-1])
outside += numpy.isnan(xi) + numpy.isnan(eta)
inside = -outside
xi = xi[inside]
eta = eta[inside]

#4


0  

A simple way would be to use a list comprehension to exclude zeros.

一种简单的方法是使用列表推导来排除零。

>>> tup = (0, 1, 2, 5, 2)
>>> min([x for x in tup if x !=0])
1

#5


0  

You could use a generator expression to filter out the zeros:

您可以使用生成器表达式过滤掉零:

array = [-2, 0, -4, 0, -3, -2]
max(x for x in array if x != 0)